Skip to content

Responsive Navigation Bar

Easy ~20 min

What you'll build

An HTML page with a navigation bar containing a logo on the left and 4 links on the right, using flexbox. On mobile, links stack vertically below the logo.

Goals

  • Use the semantic <nav> element for navigation
  • Apply flexbox to align the logo and links horizontally
  • Add a media query for mobile vertical layout
  • Style links with hover states for interactivity

Prerequisites

  • HTML structure and semantic tags
  • Basic CSS including flexbox

Steps

  1. 1

    Write the HTML structure

    Create nav.html with a nav element containing a logo (as text) and a ul of 4 links.

    Code
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Navigation</title>
        <link rel="stylesheet" href="nav.css">
      </head>
      <body>
        <nav>
          <div class="logo">MySite</div>
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
      </body>
    </html>

    Expected result

    A page with a nav containing a logo and a list of links with default styling.

  2. 2

    Style the nav with flexbox

    Create nav.css. Use flexbox on the nav to push the logo left and links right with justify-content: space-between.

    Code
    nav {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 1rem 2rem;
      background: #1e293b;
    }
    .logo {
      font-size: 1.5rem;
      font-weight: bold;
      color: #fff;
    }

    Expected result

    The logo appears on the left and the list on the right, vertically centered.

  3. 3

    Style the links

    Remove list styling and make the links horizontal with flexbox. Add hover states.

    Code
    nav ul {
      display: flex;
      gap: 1.5rem;
      list-style: none;
      margin: 0;
      padding: 0;
    }
    nav a {
      color: #cbd5e1;
      text-decoration: none;
      transition: color 0.2s;
    }
    nav a:hover {
      color: #fff;
    }

    Expected result

    Links are horizontal with no bullets, and turn white on hover.

  4. 4

    Add mobile responsive layout

    Add a media query for max-width: 600px to stack the nav vertically on mobile.

    Code
    @media (max-width: 600px) {
      nav {
        flex-direction: column;
        gap: 1rem;
      }
      nav ul {
        flex-direction: column;
        gap: 0.5rem;
        align-items: center;
      }
    }

    Expected result

    On mobile, the logo and links stack vertically and center.

  5. 5

    Add responsive styles

    Use media queries to adjust the layout for mobile: switch multi-column layouts to single column on small screens, reduce font sizes, and adjust spacing. Mobile-first means small-screen experience matters equally.

    Code
    @media (max-width: 640px) {
      .container {
        flex-direction: column;
        padding: 1rem;
      }
      h1 { font-size: 1.5rem; }
      .card { width: 100%; margin-bottom: 1rem; }
    }

    Expected result

    Below 640px viewport, the layout becomes single-column with smaller font and spacing; content does not overflow.

Try it yourself

What you learned

You built a responsive navigation bar with flexbox, semantic HTML, and a mobile breakpoint — the navigation pattern used on virtually every website.

Related Resources

Related Lessons

Related Practice

Related Cheatsheets

Related Glossary

  • CSS

    Cascading Style Sheets.

  • HTML

    HyperText Markup Language.

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

  • CSS

    Cascading Style Sheets.

  • HTML

    HyperText Markup Language.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic