{A wonderful reference article by Roger Johansson.}
I’ve received a couple of requests for a description of how I created the navigation bar that is currently used on this site. The CSS used isn’t all that advanced, and I hadn’t really thought about describing it in detail, but after being asked about it I decided to do a write-up.
I’ve cleaned up the HTML and CSS slightly, so if you compare this to what is actually used on the site there will be some small differences. In case I have redesigned by the time you read this, check out the finished example to see what the menu looked like at the time of this writing.
The HTML
The markup is very simple. It’s an unordered list, with each link in a separate list item:
Why use a list? Because a navigation bar, or menu, is a list of links. The best (most semantic) way of marking up a list of links is to use a list element. Using a list also has the benefit of providing structure even if CSS is disabled.
At this stage, with no CSS applied, the list will look like any old (normally bulleted) list, styled only by the browser’s defaults.
I’ve given id
attributes to the ul
and li
elements. The id
attribute for the ul
element is used by the CSS rules that style the entire list. The li
elements have different id
values to enable the use of CSS to highlight the currently selected link. This is done by specifying an id
for the body
element. More on that later.
The CSS
I’ll describe the CSS I’ve used to style the list in a step-by-step fashion.
First of all, I set the margins and padding of the list and list items to zero, and tell the list items to be displayed inline:
#nav {
margin:0;
padding:0;
}
#nav li {
display:inline;
padding:0;
margin:0;
}
This will make all the links display one after another on the same line, as if the list wasn’t there. It will also remove the list bullets, since they are only displayed when display:list-item
(the default display mode for list items) is used. Some browsers are said to incorrectly display the list bullets even though display:inline
has been applied to the list items. I haven’t seen this happen in any of the browsers I tested in, but if you want to make sure that no browsers display list bullets, you can add list-style-type:none
to the rule for #nav
.
Next, it’s time to start styling the menu tabs. I do this by adding styles to the links, not to the list items. The reason for that is that I want the entire area of each tab to be clickable. First a bit of colour to make the changes more obvious:
#nav a:link,
#nav a:visited {
color:#000;
background:#b2b580;
}
Note that I’m styling the normal and visited states of the links to look the same. the next step is to add a bit of padding to the links:
#nav a:link,
#nav a:visited {
color:#000;
background:#b2b580;
padding:20px 40px 4px 10px;
}
That’s a bit better. But there is a potential problem that isn’t visible here. Since the links are inline elements, their vertical padding will not add to their line height. It’s easier to see this if the ul
element has a background, so I’ll add a background colour and a background image:
#nav {
margin:0;
padding:0;
background:#808259 url(nav_bg.jpg) 0 0 repeat-x;
}
Oops. Now the links are sticking out of the list element. To fix this, I’ve turned the links into block boxes by floating them to the left. I’ve also set their width to auto
, to make them shrink to fit their content:
#nav a:link,
#nav a:visited {
color:#000;
background:#b2b580;
padding:20px 40px 4px 10px;
float:left;
width:auto;
}
Adding display:block
to the CSS rule for the links would also have made them block boxes, but since a floated element automatically generates a block box, that isn’t necessary.
As you may have noticed, the background disappeared when the links were floated. That’s because floated elements are taken out of the document flow, which causes the ul
element containing them to have zero height. Thus, the background is there, but it isn’t visible. To make the ul
enclose the links, I’ve floated that too. I’ve also set its width to 100%, making it span the whole window (except for the padding I’ve given the body
element in this example):
#nav {
margin:0;
padding:0;
background:#808259 url(nav_bg.jpg) 0 0 repeat-x;
float:left;
width:100%;
}
To visually separate the links from each other, I’ve added a right border to the links. Then, to give the first link a left border as well, I’ve used a :first-child
pseudo-class to apply a rule only to the link in the very first list item. I’ve also added top and bottom borders to the ul
element:
#nav {
margin:0;
padding:0;
background:#808259 url(nav_bg.jpg) 0 0 repeat-x;
float:left;
width:100%;
border:1px solid #42432d;
border-width:1px 0;
}
#nav a:link,
#nav a:visited {
color:#000;
background:#b2b580;
padding:20px 40px 4px 10px;
float:left;
width:auto;
border-right:1px solid #42432d;
}
#nav li:first-child a {
border-left:1px solid #42432d;
}
The :first-child
pseudo-class is not recognised by Internet Explorer for Windows, so the first link won’t have a left border in that browser. In this case, that isn’t a major problem, so I’ve left it like that. If it’s really important to you, you’ll need to add a class to the first list item (or the link in it), and then use that to give the link a left border.
Next I’ve changed the way the link text is displayed by removing the underlining, making the text bold, specifying font size, line-height, and a different font family, making the text uppercase, and adding a little bit of drop shadow. The drop shadow is created with the text-shadow
property, a CSS3 property that is currently only supported by Safari and OmniWeb:
#nav a:link,
#nav a:visited {
color:#000;
background:#b2b580;
padding:20px 40px 4px 10px;
float:left;
width:auto;
border-right:1px solid #42432d;
text-decoration:none;
font:bold 1em/1em Arial, Helvetica, sans-serif;
text-transform:uppercase;
text-shadow: 2px 2px 2px #555;
}
To give some visual feedback when the links are hovered over, I’ve given their :hover
state different text and background colours:
#nav a:hover {
color:#fff;
background:#727454;
}
In the final step, I’ve added rules that will make the selected link look different than the others, to show visitors where they are on the site.
In case you haven’t seen an example of specifying an id
attribute for the body
element to style the “current” navigation tab differently before, that’s what the first two rules do. In the examples linked to from this article, I’ve set id
of the body
element to “home
”, which makes the “Home” tab the current one. Changing it to “about
” would make the “About” tab the current one, and so on.
I’ve also made the selected link stay the same when it’s hovered over. It can be argued that the current menu item should not be a link at all. In this case, I’ve chosen to leave the link in the markup and use CSS to remove the visual feedback on hover.
To give some visual feedback when you click on one of the links, I’ve given the :active
state of the links the same styling as the selected link:
#home #nav-home a,
#about #nav-about a,
#archive #nav-archive a,
#lab #nav-lab a,
#reviews #nav-reviews a,
#contact #nav-contact a {
background:#e35a00;
color:#fff;
text-shadow:none;
}
#home #nav-home a:hover,
#about #nav-about a:hover,
#archive #nav-archive a:hover,
#lab #nav-lab a:hover,
#reviews #nav-reviews a:hover,
#contact #nav-contact a:hover {
background:#e35a00;
}
#nav a:active {
background:#e35a00;
color:#fff;
}
View Step 11, the finished navigation menu.
That’s it. This step-by-step tutorial makes the whole thing look more advanced than it really is. View source on the final example to see the complete set of CSS rules. By the way, with a couple of small exceptions (the left border on the first link, and the text shadow), this works in just about any browser, even Internet Explorer (version 5 or newer).
I hope you’ve been able to follow along well enough to be able to create your own navigation menu. The styling possibilities are almost endless.
Roger Johansson
No comments:
Post a Comment