CSS Navigation Bar
Vertical
Horizontal
navigation bar
Skilled use of the navigation bar is very important for any website.
With CSS you can transform it into a good-looking navigation bar instead of a boring HTML menu.
Navigation bar = list of links
As a standard HTML foundation, a navigation bar is necessary.
In our example we will build a standard HTML list navigation bar.
A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:
Example
Try it Yourself »
Now, let's remove margins and padding from the list:
Example analysis:
- list-style-type:none - Removes the markers before list items. A navigation bar does not need list markers.
- Remove the browser's default settings and set margin and padding to 0.
The code in the above example is the standard code used for both vertical and horizontal navigation bars.
Vertical navigation bar
With the above code, we only need to style the <a> element to build a vertical navigation bar:
Example explanation:
- display:block - Displays the link as a block element, making the whole area a clickable link area (not just text), and it allows us to specify a width.
- width:60px - By default, block elements take up the maximum width. We want to specify a width of 60 pixels.
Tip:ViewExample of a fully styled vertical navigation bar。
Note:Be sure to specify the width of the <a> element in a vertical navigation bar. If the width is omitted, IE6 may produce unexpected results.
Vertical Navigation Bar Example
Create a simple vertical navigation bar example, and change the background color when the mouse moves over an option:
Example
Try it Yourself »
Active/current navigation bar example
After an option is clicked, we can add the "active" class to indicate which option is selected:
Create links and add borders
Can be added on <li> or <a>text-align:centerStyle to center the links.
You canborderAdd to <ul>borderUse the property to give the navigation bar a border. If you want to add a border to each option, you can add it on each <li> element.border-bottom :
Example
Try it Yourself »
Fixed navigation bar with full-screen height
Next we create a fixed navigation bar of full screen height on the left, with scrollable content on the right.
Example
Try it Yourself »
Note:This example can be used on mobile devices.
Horizontal navigation bar
There are two ways to create a horizontal navigation bar. UseInlineorFloatlist items.
Both methods are good, but if you want the links to have the same size, you must use the floating method.
Inline list items
One way to build a horizontal navigation bar is to specify the elements as inline; the following code is the standard inline:
Example Analysis:
- display:inline; - by default,<li>The <li> element is a block element. Here, we remove the line breaks before and after each list item to display them in one line.
Tip:ViewExample of a fully styled horizontal navigation bar。
Floating list items
In the above example, the links have different widths.
For all links to have equal width, float the <li> elements and specify the width of the <a> elements:
Example Analysis:
- float:left - Use float to make block elements slide next to each other.
- display:block - Displays the link as a block element, making the whole area a clickable link area (not just text), and it allows us to specify a width.
- width:60px - By default, block elements take up the maximum width. We want to specify a width of 60 pixels.
Tip:ViewExample of a fully styled horizontal navigation bar。
Horizontal Navigation Bar Example
Create a horizontal navigation bar and change the background color when the mouse moves over an option.
Example
Try it Yourself »
Active/current navigation bar example
After an option is clicked, we can add the "active" class to indicate which option is selected:
Right-aligned links
Set the rightmost option of the navigation bar to right alignment (float:right;):
Example
Try it Yourself »
Add dividers
via <li>border-rightStyle to add dividing lines:
Example
Try it Yourself »
Fixed navigation bar
You can set the page's navigation bar to be fixed at the top or bottom:
Note:This example can be used on mobile devices.
Gray Horizontal Navigation Bar
Gray Horizontal Navigation Bar
ul {
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li a {
color: #666;
}
Try it Yourself »
More Examples
Responsive top navigation - How to use CSS3 media queries to create a responsive navigation.
Responsive sidebar navigation - How to use CSS3 media queries to create a sidebar navigation.
Navigation dropdown menu - Set up a dropdown menu inside the navigation bar.
other extensions
Gray Horizontal Navigation Bar
Try it Yourself »
Responsive top navigation - How to use CSS3 media queries to create a responsive navigation.
Responsive sidebar navigation - How to use CSS3 media queries to create a sidebar navigation.
Navigation dropdown menu - Set up a dropdown menu inside the navigation bar.