CSS Horizontal Navbar
In this page:
ul {
list-style-type: none;
display: flex;
}
li a {
display: block;
padding: length;
}
Logo and Links on Opposite Ends
A very common horizontal navbar pattern places a logo or brand name on the left and navigation links on the right, using justify-content: space-between on a flex container so the two groups push to opposite edges automatically without manual margins.
Example: Logo and Links on Opposite Ends
<style>
nav {
display: flex;
justify-content: space-between;
background: #eee;
padding: 8px;
}
nav > div {
background: steelblue;
color: white;
padding: 6px 12px;
}
nav > div:last-child {
background: seagreen;
}
</style>
<nav>
<div>Logo</div>
<div><a href="#" style="color:white;">Home</a> <a href="#" style="color:white;">About</a></div>
</nav>
Centering Nav Links
To center navigation links in the middle of the bar rather than pushing them to one side, use justify-content: center on the flex container, or text-align: center on the list if you are using the older inline-block approach.
- Accepted values:
- center — items centered
- flex-start — items packed at the start
- flex-end — items packed at the end
- space-between — even gaps, none at the edges
- space-around — even gaps, half-size at the edges
- space-evenly — equal gaps everywhere, including the edges
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Centering Nav Links
<style>
nav {
display: flex;
justify-content: center;
gap: 8px;
background: #eee;
padding: 8px;
}
nav a {
background: steelblue;
color: white;
padding: 6px 12px;
}
</style>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
</nav>
Spacing Links Evenly
The gap property is the cleanest way to add consistent spacing between horizontal nav links inside a flex container, replacing the older pattern of adding margin-right to every link except the last one.
- Accepted values:
- gap: <length> — same gap for rows and columns, e.g. 16px
- gap: <row> <column> — two values, e.g. 8px 16px
- gap: <percentage> — relative to the container
- normal — no gap (initial value; multi-column uses 1em)
- row-gap | column-gap — set each direction on its own
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Spacing Links Evenly
<style>
nav {
display: flex;
gap: 20px;
background: #eee;
padding: 8px;
}
nav a {
background: steelblue;
color: white;
padding: 6px 12px;
}
</style>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
Underline Indicator on Active Link
A bottom-border underline is a very common way to show the current page in a horizontal navbar, often animated with a transition so the underline slides smoothly when hovering between links instead of appearing instantly.
Example: Underline Indicator on Active Link
<style>
a.active {
border-bottom: 2px solid blue;
transition: border-color 0.2s;
}
</style>
<a href="#" class="active">Home</a>
<a href="#">About</a>
What Happens When Links Run Out of Room
Without any responsive handling, a horizontal navbar with many links or a narrow viewport will either overflow the container or wrap onto a second line, both of which usually look broken. flex-wrap: wrap lets links drop to a new line gracefully as a fallback, though a dedicated mobile hamburger menu is the standard production solution.
Example: What Happens When Links Run Out of Room
<style>
nav {
display: flex;
flex-wrap: wrap;
gap: 8px;
background: #eee;
padding: 8px;
}
nav a {
background: steelblue;
color: white;
padding: 6px 12px;
}
</style>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>
- Forgetting
display: flexon the list, so items stack vertically. - Leaving the default list bullets and padding.
- Not handling small screens, so the links overflow or wrap badly.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline