Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 243,559 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,442 people online right now. Registration is fast and FREE... Join Now!




CSS pretty Horizontal and Vertical Menus

 
Reply to this topicStart new topic

> CSS pretty Horizontal and Vertical Menus

Rating  5
supersssweety
Group Icon



post 18 Sep, 2007 - 12:44 PM
Post #1


Rollover menus are all the rage. I can't stand a menu that doesn't rollover * cough * To me it is just feels icky when I mouse over a menu item and nothing happens. CSS makes pretty menus quite easy. Today we are going to learn how to make horizontal and vertical menus that are pretty and creative smile.gif Let's get started.

First I will show you the basics with a horizontal menu, so lets make a list:

CODE

<div id="menu1">
<ul>
   <li><a href="#">Freaking</a></li>
   <li><a href="#">Pretty</a></li>
   <li><a href="#">CSS</a></li>
   <li><a href="#">Menu</a></li>
   <li><a href="#">Just</a></li>
   <li><a href="#">For</a></li>
   <li><a href="#">You</a></li>
</ul>  
</div>


Easy enough. Now let's start off with the basics for a horizontal menu:

CODE

#menu1{
    height:25px;
    font-family:Verdana, Arial, Helvetica, sans-serif;
}
#menu1 ul{
    margin:0;
    padding:0;
    list-style-type:none;
}
#menu1 ul li{
    display:inline;
}
#menu1 ul li a{
    text-decoration:none;
    float:left;
    height:25;
    margin:12px 0;
    padding:15px;
    width:65px;
    text-align:center;
}


IPB Image

Well thats nice but it sure is ugly! Let's make it pretty now...time to go to photoshop...don't freak out it is the easiest step: Make a new file that is 25px in height and 3px in width:

IPB Image

Pick any two colors you like, put one on the forground and one on the background. I am going to go with dream-in-code orange. Now get your gradient tool, make sure the gradient is set on foreground to background:

IPB Image

Now make a line from the top to bottom and save it for the web in your images folder:

IPB Image

Now make a line from the bottom to the top and do the same:

IPB Image

Now here add this to your CSS, using your own image names:

CODE

#menu1 a:link, #menu1 a:visited{
    color:#fff;
    background-image:url(images/link.gif);
    background-repeat:repeat-x;
    background-position:center;
}
#menu1 a:hover, #menu1 a:active{
    color:#000;
    background-image:url(images/hover.gif);
    background-repeat:repeat-x;
    background-position:center;
}



Still not good enough for ya? OK let's spruce it up a bit with a border:

CODE

#menu1 a:link, #menu1 a:visited{
    color:#fff;
    background-image:url(images/link.gif);
    background-repeat:repeat-x;
    background-position:center;
    border: 1px solid #f16b12;
}
#menu1 a:hover, #menu1 a:active{
    color:#000;
    background-image:url(images/hover.gif);
    background-repeat:repeat-x;
    background-position:center;
    border: 1px solid #e89642;
}


IPB Image

Wow!!! Pretty!!...as pretty as orange can be...ok let's make a vertical one, use the same unordered list and here is the basic CSS for the vertical menu:

CODE

#menu2{
    padding:24px;
    font-family:Verdana, Arial, Helvetica, sans-serif;
}
#menu2 ul {
    margin:0px;
    padding:0px;
    list-style-type:none;
}
#menu2 ul li{
    padding:0;    
}
#menu2 ul li a{
    display: block;
    width:150px;
    height:30px;
    margin:0;
    padding: 10px;
    text-decoration:none;
    text-align:right;
}


But we need to make it pretty now! Go to photoshop and do the same as before only make the width the same as your ul li a width + padding on each side = 170 and make the height 3px, then take your gradient from right to left and then left to right. Then add this to your CSS:

CODE

#menu2 a:link, #menu2 a:visited{
    color:#fff;
    background-image:url(images/link2.gif);
    background-repeat:repeat-y;
    background-position:center;
    border: 1px solid #f16b12; /* this border doesn't work in IE6 */
}
#menu2 a:hover, #menu2 a:active{
    color:#000;
    background-image:url(images/hover2.gif);
    background-repeat:repeat-y;
    background-position:center;
    border: 1px solid #e89642;
}


The possibilities are endless, you can play with margins, padding, color, borders-- just remember DON'T mess with the ul margins and don't cop out w/ the a's you MUST have them in this order link, visited, hover, active. Otherwise they won't work properly. Let me know how it goes!

You can see these menus at: http://www.webtestinglab.com/tutorials/
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

William_Wilson
Group Icon



post 18 Sep, 2007 - 06:07 PM
Post #2
Excellent explanation, and an example page too!

+1 for good use of the word Freaking.
Go to the top of the page
+Quote Post

Jayman
Group Icon



post 18 Sep, 2007 - 07:24 PM
Post #3
Very nicely done! Good work.
Go to the top of the page
+Quote Post

skyhawk133
Group Icon



post 18 Sep, 2007 - 07:42 PM
Post #4
What's wrong with orange?! blink.gif tongue.gif
Go to the top of the page
+Quote Post

supersssweety
Group Icon



post 19 Sep, 2007 - 06:13 AM
Post #5
awww thank you guys...

I knew I couldn't use the alternative to freakin wink2.gif

I am a fan of all colors, even the ugliest color can look good if it is complemented right, and the orange on this site is done well, I don't think any other color would do, it is unique enough to constitute a brand, I get a nice warm fuzzy feeling from it when I visit the site and when I see the color I will always associate it w/ </dream.in.code> haha but orange is just one of those colors that only fit well in certain situations, it is hard to make it work wink2.gif but works perfectly for this site
Go to the top of the page
+Quote Post

ahmad_511
Group Icon



post 19 Sep, 2007 - 12:45 PM
Post #6
Good one, you encourage me to finish my tutorial.
Regards.
Go to the top of the page
+Quote Post

smdesignworks
*



post 20 Sep, 2007 - 08:02 PM
Post #7
Essentially you could even make it better by using the "CSS Sprite" technique to put both those images on a single image file and just make the CSS move the positioning of the image down. I believe (or have read) that it makes the hover more responsive which means faster, and also instead of 2 menu images, its just one and would have a tiny bit less of a file size (I guess this is because it only needs 1 color palette instead of 2), so its even that much faster. Not to mention its so much cleaner when you have less images in your image folder, just one aspect of being extremely organized. smile.gif
Go to the top of the page
+Quote Post

supersssweety
Group Icon



post 21 Sep, 2007 - 10:37 AM
Post #8
QUOTE(smdesignworks @ 20 Sep, 2007 - 09:02 PM) *

Essentially you could even make it better by using the "CSS Sprite" technique to put both those images on a single image file and just make the CSS move the positioning of the image down. I believe (or have read) that it makes the hover more responsive which means faster, and also instead of 2 menu images, its just one and would have a tiny bit less of a file size (I guess this is because it only needs 1 color palette instead of 2), so its even that much faster. Not to mention its so much cleaner when you have less images in your image folder, just one aspect of being extremely organized. smile.gif


hrm that is a valid point, i'll have to google that one
Go to the top of the page
+Quote Post

skyhawk133
Group Icon



post 21 Sep, 2007 - 10:39 AM
Post #9
QUOTE(smdesignworks @ 20 Sep, 2007 - 10:02 PM) *

Essentially you could even make it better by using the "CSS Sprite" technique to put both those images on a single image file and just make the CSS move the positioning of the image down. I believe (or have read) that it makes the hover more responsive which means faster, and also instead of 2 menu images, its just one and would have a tiny bit less of a file size (I guess this is because it only needs 1 color palette instead of 2), so its even that much faster. Not to mention its so much cleaner when you have less images in your image folder, just one aspect of being extremely organized. smile.gif


Interesting, never thought of doing that but it makes complete sense.
Go to the top of the page
+Quote Post

supersssweety
Group Icon



post 21 Sep, 2007 - 11:09 AM
Post #10
found something...very interesting...

http://www.alistapart.com/articles/sprites
Go to the top of the page
+Quote Post

axel
Group Icon



post 21 Sep, 2007 - 12:07 PM
Post #11
Ugh I'm no good at this. Ok so here is my code. It is not looking like yours at all. crazy.gif

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="redefine.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div id="menu1">
<ul>
   <li><a href="#">Electric</a></li>
   <li><a href="#">Acoustic</a></li>
   <li><a href="#">Amps</a></li>
   <li><a href="#">Other Sites</a></li>
</ul>  
</div>


</body>
</html>



and here is the CSS

CODE


#menu1{
    height:25px;
    font-family:Verdana, Arial, Helvetica, sans-serif;
}
#menu1 ul{
    margin:0;
    padding:0;
    list-style-type:none;
}
#menu1 ul li{
    display:inline;
}
#menu1 ul li a{
    text-decoration:none;
    float:left;
    height:25;
    margin:12px 0;
    padding:15px;
    width:65px;
    text-align:center;
}

#menu1 a:link, #menu1 a:visited{
    color:#fff;
    background-image:url(../Images/menu2.jpg);
    background-repeat:repeat-x;
    background-position:center;
}
#menu1 a:hover, #menu1 a:active{
    color:#000;
    background-image: url(../Images/menu1.jpg);
    background-repeat:repeat-x;
    background-position:center;
    border: 1px solid #e89642;
}



What's going on? sad.gif
Go to the top of the page
+Quote Post

supersssweety
Group Icon



post 21 Sep, 2007 - 01:11 PM
Post #12
Is the "other sites" dropping down? This is what it does for me when I pasted your code in my testing lab...I tried positioning the background to the top for the a lvha and that helped a little.....let me keep tweaking it...it is the fact that it is on two line, I even tried to put a br in it, and that didn't work either...let me keep tweaking and see...
Go to the top of the page
+Quote Post

supersssweety
Group Icon



post 21 Sep, 2007 - 01:27 PM
Post #13
turns out the background position did work but it still looks like poo. So if you delete the width, or make it long enough so that word won't wrap you problem will be solved...also deleting the height will make the border quit being funny, but it will always come out a little bit, but there is no white space
here is the link:

http://www.webtestinglab.com/tutorials/test.html

and new code

CODE

#menu1{
    height:25px;
    font-family:Verdana, Arial, Helvetica, sans-serif;
}
#menu1 ul{
    margin:0;
    padding:0;
    list-style-type:none;
}
#menu1 ul li{
    display:inline;
}
#menu1 ul li a{
    text-decoration:none;
    float:left;
    margin:12px 0;
    padding:15px;
    text-align:center;
}

#menu1 a:link, #menu1 a:visited{
    color:#fff;
    background-image:url(images/link.gif);
    background-repeat:repeat-x;
    background-position:top;
}
#menu1 a:hover, #menu1 a:active{
    color:#000;
    background-image:url(images/hover.gif);
    background-repeat:repeat-x;
    background-position:top;
    border: 1px solid #e89642;
}
Go to the top of the page
+Quote Post

smdesignworks
*



post 21 Sep, 2007 - 03:32 PM
Post #14
QUOTE(skyhawk133 @ 21 Sep, 2007 - 11:39 AM) *


Interesting, never thought of doing that but it makes complete sense.


Thats actually the article I was referring to, you can do the technique with a ton of things, for example if you have a h2 tag where you use custom art for it instead of straight text you can line up all those h2 images, lets say you have 6 different h2 images, you can just have one art file for all those 6 images and just move the positioning with classes on those h2's, also anything with a hover the sprite technique is recommended, its just better... It may be a pain at first but stick with it and eventually it just clicks and it becomes really easy and requires less code for things like hovers. smile.gif

Hope that makes sense.
Go to the top of the page
+Quote Post

axel
Group Icon



post 21 Sep, 2007 - 06:59 PM
Post #15
Hey thanks a lot! I almost had it figured out it looks like. This is a great tutorial. smile.gif
Go to the top of the page
+Quote Post

Footsie
Group Icon



post 19 May, 2009 - 01:35 PM
Post #16
I thought I would just post a small problem I came across while working through this tutorial. Just before your first Photoshop screenshot your text says to make the graphic 25px high and 3px wide. However in your screenshot it shows the height at 50px.

I used the size as 25px high which was a big mistake because the graphic does not fill the menu box.
It took me a long time to discover why it wouldn't work.

Just make sure your graphics you create are 50px high not 25!

Thanks for a great tutorial though.
Go to the top of the page
+Quote Post

SixOfEleven
Group Icon



post 12 Jun, 2009 - 04:22 PM
Post #17
I used your tutorial for my web sites (vertical menus) and it works great!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 7/2/09 07:15PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month