Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.
A navigation bar needs standard HTML as a base.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:
<ul>
<li><a href="default.html">Home</a></li>
<li><a href="news.html">News</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="about.html">About</a></li>
</ul>Now let's remove the bullets and the margins and padding from the list:
ul {
list-style-type: none;
margin: 0;
padding: 0;
}Example explained:
list-style-type: none; - Removes the bullets. A navigation bar
does not need list markersmargin: 0; and padding: 0;
to remove browser default settingsThe code in the example above is the standard code used in both vertical, and horizontal navigation bars.
To build a vertical navigation bar, you can style the <a> elements inside the list, in addition to the code above:
li a {
display: block;
width: 60px;
}Example explained:
display: block; - Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width
(and padding, margin, height, etc. if you want)width: 60px; - Block elements take up the full width available by default. We want to specify a 60 pixels widthYou can also set the width of <ul>, and remove the width of <a>, as they will take up the full width available when displayed as block elements. This will produce the same result as our previous example:
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}