TExcelTAD
Home › CSS › CSS Navigation Bar

CSS Navigation Bar

Nearly every website has a navigation bar. Semantically it's just a list of links inside a <nav> element — CSS turns that list into a polished horizontal or vertical menu.

The HTML

html
<nav>
  <ul>
    <li><a href="#" class="active">Home</a></li>
    <li><a href="#">Products</a></li>
    <li><a href="#">About</a></li>
  </ul>
</nav>

The first step is always the same: remove the list's bullets, padding and margin.

css
nav ul { list-style: none; margin: 0; padding: 0; }

Vertical navigation

Make each link display: block so the whole row is clickable, not just the text. Then add padding and hover/active states.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; display: flex; }
  nav { width: 180px; background: #f5f5f5; height: 100vh; border-right: 1px solid #ddd; }
  nav ul { list-style: none; margin: 0; padding: 0; }
  nav a { display: block; padding: 12px 16px; color: #333; text-decoration: none; border-left: 4px solid transparent; }
  nav a:hover { background: #e0e0e0; }
  nav a.active { background: #e8f5e9; border-left-color: #43a047; color: #1b5e20; font-weight: 600; }
  main { padding: 16px; }
</style>
</head>
<body>
  <nav>
    <ul>
      <li><a href="#" class="active">Dashboard</a></li>
      <li><a href="#">Projects</a></li>
      <li><a href="#">Team</a></li>
      <li><a href="#">Settings</a></li>
    </ul>
  </nav>
  <main>Page content</main>
</body>
</html>

Tip: Mark the current page with a class like .active (or the aria-current="page" attribute and the selector a[aria-current="page"]) so users know where they are.

Horizontal navigation

The modern way is Flexbox on the <ul>. The older ways — display: inline-block on the <li>s or float: left — still work, but Flexbox handles alignment and spacing more easily.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  nav { background: #1e1e2f; }
  nav ul { list-style: none; margin: 0; padding: 0; display: flex; }
  nav a { display: block; padding: 16px 18px; color: #cfd2ff; text-decoration: none; }
  nav a:hover { background: #2d2d48; color: white; }
  nav a.active { background: #5c6bc0; color: white; }
  nav .right { margin-left: auto; }
</style>
</head>
<body>
  <nav>
    <ul>
      <li><a href="#" class="active">Home</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact</a></li>
      <li class="right"><a href="#">Log in</a></li>
    </ul>
  </nav>
</body>
</html>

margin-left: auto on the last item pushes it to the far right.

Variations

Effect How
Dividers between items li + li { border-left: 1px solid … }
Centered links justify-content: center on the ul
Underline indicator border-bottom on a.active or an animated ::after
Pill links border-radius: 999px on the links, gap on the list
Fixed / sticky top bar position: sticky; top: 0; on nav
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  nav { position: sticky; top: 0; background: white; box-shadow: 0 2px 6px rgba(0,0,0,.1); }
  nav ul { list-style: none; margin: 0; padding: 8px; display: flex; justify-content: center; gap: 6px; }
  nav a { display: block; padding: 8px 16px; border-radius: 999px; color: #555; text-decoration: none; position: relative; }
  nav a:hover { background: #fce4ec; color: #ad1457; }
  nav a.active { background: #ad1457; color: white; }
  .page { height: 600px; padding: 16px; background: linear-gradient(#fafafa, #f8bbd0); }
</style>
</head>
<body>
  <nav>
    <ul>
      <li><a href="#" class="active">Home</a></li>
      <li><a href="#">Menu</a></li>
      <li><a href="#">Book</a></li>
    </ul>
  </nav>
  <div class="page">Scroll — the pill nav is sticky.</div>
</body>
</html>

Responsive navigation

On narrow screens a horizontal bar may not fit. A simple approach: let items wrap with flex-wrap: wrap, or switch to a vertical stack with a media query.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  nav ul { list-style: none; margin: 0; padding: 0; display: flex; background: #00695c; }
  nav a { display: block; padding: 14px 16px; color: white; text-decoration: none; }
  nav a:hover { background: #004d40; }
  @media (max-width: 450px) {
    nav ul { flex-direction: column; }
    nav a { text-align: center; border-top: 1px solid #00897b; }
  }
</style>
</head>
<body>
  <nav><ul><li><a href="#">Home</a></li><li><a href="#">Services</a></li><li><a href="#">Pricing</a></li><li><a href="#">Contact</a></li></ul></nav>
  <p style="padding: 0 12px">Drag the playground narrower than 450px — the menu stacks vertically.</p>
</body>
</html>

Test yourself

Why set display: block on navigation links?

  1. To remove the underline
  2. So the whole padded area is clickable
  3. To make them bold
  4. To hide bullets

Which CSS removes default list bullets and indentation?

  1. list-style: none; margin: 0; padding: 0;
  2. display: none;
  3. bullets: off;
  4. text-indent: 0;

In a flex navbar, what pushes the last item to the far right?

  1. float: right on the ul
  2. margin-left: auto on that item
  3. text-align: right
  4. position: right