CSS Navigation Bar
A navigation bar is the strip of links that lets visitors move between the main sections of a site. In HTML it is simply a list of links inside a <nav> element; CSS turns that plain vertical list into a horizontal or vertical menu with hover states, spacing and a highlight for the current page.
Starting with meaningful markup
A list is the right structure because a menu is a group of related items. Screen readers announce how many links the list holds, and the <nav> element marks the region as navigation so users can jump straight to it.
<nav class="site-nav" aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/tutorials">Tutorials</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
Turning the list into a horizontal bar
Flexbox is the simplest way to lay items out in a row. Remove the default bullets and spacing, then style the links as blocks so the whole padded area is clickable, not just the text.
.site-nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
gap: 4px;
background: #1f2937;
}
.site-nav a {
display: block;
padding: 14px 18px;
color: #f9fafb;
text-decoration: none;
}
.site-nav a:hover,
.site-nav a:focus-visible {
background: #374151;
}
.site-nav a[aria-current="page"] {
background: #2563eb;
}
/* push the last item to the right edge */
.site-nav li:last-child {
margin-left: auto;
}
Older tutorials float each <li> left or set it to display: inline-block. Both still work, but floats need a clearfix and inline-block leaves small gaps from the whitespace between tags. Flexbox avoids both problems and makes alignment easy.
Vertical menus and sticky bars
For a sidebar menu, leave the list as a column (drop display: flex or use flex-direction: column) and give the <nav> a fixed width. Links stacked as blocks already fill the width, so hover backgrounds span the whole row.
To keep a top bar visible while scrolling, add position: sticky; top: 0; to the <nav>. Unlike position: fixed, sticky keeps the bar in the normal flow, so content below does not slide underneath it. Give it a z-index so other positioned elements do not cover it.
Making it work on small screens
Four short links fit on a phone, but seven or eight will not. A few options, from simplest to most involved:
- Allow wrapping with
flex-wrap: wrap, as in the example, so items flow onto a second line. - Let the list scroll sideways inside the bar with
overflow-x: auto, keeping the page itself from scrolling. - Use a media query to hide the list behind a menu button, which needs a little JavaScript or a checkbox trick to toggle.
Common mistakes
The first is styling only :hover. Keyboard users move through links with Tab, and they need a visible state too, so style :focus-visible alongside it and never remove outlines without a replacement. The second is putting padding on the <li> instead of the <a>, which leaves a dead zone around the text that does not respond to clicks. The third is marking the current page with colour alone; adding aria-current="page" tells assistive technology which link is active, and you can style that attribute directly.
Summing up the pattern
Build the menu as a <nav> containing a list of links, reset the list styles, lay items out with flexbox, put padding on the links themselves, and style hover, focus and current states. Add sticky positioning and a wrap or toggle for small screens, and you have a navigation bar that works for every visitor.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.