CSS Navigation Bar (Horizontal Menus)
Horizontal Navigation Bar
There are two ways to create a horizontal navigation bar. Using inline or floating list items.
Inline List Items
One way to build a horizontal navigation bar is to specify the <li> elements as inline-block.
Example
display: inline-block;
}
Example explained:
display: inline-block;
- By default, <li> elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one line
Floating List Items
Another way of creating a horizontal navigation bar is to float the <li> elements, and specify a layout for the navigation links:
Example
float: left;
}
a {
display: block;
padding: 8px;
background-color: #dddddd;
}
Example explained:
float: left;
- Use float to get block elements to float next to each otherdisplay: block;
- Allows us to specify padding (and height, width, margins, etc. if you want)padding: 8px;
- Specify some padding between each <a> element, to make them look goodbackground-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:
Example
background-color: #dddddd;
}
Horizontal Navigation Bar Examples
Create a basic horizontal navigation bar with a dark background color and change the background color of the links when the user moves the mouse over them:
Example
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;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
Active/Current Navigation Link
Add an "active" class to the current link to let the user know which page he/she is on:
Example
background-color: #0054ff;
}
Right-Align Links
Right-align links by floating the list items to the right (float:right;
):
Example
<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>
Border Dividers
Add the border-right
property to <li> to create link dividers:
Example
li {
border-right: 1px solid #bbb;
}
li:last-child {
border-right: none;
}
Fixed Navigation Bar
Make the navigation bar stay at the top or the bottom of the page, even when the user scrolls the page:
Fixed Top Navigation Bar
Scroll this page to see the effect
The navigation bar will stay at the top of the page while scrolling
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Fixed Top
position: fixed;
top: 0;
width: 100%;
}
Fixed Bottom Navigation Bar
Scroll this page to see the effect
The navigation bar will stay at the bottom of the page while scrolling
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Some text some text some text some text..
Fixed Bottom
position: fixed;
bottom: 0;
width: 100%;
}
Note: Fixed position might not work properly on mobile devices.
Gray Horizontal Navbar
An example of a gray horizontal navigation bar with a thin gray border:
Example
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li a {
color: #666;
}
Full Code
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li {
float: left;
}
li a {
display: block;
color: #666;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #ddd;
}
li a.active {
color: white;
background-color: #0054ff;
}
</style>
</head>
<body>
<ul>
<li><a class="active" 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>
</body>
</html>
Sticky Navbar
Add position: sticky;
to <ul> to create a 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).
Scroll Down
Scroll down to see the sticky effect.
Sticky Navigation Bar Example
The navbar will stick to the top when you reach its scroll position.
Note: Internet Explorer do not support sticky positioning and Safari requires a -webkit- prefix.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Example
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
}
Note: Internet Explorer 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.
Comments
Post a Comment