
A horizontal navigation menu can be built with a semantic list of links and a small Flexbox layout. Keep the links reachable by keyboard and let them wrap when there is not enough room.
1. Structure the menu in HTML
<nav aria-label="Main navigation">
<ul class="menu">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact/">Contact</a></li>
</ul>
</nav>2. Arrange the links with CSS
.menu {
display: flex;
flex-wrap: wrap;
gap: .5rem;
padding: 0;
margin: 0;
list-style: none;
}
.menu a { display: block; padding: .65rem 1rem; }3. Add colors and interaction states
.menu a:hover, .menu a[aria-current="page"] {
background: #e8f0fa;
color: #18395d;
}
.menu a:focus-visible { outline: 2px solid #2458b5; }4. Compare traditional CSS and Flexbox
Traditional menus sometimes use floats or inline-block links. Float layouts may need clearing, and inline-block layouts may pick up whitespace gaps. Flexbox gives you direct control over gaps, wrapping, and alignment.
5. Add transitions only where helpful
A short transition on color can make the hovered state easier to follow. Keep focus visible even without animation. Test the menu at a narrow width, with text zoomed, and using Tab.
Original CodePen examples
- Open example 1
- Open example 2
- Open example 3
- Open example 4
- Open example 5
- Open example 6
- Open example 7
- Open example 8
- Open example 9