Skip to main content

CSS Horizontal Navigation Bar

A horizontal navigation bar places its links side by side, and usually sits at the top of a webpage.

A <nav> element is often wrapped around the <ul> to serve as the bar's container.


Horizontal Navbar Using Float

Setting float on the <li> elements is one route to building a horizontal navigation bar.

Example

Prompt
Create a horizontal navigation bar using float on the li elements. The ul has no list-style, no margin/padding, overflow hidden, and a dark background (#333333). The li elements float left. Links are block, white, centered, 14px 16px padding, no underline. Hover changes background to #111111. Include Home, News, Contact, About links.
Output: complete HTML page

Horizontal Navbar Using Flex

CSS flexbox offers a more modern route: just put display: flex on the <ul>.

Flex navbar

Prompt
Create a horizontal navigation bar using CSS flexbox (display: flex on ul). The ul has no list-style, no margin/padding, dark background (#333333). Links are block, white, 14px 16px padding, no underline. Hover changes background to #111111. Include Home, News, Contact, About links.
Output: complete HTML page

Centered flex navbar

To move the navbar links to the center, add justify-content: center to the bar.

Prompt
Create a horizontally centered navigation bar using CSS flexbox. Same as the flex navbar but add justify-content: center to the ul to center the links.
Output: complete HTML page

Active State

Users can see which page they are on when its link is highlighted through an "active" class.

Example

Prompt
Create a horizontal navigation bar with an active state. Use the dark background horizontal navbar and add an "active" class to the Home link with background-color #04AA6D.
Output: complete HTML page

Gray Horizontal Navbar

Here the bar is gray, with a thin gray border around it.

Example

Prompt
Create a gray horizontal navigation bar using flexbox. The ul has a light gray background (#f3f3f3), a 1px solid #e7e7e7 border, no list-style, no margin/padding. Links are dark text with padding. Include Home, News, Contact, About links.
Output: complete HTML page

Right-align Last Link

Any link, or several, can sit on the right side of the bar: give its list item float: right.

Example

Prompt
Create a horizontal navigation bar where the last link (About) is right-aligned using float: right on that li element. The rest of the links are left-aligned. Use the dark background navbar style.
Output: complete HTML page

Border Dividers

Dividers between links come from a border-right on each <li> element other than the last.

Example

Prompt
Create a horizontal navigation bar with border dividers between links. Add border-right: 1px solid #bbbbbb to all li elements except the last one (using li:last-child). Use the dark background navbar style with float.
Output: complete HTML page

Fixed Navigation Bar

To pin the navigation bar to the top or bottom of the page, combine the position property with top or bottom.

Fixed Top

Prompt
Create a horizontal navigation bar fixed to the top of the page using position: fixed, top: 0, and width: 100%. Add enough body content to demonstrate scrolling while the navbar stays fixed. Add margin-top to the body content so it's not hidden behind the navbar.
Output: complete HTML page

Fixed Bottom

Prompt
Create a horizontal navigation bar fixed to the bottom of the page using position: fixed, bottom: 0, and width: 100%. Add enough body content to demonstrate scrolling while the navbar stays at the bottom.
Output: complete HTML page

Sticky Navigation Bar

A sticky navbar needs position: sticky on the <ul>. Depending on the scroll position, such an element switches between relative and fixed positioning.

Note: Sticky positioning only works once at least one of top, right, bottom, or left is specified.

Example

Prompt
Create a sticky horizontal navigation bar using position: sticky and top: 0. Add content before and after the navbar to demonstrate that it becomes fixed when scrolling past it.
Output: complete HTML page

Dropdown Menu in Navigation Bar

Hovering over a menu item can reveal a dropdown menu placed inside the navigation bar.

Example

Prompt
Create a horizontal navigation bar with a dropdown menu. One of the menu items ("Dropdown") reveals a dropdown list of links on hover. Use a div inside the li for the dropdown content, hidden by default and shown on hover with position: absolute.
Output: complete HTML page

Responsive Navbar

With CSS media queries, a responsive top navigation can switch from horizontal to vertical when the screen gets smaller.

Example

Prompt
Create a responsive horizontal navigation bar that collapses to vertical on small screens using CSS media queries. On wide screens the links display horizontally; on narrow screens they stack vertically.
Output: complete HTML page