CSS Navigation
- November 8th, 2009
- Write comment
In this tutorial I am going to show the basics of using CSS to create easily manipulated navigation. In a further post to be created later I will demonstrate drop down, pop out functionality using CSS. Before we try to implement the advanced drop down, pop out functionality, I need to show you the basics.
The XHTML
There are many ways to layout your navigation with XHTML, but I prefer the simple approach, utilising the ul and li tags. An example of this type of navigation can be found below. I have not placed any CSS within the first step so we can step through and view the changes.
<ul>
<li><a href=”#”>Link</a></li>
<li><a href=”#”>Link</a></li>
<li><a href=”#”>Link</a></li>
<li><a href=”#”>Link</a></li>
</ul>
If we display this code in a browser you will see four links listed with bullet points vertically.
Adding the CSS
CSS needs to be able to associate it’s styles with your XHTML code. To do this we use a selector, this is the element you are wanting to add styles to. In this tutorial you are going to be introduced to three of the available selectors. The attribute selector, the ID selector and the class selector.
The Attribute Selector
The attribute selector is the simplest form of selection available within CSS, for example if we wanted to select and transform the colour of the text within every occurrence of the li tag to black in a given document. We would simply place the following code within our CSS:
li {
color:#000;
}
This is relatively simple and the colour shown here is in Hex format, hex is a six digit representation for colour, in this case being as each second character is the same as the first we can use a short hand version of the hex for example:
#000000 black can be shortened to #000
#FFFFFF white can be shortened to #FFF
The attribute selector is the very basics of CSS.
The ID Selector
If we take our example unordered list above we are going to utilise an ID selector here by simply adding the following to the ul element:
<ul id=”nav”>
We have opened up the ul element to be uniquely identified within our style-sheet and document. The ID selector should be used sparingly within a document. To demonstrate using the ID selector within your CSS code I have set a height of 56px like so:
#nav {
height:56px;
}
As you can see from above the ID selector is accessed using the # symbol.
The Class Selector
this selector is designed for reuse for instance in our code we have an li tag that has a class of last defined like so:
<li class=”last”><a href=”#”>Link</a></li>
we can now declare the rules for this class in our css like so:
#nav li.last {
border-right:solid #000 1px;
}
you can see that I am combining selectors here an ID selector with a Class selector, this is not required but I have done this to demonstrate two points, the first being that of Specificity, this is the internal scoring system used within CSS to determine which rules to use for a given element. for more on Specificity look at the W3C’s candidate recommendation 6.4.3 Calculating a selector’s specificity. and the second being that you can use the parent child relationship to increase specificity and isolate elements you wish to have unique styles. In our example any li with a class of last will only be affected if they have an ancestor with with an id of nav.
Styling the Example
We need to make our list items line up so in line, at this point most people would use the display:inline attribute as this will take the list items and line them up horizontally along the page. My problem with this method is that there is an inherent margin which means we cant get them lined up next to each other. To remedy this I use float left, this has the same effect as display:inline but without the margin. as you can see from the following navigation example.
Now we have taken away the gap, placed a grey background on the links, also we have a border and made the font white.
Developer Tools
great way to view CSS and other style related features is through two Firefox plug-ins I would highly recommend, the first is the Web Developer tool, and the second is Firebug. If you do not have these add-on’s then you can get the CSS from the file here by viewing the source navigation example.

