CSS Navigation Bar
Vertical
- Home
- News
- Contact
- About
Horizontal
-
Home
-
News
-
Contact
-
About
-
Home
-
News
-
Contact
-
About
Navigation bar
Being proficient in using navigation bars is very important for any website.
Using CSS you can turn boring HTML menus into good-looking navigation bars.
Navigation bar = list of links
As a standard HTML foundation, a navigation bar is essential.
In our example we will build a standard HTML list navigation bar.
A navigation bar is basically a list of links, so using
-
and
- elements makes a lot of sense:
Example
<ul> <li> <a href="#home" Home a li> <li> <a href="#news" News a li> <li> <a href="#contact" Contact a li> <li> <a href="#about" About a li ul>Try it »
Now, let's remove the margins and padding from the list:
Example
ul
{
list-style-type:
none
;
margin:
0
;
padding:
0
;
}
Try it »
Example explanation:
- list-style-type:none - removes the small markers before the list. A navigation bar does not need list markers
- Remove the browser's default settings by setting margins and padding to 0
The code in the example above is the standard code used for both vertical and horizontal navigation bars.
Vertical Navbar
With the code above, we only need to style the element to build a vertical navigation bar:
Example
a
{
display:
block
;
width:
60
px
;
}
Try it »
Example explanation:
- display:block - displays the links as block elements, making the whole area clickable (not just the text), and allowing us to specify a width
- width:60px - block elements take the maximum width by default. We want to specify a width of 60 pixels
**Tip:** See Example of a fully styled vertical navigation bar。
Note: Be sure to specify the width of the 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 that changes the background color when the mouse moves over an option:
- Home
- News
- Contact
- About
Example
ul
{
list-style-type:
none
;
margin:
0
;
padding:
0
;
width:
200
px
;
background-color:
#f1f1f1
;
}
li
a
{
display:
block
;
color:
#000
;
padding:
8
px
16
px
;
text-decoration:
none
;
}
/*
Change the background color when the mouse moves over an option
*/
li
a
:hover
{
background-color:
#555
;
color:
white
;
}
Try it »
Active/current navigation bar example
After clicking an option, we can add the "active" class to mark which option is selected:
- Home
- News
- Contact
- About
Example
li
a
.active
{
background-color:
#4CAF50
;
color:
white
;
}
Try it »
Create links and add borders
can be done during
- or on the element, addtext-align:center style to center the links.
can be done during border
-
on the element, add border property to give the navigation bar a border. If you want to add a border to each option, you can add it to each
- element, add theborder-bottom :
Example
ul
{
border:
1
px
solid
#555
;
}
li
{
text-align:
center
;
border-bottom:
1
px
solid
#555
;
}
li
:last-child
{
border-bottom:
none
;
}
Try it »
Full-height fixed navigation bar
Next we create a fixed navigation bar on the left with full screen height, and scrollable content on the right.
Example
ul
{
list-style-type:
none
;
margin:
0
;
padding:
0
;
width:
25
%
;
background-color:
#f1f1f1
;
height:
100
%
;
/*
Full screen height
*/
position:
fixed
;
overflow:
auto
;
/*
Allow scrolling if there are many navigation bar options
*/
}
Try it »
Note: This example can be used on mobile devices.
Horizontal navigation bar
There are two ways to create a horizontal navigation bar. Using **inlineoror float** list items.
Both methods are good, but if you want the links to have the same size, you must use the float method.
Inline list items
One way to build a horizontal navigation bar is to specify the element; the following code is the standard inline:
Example
li
{
display:
inline
;
}
Try it »
Example analysis:
- display:inline; - by default,
- elements are block elements. Here, we remove the line breaks before and after each list item to display them in a single line.
Tip: Check Example of a fully styled horizontal navigation bar。
Float list items
In the example above the links have different widths.
For all links to have equal width, float the
- elements and specify them as The width of an element:
Example
li
{
float:
left
;
}
a
{
display:
block
;
width:
60
px
;
}
Try it »
Example analysis:
- float:left - use floating block elements so they slide up next to each other
- display:block - displays the links as block elements, making the whole area clickable (not just the text), and allowing us to specify a width
- width:60px - block elements take the maximum width by default. We want to specify a width of 60 pixels
**Tip:** See example of a fully styled horizontal navigation bar。
Horizontal navigation bar example
Create a horizontal navigation bar that changes the background color when the mouse moves over an option.
Example
ul
{
list-style-type:
none
;
margin:
0
;
padding:
0
;
overflow:
hidden
;
background-color:
#333
;
}
li
{
float:
left
;
}
li
a
{
display:
block
;
color:
white
;
text-align:
center
;
padding:
14
px
16
px
;
text-decoration:
none
;
}
/*
Change the background color when the mouse moves over an option
*/
li
a
:hover
{
background-color:
#111
;
}
Try it »
Active/current navigation bar example
After clicking an option, we can add the "active" class to indicate which option is selected:
Example
.active
{
background-color:
#4CAF50
;
}
Try it »
Right-align links
Set the rightmost option of the navigation bar to right alignment (float:right;):
Example
<ul> <li> <a href="#home" Home a li> <li> <a href="#news" News a li> <li> <a href="#contact" Contact a li> <li style="float:right"> <a class="active" href="#about" About a li ul>Try it »
Add separator lines
- Add separator lines with the **border-right** style:
Example
/*
Add separator lines to all options except the last one (last-child)
*/
li
{
border-right:
1
px
solid
#bbb
;
}
li
:last-child
{
border-right:
none
;
}
Try it »
Fixed navigation bar
You can set the page's navigation bar to be fixed at the top or bottom:
Fixed at the top
ul
{
position:
fixed
;
top:
0
;
width:
100
%
;
}
Try it »
Fixed at the bottom
ul
{
position:
fixed
;
bottom:
0
;
width:
100
%
;
}
Try it »
Note: This example can be used on mobile devices.
Gray horizontal navigation bar
Gray horizontal navigation bar
ul
{
border:
1
px
solid
#e7e7e7
;
background-color:
#f3f3f3
;
}
li
a
{
color:
#666
;
}
Try it »
More examples
- element, add theborder-bottom :