Simple and Easy Horizontal Menu with CSS
The HTML
Start off making sure that you have the proper markup for a 1.0 strict xhtml document:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Horizontal List</title>
- </head>
- <body>
- </body>
- </html>
Next we are going to enter some divisions for the page. The first division is for what is called a "wrapper". All of the content on a page will go inside of a main division called the wrapper. The second division call "header". Assign "wrapper" and "header" using the id attribute. (This goes inside of the body)
- <body>
- <div id="wrapper">
- <div id="header">
- </div>
- </div>
- </body>
Now we are going to create an unordered list. Assign the <ul> element an id of "menu" and create three items in the list: Home, About, and Contact. Make dummy links using the "#" as the value for the href attribute.
- <body>
- <div id="wrapper">
- <div id="header">
- <ul id="main-menu">
- <li><a href="#">Home</a></li>
- <li><a href="#">About</a></li>
- <li><a href="#">Contact</a></li>
- </ul>
- </div>
- </div>
- </body>
At this point your navigation should look like this example below:
Applying the CSS
First, we are going to set what is called a global clear. Using the "*" symbol we can apply settings to all html tags. Inside of the <head> tag type the following:
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Horizontal List</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- </style>
- </head>
Now that we have cleared all margins. We can style our body background and wrapper. We want the body to be light grey and the wrapper to be horizontally centered and 600px wide.
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Horizontal List</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- body {
- background: #ccc;
- }
- #wrapper {
- width: 600px;
- margin: 0 auto;
- background: #fff;
- }
- </style>
- </head>
Now your page should look like this example below:
The next step is to add the css to the main-menu list so that it appears horizontally rather than vertically.
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Horizontal List</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- body {
- background: #ccc;
- }
- #wrapper {
- width: 600px;
- margin: 0 auto;
- background: #fff;
- }
- ul#main-menu {
- list-style: none;
- }
- ul#main-menu li {
- float: left;
- clear: none;
- }
- </style>
- </head>
Now your page should look like this:
Looks good, but in our last move we are going to give some margin and padding to the list.
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Horizontal List</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- body {
- background: #ccc;
- }
- #wrapper {
- width: 600px;
- margin: 0 auto;
- background: #fff;
- }
- ul#main-menu {
- list-style: none;
- }
- ul#main-menu li {
- float: left;
- clear: none;
- margin-right: 10px;
- padding: 5px 0 ;
- }
- </style>
- </head>
Great! There you go, a simple horizontal navigation using an unsorted list and css floats.