Skip to content

CSS Navigation Bar

Navigation bars are essential components of most websites. Modern CSS, especially Flexbox, makes creating responsive and flexible navigation bars much simpler than older methods.

Below is a conceptual representation of a horizontal navigation bar you can build with HTML and CSS. We’ll use Flexbox for alignment.

Home News Articles Forum Contact About

A navigation bar is fundamentally a list of links. The semantic HTML structure typically uses an unordered list (<ul>) containing list items (<li>), with each list item holding an anchor tag (<a>).

<nav class="navbar">
<ul class="nav-list">
<li class="nav-item"><a href="#home">Home</a></li>
<li class="nav-item"><a href="#news">News</a></li>
<li class="nav-item"><a href="#contact">Contact</a></li>
<li class="nav-item"><a href="#about">About</a></li>
</ul>
</nav>

First, let’s remove default list styling and set up the base for our navigation list.

/* Basic reset for the list */
.nav-list {
list-style-type: none; /* Remove bullets */
margin: 0;
padding: 0;
background-color: #333; /* Dark background for the navbar */
}
/* Style for individual navigation items and links */
.nav-item a {
text-decoration: none; /* Remove underline from links */
color: white; /* Link text color */
padding: 14px 20px; /* Padding around link text */
display: block; /* Make the entire area clickable, not just text */
text-align: center;
}
/* Hover effect for links */
.nav-item a:hover {
background-color: #555; /* Darker background on hover */
}

To create a horizontal navigation bar, we apply display: flex to the ul element (.nav-list).

.nav-list {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
/* --- Flexbox properties --- */
display: flex;
/* justify-content: flex-start; /* Default: aligns items to the start */
/* To center items: justify-content: center; */
/* To space items out: justify-content: space-around; or space-between; */
}
/* Individual list items no longer need special display properties for horizontal layout */
.nav-item a {
/* Styles from above (text-decoration, color, padding, display: block) */
text-decoration: none;
color: white;
padding: 14px 20px;
display: block;
}
.nav-item a:hover {
background-color: #555;
}

Flexbox Explanation:

  • display: flex; on .nav-list: This turns the ul into a flex container, and its direct children (li elements) become flex items, arranging themselves in a row by default.
  • justify-content: This property (applied to the flex container .nav-list) controls alignment along the main axis (horizontal in this case). Common values include flex-start (default), center, flex-end, space-between (first item at start, last at end, space distributed between others), space-around (equal space around each item).
  • Flexibility: Flex items can also be made to grow or shrink using flex-grow, flex-shrink, and flex-basis properties, providing more control over how items fill the available space.

Example: Right-aligning some navigation items

Section titled “Example: Right-aligning some navigation items”

Flexbox makes it easy to push some items to the right, like a ‘Login’ or ‘Search’ button.

HTML:
<ul class="nav-list">
<li class="nav-item"><a href="#home">Home</a></li>
<li class="nav-item"><a href="#news">News</a></li>
<li class="nav-item nav-item-right"><a href="#login">Login</a></li>
</ul>
CSS:
.nav-list {
display: flex;
/* ... other styles ... */
}
.nav-item-right {
margin-left: auto; /* This pushes the item to the far right */
}

For a vertical navigation bar, the basic styling for <a> elements (like display: block) is often sufficient. Flexbox can also be used with flex-direction: column; if more complex alignment or ordering is needed.

.vertical-nav-list {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px; /* Set a width for the vertical navbar */
background-color: #f1f1f1;
}
.vertical-nav-list .nav-item a {
display: block; /* Makes the whole link area clickable and allows padding */
color: #000;
padding: 8px 16px;
text-decoration: none;
}
.vertical-nav-list .nav-item a:hover {
background-color: #555;
color: white;
}
.vertical-nav-list .nav-item.active a {
background-color: #04AA6D; /* Active/current link style */
color: white;
}

Why Flexbox over Older Methods (Floats/Inline-block)?

Section titled “Why Flexbox over Older Methods (Floats/Inline-block)?”
  • Simpler Alignment: Vertical alignment of items is trivial with Flexbox (align-items).
  • Flexible Sizing: Items can grow and shrink more intelligently.
  • Ordering: The visual order of items can be changed without altering HTML source order (order property).
  • Less Hacky: Avoids issues like clearing floats or dealing with whitespace gaps of inline-block.

Tip: For fully responsive navigation, combine Flexbox with Media Queries to change layout (e.g., from horizontal to vertical or a ‘hamburger’ menu) on smaller screens.