Flag This Hub

CSS Tutorial - css menu

By


In this css tutorial i'm going to show you how to make a menu using css. Your css menu can include image rollovers or just text.

We'll be using the same layout we used in the previous tutorial: CSS Tutorial - using float to layout your website.

First we'll look at using text for our menu items. The menu will appear as a horizontal menu.

You will need to first declare your ul in your style sheet and attach a class to that ul. We will add a width and a float to our menu class. Then we will put write our style for the li as below:

ul.menu{
float: left;
height: 30px;
width: 900px;	
}

ul.menu li{
display: inline;
width: auto;
padding: 6px 8px;
margin-left: 20px;	
}

Notice in our li how we have set the style 'display: inline' this causes your li's to run horizontally. We've also added some margin to the left of the li to space out your menu items and added some padding around the links.

Below is what you html should look like:

<ul class="menu">
   
   	<li><a href="link.htm">Item 1</a></li>
        <li><a href="link2.htm">Item 2</a></li>
        <li><a href="link3.htm">Item 3</a></li>
        <li><a href="link4.htm">Item 4</a></li>   
   
</ul>
See all 3 photos

Above is how our menu is looking at the moment - not looking great but it's a start.

Next we'll add some formatting to the links to improve their looks.

Below is the css for the links:

ul.menu li a{
color: black;
font-family: Verdana, Geneva, sans-serif;
font-weight: bold;
text-decoration: none;	
}

ul.menu li a:hover{
color: white;
}

Now we've added some colour to our menu items with a white rollover. Obviously in terms of style you can do a lot better but you should now understand how to create a horizontal menu with rollvers.

Next I will show you how to use images with a rollover for your menu items.

First you need to start off with an image which shows your upstate of your menu item and your down state. These 2 images need to be within the same image, like below:

Next we'll add that image into our css as a background image on the a:link. However we won't display the whole image because we only want the top half. The image is 60px in height so we only want to show the top 30px, then one the button is rolled over we want to show the bottom 30px. Here's how we do that with the css:

ul.menu li{
display: inline;
width: auto;
margin-left: 20px;
}

ul.menu li a{
color: black;
font-family: Verdana, Geneva, sans-serif;
font-weight: bold;
text-decoration: none;
background-image: url(btn.jpg);
background-repeat:repeat-x;
height: 24px;
width: auto;
padding: 6px 8px;
}

ul.menu li a:hover{
color: white;
background-position: 0 -30px;
}

There are a few things to explain here. Firstly, in the li we have taken out background-color because we now have an image in the background on our link.

Our link has the background image on it and we've said background-repeat: repeat-x this is because our background image is only 12px wide, so we just repeat it to fill up the area. We've added a height of 24px and made the width auto to accommodate the length of the text which is different for each button. We've add a padding to the top of the link to push down the text into the middle of the button and because we've done this we've had to change the height from 30px to 24px because padding-top: 6px increases the size of the link to 36px so we've had to make it 24px to accommodate this.

Lastly in our hover we've put background-position: 0 -30px the first value '0' is to position the background image horizontally and the second value '-30px' positions the background image vertically. So in effect the background image moves up 30px.

Below is what your menu should now look like. The html is unchanged from above.

And that's how to make a simple css menu. If you use a font for your menu items that is not websafe you can always create your buttons as an image with text on and then delete the text in the li, so your background image is the button with text as part of the image. Then add a class to each link and in the css create your classes, then just change the background image for each class to show the correct button.

I hope you have found this tutorial useful. Click the link if you need a website design.

  • Ebook reader vs paper books

    Today ebook readers are becoming more and more popular but it remains to be seen as to whether they are the preferred medium for reading. Below I will argue for both....Let the debate begin! The... - 11 months ago

  • CSS Tutorial - Learn CSS - CSS3 Drop Shadow

    With CSS3 now in it's 4th year, it's becoming more and more common and compatible with upgrades to the major browsers. In this article i'm going to give you a tutorial on how to implement drop... - 12 months ago

  • Honeymoon - Phuket

    The next best thing after getting married is going away to a world that's perfect in every way! This is what my partner and I did. We went to Thailand for our honeymoon in Phuket, somewhere we had... - 13 months ago

  • Mobile Websites

    These days, it's becoming more and more popular for people to browse the internet from their phone and as soon as a site takes to long to load they give up. This is why businesses are creating mobile... - 13 months ago

Below are some great resources for css menus

CSS: The Missing Manual
Amazon Price: $18.00
List Price: $34.99
Learn CSS with w3Schools
Amazon Price: $9.49
List Price: $29.99
HTML and CSS: Design and Build Websites
Amazon Price: $15.08
List Price: $29.99
Sams Teach Yourself HTML and CSS in 24 Hours (Includes New HTML 5 Coverage) (8th Edition)
Amazon Price: $18.00
List Price: $34.99
Learn HTML and CSS
Amazon Price: $9.00

Comments

apStumbo 13 months ago

Wow, CSS is sweet. I am very new to it though. I have done plenty of html, do I go about it the same? Do I just use textedit and then save it as a .html? or what do I do?

springz 13 months ago

You start with your blank document (either in dreamweaver or some other text editory) then save the document as yourstylesheet.css (make sure the extension is .css) Write your css in that document, then in your html document include the stylesheet like this -

This goes between in your html document.

apStumbo 13 months ago

Oh awesome. Thanks so much. That is exciting. So CSS is almost an extension to html. Like something that you can do some cooler stuff with on top of html?

mr-burns 8 months ago

Some great basics to use. Thanks

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working