CSS List Menus
At one time, most websites had simple text links. Even an experienced web designer would, at best, organize some type of simple text list or table for their menu. If they wanted something more fancy (i.e. mouseover effects or image backgrounds) the only way to accomplish it was with an external plugin, such as Java. That often slowed websites to an unusable state.
First we should define a few terms. A “list” in HTML lingo refers to the sets of tags used to list a number of items, either sequentially or not.Â A sequential list is called an “ordered list” and is represented by the
<ol> tag. A non-sequential list is called an “unordered list” and is represented by the
<ul> tag. In web browsers, the default ordered list will have Arabic numbers and look like this:
An unordered list will use bullets by default and look like this:
The first step to creating a list menu is to make the actual list in your HTML file. Create a div container for the list, which will allow you to place it where you want on the page. The
<ul></ul> tags will surround each list item
Next, add id’s to the list tags to identify them in your CSS file.
For a basic navigation list, that is all you have to do to create it. If you want to manipulate backgrounds, colors, hover actions, and more, the rest of your work will take place in the CSS file.
Find a good spot in your CSS file, or create a new one. Begin by adding the navcontainer id and assigning a width to it:
The next step will be to specify the style and attributes of the unordered list as a whole:
Rather than displaying a style of bullet or numeric character, “list-style-type: none” will remove all list-item markers and leave only the words in the list.
The next items to configure are the list items themselves. In this case, you will style any list item in the container with an “a” link tag.
The element “display: block” tells the list item to take up the full width allowed for it and to place a line break before and after each item. This will essentially make vertical buttons. The “text-decoration: none” removes the underline from the link.
Finally, specify the style of mouse-over for the buttons. In CSS the mouse-over effect is called “hover”, and an item’s style can be changed however you like, when the mouse pointer hovers over it. In this case, we will only change the background color and text color.
That is all it takes to create a vertical menu using CSS. If you would like to try out your own versions, you can download the HTML and CSS files and customize them however you wish.
With just a few lines of code, you were able to create a vertical menu with CSS. You can use a similar process to create a horizontal one. The first thing you should know is that you do not have to change the HTML code at all. You can use the exact same HTML file and just change the tags in the CSS file to morph your list into a horizontal menu.
For the horizontal list, you will apply styles to the
#navlist id rather than the
#navcontainer. Changing the list from vertical to horizontal actually only requires one small tag change from “display: block” to “display: inline”.
Notice the “white-space: nowrap” element, which prevents the list from wrapping at the end of the line when there is white space. Also notice that you can remove the “width” setting, but you are free to add a height setting, if you want to change the height of your menu.
Like the vertical menu, the next step is to specify the colors and behavior of the links and mouse hover effect.
With just those CSS tags, your vertical menu will now be a horizontal menu. You can view the HTML and CSS file for the horizontal menu and customize it to your liking.
First, create images, and make sure they are the exact size of the buttons you want to display on your page. Create one button image for the normal state, save it, and then alter that image (i.e. make it lighter, darker, or a different color) and save it under a different name for the hovered state.
In the “navcontainer a” tag, you need to add a “background-image” element that will take the place of the background color:
Next, edit the “navcontainer a:hover” tag to use the hovered state image:
Now, each button will have a background image that may look like any type of button you want, depending on your graphic design, and will also seamlessly change to another image when hovered. Be sure to keep the image file size small so that there is no delay when users hover their mouses over the buttons.
There are a multitude of other styles and techniques for making CSS list menus. Experiment, find new ways to beautify your websites, and share your discoveries.