Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Drop-right Menus With Pure CSS, Just for educational purposes, not to be used for real sites
pyost
post Jan 6 2007, 08:01 PM
Post #1


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,013
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500



I am sure you've all encountered drop-down (not to say drop-right) menus. While these may seem pretty hard to make, it is actually extremely easy. Actually, it can get rather hard when using JavaScript, but this tutorial will show you how to create good-looking "pop up" menus opening to the right with pure CSS.

However, I must warn you that this tutorial is more for educational purposes, then for use in real web sites. Why? Simply because the following method won't work properly in Internet Explorer (how unexpected..). I am sure this might as well work with several (JavaScript) hacks, but that's not how W3C compliant browser should behave like, is it? On the other hand, the example works perfectly fine in Firefox, and that's the browser I suggest using for accomplishing this tutorial.

Let's start, shall we? First, I will show you what HTML structure we will be using:

HTML
<ul>

<li><a href="#">Level 1 | Link 1</a></li>
<li><a href="#">Level 1 | Link 2</a></li>
<li><a href="#">Level 1 | Link 3</a></li>
<li><a href="#">Level 1 | Link 4</a></li>
<li><a href="#">Level 1 | Link 5</a></li>

</ul>


As you can see, here we have an unordered list, with five elements. Each of these elements is one level 1 link. Obviously, it means that this is the main part of the menu that will always be visible. Now, in order to have sub levels that appear when we hover a link, we shall insert another unordered list into the wanted link.

HTML
<ul>

<li>
<a href="#">Level 1 | Link 1</a>
<ul>
<li><a href="#">Level 2 | Link 1</a></li>
<li><a href="#">Level 2 | Link 2</a></li>
<li><a href="#">Level 2 | Link 3</a></li>
</ul>
</li>

<li><a href="#">Level 1 | Link 2</a></li>

<li>
<a href="#">Level 1 | Link 3</a>
<ul>
<li><a href="#">Level 2 | Link 1</a></li>
<li><a href="#">Level 2 | Link 2</a></li>
</ul>
</li>

<li><a href="#">Level 1 | Link 4</a></li>
<li><a href="#">Level 1 | Link 5</a></li>

</ul>


Just a notice - we don't insert the new list between <a> </a> tags, but rather between <li> </li> tags. Anyhow, now we've added two sub menus, one (with 3 links) connected to the first link, and the other (with 2 links) connected to the third. Finally, we shall add another (third) level with 4 links, which will be connected to Link 2, Level 2.

HTML
<ul>

<li>
<a href="#">Level 1 | Link 1</a>
<ul>
<li><a href="#">Level 2 | Link 1</a></li>
<li>
<a href="#">Level 2 | Link 2</a>
<ul>
<li><a href="#">Level 3 | Link 1</a></li>
<li><a href="#">Level 3 | Link 2</a></li>
<li><a href="#">Level 3 | Link 3</a></li>
<li><a href="#">Level 3 | Link 4</a></li>
</ul>
</li>
<li><a href="#">Level 2 | Link 3</a></li>
</ul>
</li>

<li><a href="#">Level 1 | Link 2</a></li>

<li>
<a href="#">Level 1 | Link 3</a>
<ul>
<li><a href="#">Level 2 | Link 1</a></li>
<li><a href="#">Level 2 | Link 2</a></li>
</ul>
</li>

<li><a href="#">Level 1 | Link 4</a></li>
<li><a href="#">Level 1 | Link 5</a></li>

</ul>


I know the code might seem a bit confusing, but I can't indent it when using the HTML tag dry.gif Now, copy this code into a HTML file, save it, and then open it. What did you get? Not what we need yet, but you can see the structure of the menu. Now that we have (almost) completed the HTML part, let's move on to the CSS part.

We will start with defining some styles for the <ul> tag:

CODE

ul {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 150px;
}


What do we have here? list-style: none removes the bulletins (you know, those dots and squares), margin and padding remove the indenting and width defines how much space (horizontally) the menu will take up. If you preview the page now, you will see all the links one below each other. Let's put these links into boxes/rectangles.

CODE

li {
    border-top: 1px solid #dedede;
    border-left: 1px solid #dedede;
    border-right: 1px solid #dedede;
}

ul {
    border-bottom: 1px solid #dedede;
}

li a {
    display: block;
    color: #000000;
    text-decoration: none;
    padding-left: 5px;
    padding-right: 5px;
    font-family: Arial;
    font-size: 12px;
    line-height: 20px;
}

li a:hover {
    background-color: #f0f0f0;
}


In the li part we are "creating" the box. We haven't defined border-bottom because then we would have double borders (top + bottom), and we don't want that. That's why we have added it to the ul part.

Moving on to the li a code. All but the first line is somewhat connected to how the link will look. By writing display: block, we have told the browser that the link (<a> </a>) will take up as much space as it can, therefore becoming a box with the border defined in the li code. Try previewing the page now, so you can see what part of the box is hyperlinked - yes, the whole box! Obviously, the li a:hover part determines what happens when we hover over a linked area

So now we are finally getting something. Feel free to ignore those double border appearing at some places, this will be sorted out as soon as we hide those parts. And how are we going to do that? By using relative/absolute positioning. In short, this will help us position a sub menu according to its parent link. Very ease to accomplish, yet very powerful. Here's what we will be adding to the CSS code:

CODE

li {
    position: relative;
}

li ul {
    display: none;
    position: absolute;
    top: -1px;
    left: 148px;
}


li:hover ul {
    display: block;
}


Now, this might be the hardest part to understand. The position: relative in the li code tells the browser that any elements "below" the li tag and with position: absolute should be positioned relatively to <li>. Without top and left, the cell and the child list would overlap. Since we want to move the child list right, we use left: 148px;, and top: -1px; because of the border. When you try it out, you will see what I am talking about.

But wait, it's not working right! When we hover over the first link, we get two submenus, and we only want one. That is achieved by adding additional CSS attributes that tell the browser not to show the child of the child tongue.gif Practically, this is what we need:

CODE
li:hover ul li ul {
    display: none;
    position: absolute;
    top: -1px;
    left: 148px;
}

li:hover ul li:hover ul {
    display: block;
}


So, when we are hovering over the main level item, it will remain hidden. But when we hover of the submenu item, it will be shown. You would need to add a modification of this every time you add a new submenu.

And now everything is working!

Finally, I said it once, but will say it again: this tutorial will just help you understand better how CSS works and what it can be used for. Don't use this on your own web sites, since you will put off visitors using Internet Explorer.

If you have any more enquires concerning the code, feel free to ask me.

Oh, and here is the complete CSS file

CODE
ul {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 150px;
    border-bottom: 1px solid #dedede;
}

li {
    border-top: 1px solid #dedede;
    border-left: 1px solid #dedede;
    border-right: 1px solid #dedede;
    position: relative;
}

li a {
    display: block;
    color: #000000;
    text-decoration: none;
    padding-left: 5px;
    padding-right: 5px;
    font-family: Arial;
    font-size: 12px;
    line-height: 20px;
}

li a:hover {
    background-color: #f0f0f0;
}

li ul {
    display: none;
    position: absolute;
    top: -1px;
    left: 148px;
}


li:hover ul {
    display: block;
}

li:hover ul li ul {
    display: none;
    position: absolute;
    top: -1px;
    left: 148px;
}

li:hover ul li:hover ul {
    display: block;
}
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Jan 7 2007, 05:51 AM
Post #2


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411



Good one man smile.gif Makes the inner workings seem really simple..

Cheers,
m^e
Go to the top of the page
 
+Quote Post
toby
post Jan 8 2007, 10:53 AM
Post #3


Super Member
Group Icon

Group: Members
Posts: 511
Joined: 29-September 06
Member No.: 16,228



Wow.. nice one.
Go to the top of the page
 
+Quote Post
saint-michael
post Jan 9 2007, 09:44 AM
Post #4


SM- the Man -The Myth - The Legend Himself
Group Icon

Group: Members
Posts: 443
Joined: 4-September 05
From: Drinking da rootbeers
Member No.: 8,313



not bad it could save me the time of reverse engineering a left drop down menu.

I seen menu's at cssplay that up inside of down for those who want footer links and such. Of course for some strange reason though when I look at tutorials for suckerfish drop downs it looks greek to me, most likely due to the fact can't bring down the menu's right when it's all crunch together like that.
Go to the top of the page
 
+Quote Post
nightfox
post Jan 10 2007, 11:21 AM
Post #5


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



Wow... very nice! It seems very nice! I'll have to see how it works someday.

[N]F

This post has been edited by nightfox: Jan 10 2007, 11:21 AM
Go to the top of the page
 
+Quote Post
CaptainRon
post Jan 11 2007, 07:49 AM
Post #6


Premium Member
Group Icon

Group: Members
Posts: 238
Joined: 9-September 05
Member No.: 8,400



Thats a cool article. I will try putting in efforts to make it run on IE if i find time. I guess it shall run on IE7...
Go to the top of the page
 
+Quote Post
livingston
post Feb 17 2007, 07:08 PM
Post #7


Advanced Member
Group Icon

Group: Members
Posts: 149
Joined: 14-February 07
From: Tuticorin, India
Member No.: 20,415



that is a great peice of code thanx for sharing, it would be better if you added a live working page for us to see it in action.

This post has been edited by livingston: Feb 25 2007, 08:52 PM
Go to the top of the page
 
+Quote Post
Niru
post Feb 18 2007, 02:08 PM
Post #8


Advanced Member
Group Icon

Group: Members
Posts: 190
Joined: 18-August 06
From: Fun.NiranVv.Com
Member No.: 15,325



Thats a neat a cool tutorial friend! Dropdown menus are now an essential part of each and every site!
thanks for sharing tha5t with us friend
cool.gif

Niru
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Logo(0)
  2. Here Are Some Great Php Tutorial Sites(4)
  3. How To Make Realistic Fire!(15)
  4. Real Driving(24)
  5. Great Software For Building Flash Web Sites(1)
  6. Some Of The Best Soccer Sites ( Add More To This )(5)
  7. Making Educational Game(3)
  8. Problem With Drag And Drop (or So It Seems).(11)
  9. Tennis Sites(4)
  10. full flash sites(60)
  11. Content Sites And Mini Sites(1)
  12. Bypassing Filtered Sites(29)
  13. Music Sites(25)
  14. Online Dating Sites(15)
  15. Free Photo Storage Sites.(10)
  1. Sitepoint's Css And Html Reference Sites(2)
  2. Psp Web-browser(12)
  3. Anyone Into The Number 11 ?(10)
  4. Juggling An Iframe Box With Xhtml Sites(8)
  5. Network Places: Alternative To Ftp On Windows(10)
  6. Paint.NET(13)
  7. Tremulous Is A Free And Open Source Team-based First-person Shooter With Real-time Strategy Elements(0)
  8. Drag And Drop Tutorial(5)
  9. Real Player(12)
  10. Acer Desktop/laptop Drivers Download(0)
  11. Can't Access Astahost Hosteed Sites (including Cpanel And Ftp)(21)
  12. Persian Sites(2)
  13. How Do I Find A Site's Ip Address?(15)
  14. My Real Nick(4)
  15. The Most Pathetic Web Sites On The Internet Awards(13)


 



- Lo-Fi Version Time is now: 13th October 2008 - 04:34 AM