CSS Navigation Bar
CSS Navigation Bar
asp
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.
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:
Example
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
Try it Yourself »
Now let's remove the bullets and the margins and padding from the list:
Example explained:
list-style-type: none; - Removes the bullets. A navigation bar does not need list markers
Set margin: 0; and padding: 0; to remove browser default settings
The code in the example above is the standard code used in both vertical, and horizontal navigation bars.
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 width
You 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:
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}
Try it Yourself »
Home
News
Contact
About
Example
ul {
list-style-type: none;
margin: 0;
1 of 5 10/27/2019, 3:22 PM
CSS Navigation Bar https://www.w3schools.com/css/css_navbar.asp
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a:hover {
background-color: #555;
color: white;
}
Try it Yourself »
Home
News
Contact
About
Add the border property to <ul> add a border around the navbar. If you also want borders inside the navbar, add a border-bottom to all <li> elements, except for
the last one:
Home
News
Contact
About
Example
ul {
border: 1px solid #555;
}
li {
text-align: center;
border-bottom: 1px solid #555;
}
li:last-child {
border-bottom: none;
}
Try it Yourself »
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%;
position: fixed;
overflow: auto;
}
Try it Yourself »
Example explained:
display: inline; - By default, <li> elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one
2 of 5 10/27/2019, 3:22 PM
CSS Navigation Bar https://www.w3schools.com/css/css_navbar.asp
line
Another way of creating a horizontal navigation bar is to float the <li> elements, and specify a layout for the navigation links:
Example
li {
float: left;
}
a{
display: block;
padding: 8px;
background-color: #dddddd;
}
Try it Yourself »
Example explained:
float: left; - use float to get block elements to slide next to each other
display: block; - Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify padding (and
height, width, margins, etc. if you want)
padding: 8px; - Since block elements take up the full width available, they cannot float next to each other. Therefore, specify some padding to make them
look good
background-color: #dddddd; - Add a gray background-color to each a element
Tip: Add the background-color to <ul> instead of each <a> element if you want a full-width background color:
Home
News
Contact
About
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: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
Try it Yourself »
Home
News
Contact
About
Right-Align Links
Right-align links by floating the list items to the right (float:right;):
Home
News
Contact
About
Example
<ul>
3 of 5 10/27/2019, 3:22 PM
CSS Navigation Bar https://www.w3schools.com/css/css_navbar.asp
<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 Yourself »
Border Dividers
Home
News
Contact
About
Example
li {
border-right: 1px solid #bbb;
}
li:last-child {
border-right: none;
}
Try it Yourself »
Make the navigation bar stay at the top or the bottom of the page, even when the user scrolls the page:
Home
News
Contact
About
Example
ul {
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li a {
color: #666;
}
Try it Yourself »
Sticky Navbar
A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport -
then it "sticks" in place (like position:fixed).
Note: Internet Explorer, Edge 15 and earlier versions do not support sticky positioning. Safari requires a -webkit- prefix (see example above). You must also
specify at least one of top, right, bottom or left for sticky positioning to work.
More Examples
Responsive Topnav
How to use CSS media queries to create a responsive top navigation.
4 of 5 10/27/2019, 3:22 PM
CSS Navigation Bar https://www.w3schools.com/css/css_navbar.asp
Try it Yourself »
Responsive Sidenav
Try it Yourself »
Dropdown Navbar
Try it Yourself »
5 of 5 10/27/2019, 3:22 PM