IntermediateLesson 18 of 308 min read

Build a responsive navigation bar with a mobile menu

Combine flexbox, positioning, media queries and states to build a navigation bar that becomes a burger menu on phones.

In this lesson you will

  • Build mobile and desktop layouts for a nav bar
  • Use sibling combinators and :checked
  • Animate a burger into an X
  • Know the accessible alternative

About 18 minutes

Your first real component

Every website has a navigation bar, and nearly every one has the same problem: on a wide screen the links sit in a row, and on a phone there is no room, so they hide behind a menu button. Building that is a rite of passage. It uses almost everything from this section: flexbox, positioning, media queries and states. And the final result is something you will reuse in every project.

Step 1: the HTML

<header class="site-header">
  <a class="logo" href="/">Skills in Minutes</a>

  <input type="checkbox" id="menu-toggle" class="menu-toggle">
  <label for="menu-toggle" class="menu-button" aria-label="Open menu">
    <span></span><span></span><span></span>
  </label>

  <nav class="menu" aria-label="Main">
    <a href="/skills" aria-current="page">Skills</a>
    <a href="/tools">Tools</a>
    <a href="/blog">Blog</a>
    <a class="cta" href="/signup">Start free</a>
  </nav>
</header>

This uses a well-known CSS-only trick. A hidden checkbox remembers whether the menu is open, and a label connected to it acts as the button. When the checkbox is ticked, CSS can reveal the menu, and no JavaScript is needed. The label has an aria-label so assistive technology knows what the button does.

Step 2: the mobile layout (the default)

.site-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: #fff;
  box-shadow: 0 2px 12px rgba(20, 17, 46, .08);
}
.logo { font-weight: 800; color: #14112E; text-decoration: none; }

.menu-toggle { position: absolute; opacity: 0; pointer-events: none; }

.menu-button {
  display: grid;
  gap: 5px;
  padding: 10px;
  cursor: pointer;
}
.menu-button span {
  display: block;
  width: 24px;
  height: 3px;
  background: #14112E;
  border-radius: 2px;
}

.menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  flex-direction: column;
  background: #fff;
  padding: 8px 20px 20px;
  box-shadow: 0 12px 24px rgba(20, 17, 46, .1);
}
.menu a { padding: 12px 0; color: #14112E; text-decoration: none; font-weight: 600; }

.menu-toggle:checked ~ .menu { display: flex; }

The last rule is the heart of the trick. The ~ sign is the general sibling combinator: it selects .menu when it follows a checked .menu-toggle at the same level. So ticking the box shows the menu.

Press the burger button in the result to open and close the menu on a small screen. Then change the background of the open menu to a light violet (#F1EDFE).

<style>
 body{margin:0;font-family:Arial}
 .bar{position:relative;display:flex;justify-content:space-between;align-items:center;padding:12px 16px;background:#fff;box-shadow:0 2px 10px rgba(0,0,0,.1)}
 .t{position:absolute;opacity:0}
 .b{display:grid;gap:5px;padding:10px;cursor:pointer}
 .b span{display:block;width:24px;height:3px;background:#14112E}
 .m{display:none;position:absolute;top:100%;left:0;right:0;flex-direction:column;background:#fff;padding:8px 16px 16px}
 .m a{padding:10px 0;color:#14112E;text-decoration:none}
 .t:checked ~ .m{display:flex}
</style>
<div class="bar"><strong>Skills</strong><input type="checkbox" id="t" class="t"><label for="t" class="b"><span></span><span></span><span></span></label><nav class="m"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

In the .m rule, change background:#fff to background:#F1EDFE.

<style>
 body{margin:0;font-family:Arial}
 .bar{position:relative;display:flex;justify-content:space-between;align-items:center;padding:12px 16px;background:#fff;box-shadow:0 2px 10px rgba(0,0,0,.1)}
 .t{position:absolute;opacity:0}
 .b{display:grid;gap:5px;padding:10px;cursor:pointer}
 .b span{display:block;width:24px;height:3px;background:#14112E}
 .m{display:none;position:absolute;top:100%;left:0;right:0;flex-direction:column;background:#F1EDFE;padding:8px 16px 16px}
 .m a{padding:10px 0;color:#14112E;text-decoration:none}
 .t:checked ~ .m{display:flex}
</style>
<div class="bar"><strong>Skills</strong><input type="checkbox" id="t" class="t"><label for="t" class="b"><span></span><span></span><span></span></label><nav class="m"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

Step 3: the desktop layout

@media (min-width: 800px) {
  .menu-button { display: none; }

  .menu {
    display: flex;
    position: static;
    flex-direction: row;
    align-items: center;
    gap: 24px;
    padding: 0;
    background: none;
    box-shadow: none;
  }
  .menu a { padding: 6px 0; }
}

At 800 pixels and wider, the burger button disappears and the menu turns back into a normal flex row. The rules inside simply undo the mobile positioning, which is the cost of choosing a hidden mobile menu. The checkbox is still in the page but invisible and harmless.

Step 4: polish

.menu a:hover { color: #5A28C8; }
.menu a[aria-current="page"] { color: #5A28C8; border-bottom: 2px solid currentColor; }

.menu .cta {
  background: #5A28C8;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
}
.menu .cta:hover { background: #3F1A9A; color: #fff; }

.menu-toggle:focus-visible + .menu-button { outline: 3px solid #46D3DC; outline-offset: 2px; }

.menu-toggle:checked + .menu-button span:nth-child(1) { transform: translateY(8px) rotate(45deg); }
.menu-toggle:checked + .menu-button span:nth-child(2) { opacity: 0; }
.menu-toggle:checked + .menu-button span:nth-child(3) { transform: translateY(-8px) rotate(-45deg); }
.menu-button span { transition: transform .2s, opacity .2s; }

The last four rules animate the three bars into an X when the menu is open. The + is the adjacent sibling combinator, selecting the label that comes right after the checkbox. The focus rule is important: because the checkbox itself is invisible, we show the focus ring on the label so keyboard users can see where they are.

Accessibility of the checkbox trick

The checkbox method works without JavaScript and is a good way to learn, but it has limits. Screen readers announce a checkbox rather than a menu button, and the menu does not close when you press Escape or click a link on the same page. For professional sites, the usual solution is a real button with aria-expanded and a few lines of JavaScript.

<button class="menu-button" aria-expanded="false" aria-controls="menu">Menu</button>
<nav id="menu" class="menu">...</nav>

<script>
  const btn = document.querySelector('.menu-button');
  const menu = document.getElementById('menu');
  btn.addEventListener('click', () => {
    const open = btn.getAttribute('aria-expanded') === 'true';
    btn.setAttribute('aria-expanded', String(!open));
    menu.classList.toggle('is-open');
  });
</script>

Then style .menu.is-open { display: flex; } instead of the checkbox rule. The CSS parts you learned today stay exactly the same. In the JavaScript course you will build this properly.

A sticky header

.site-header { position: sticky; top: 0; z-index: 100; }

Add this one line and the bar stays at the top while the page scrolls. A semi-transparent background with backdrop-filter: blur(8px) makes it look modern, as content seems to fade through frosted glass.

Common mistake

Forgetting position: relative on the header. The mobile menu uses position: absolute; top: 100%, and without a positioned parent it flies off to the page corner instead of dropping under the bar.

Make the desktop version: at 600px and wider, hide the burger label (.b) and show the menu (.m) as a static flex row with no background.

<style>
 body{margin:0;font-family:Arial}
 .bar{position:relative;display:flex;justify-content:space-between;align-items:center;padding:12px 16px;background:#fff;box-shadow:0 2px 10px rgba(0,0,0,.1)}
 .t{position:absolute;opacity:0}
 .b{display:grid;gap:5px;padding:10px}
 .b span{display:block;width:24px;height:3px;background:#14112E}
 .m{display:none;position:absolute;top:100%;left:0;right:0;flex-direction:column;background:#fff;padding:8px 16px 16px}
 .m a{padding:10px 0;color:#14112E;text-decoration:none}
 /* add the media query */
</style>
<div class="bar"><strong>Skills</strong><label class="b"><span></span><span></span><span></span></label><nav class="m"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

@media (min-width:600px){ .b{display:none} .m{display:flex;position:static;flex-direction:row;gap:20px;padding:0;background:none} }

<style>
 body{margin:0;font-family:Arial}
 .bar{position:relative;display:flex;justify-content:space-between;align-items:center;padding:12px 16px;background:#fff;box-shadow:0 2px 10px rgba(0,0,0,.1)}
 .t{position:absolute;opacity:0}
 .b{display:grid;gap:5px;padding:10px}
 .b span{display:block;width:24px;height:3px;background:#14112E}
 .m{display:none;position:absolute;top:100%;left:0;right:0;flex-direction:column;background:#fff;padding:8px 16px 16px}
 .m a{padding:10px 0;color:#14112E;text-decoration:none}
 @media (min-width:600px){ .b{display:none} .m{display:flex;position:static;flex-direction:row;gap:20px;padding:0;background:none} .m a{padding:0} }
</style>
<div class="bar"><strong>Skills</strong><label class="b"><span></span><span></span><span></span></label><nav class="m"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does the checkbox trick rely on?

  1. JavaScript only
  2. The :checked state and the sibling combinator
  3. Floats
  4. Tables

A checked checkbox lets CSS reveal the menu with ~ or +.

Why does the mobile menu need a positioned parent?

  1. It is required by HTML
  2. So position: absolute is measured from the header
  3. To change the colour
  4. To hide the logo

Without it the menu is placed relative to the page.

What does ~ mean in .a:checked ~ .b?

  1. Child of
  2. Any later sibling
  3. Parent of
  4. Descendant

The general sibling combinator selects later siblings.

What is a better approach for accessibility than the checkbox?

  1. More checkboxes
  2. A real button with aria-expanded and a little JavaScript
  3. A bigger image
  4. Removing the menu

A button with aria-expanded communicates state to assistive technology.

Which makes the header stay at the top while scrolling?

  1. position: sticky; top: 0
  2. float: top
  3. display: sticky
  4. z-index: 0

Sticky with an offset keeps it in view.

Summary

  • Style the mobile menu first, then reveal a row at a breakpoint.
  • A checkbox and sibling selectors can toggle a menu with CSS.
  • Real buttons with aria-expanded are better for accessibility.
  • A sticky header keeps navigation in reach.

Next lesson

Next: card layouts and components, the building blocks of modern pages.

Questions

Is the checkbox trick bad practice?

It is fine for learning and simple sites, but a button with aria-expanded and a little JavaScript is more accessible.

Why use a label for the burger?

Clicking a label toggles its checkbox, so the label acts as a button without any script.

How do I make the menu slide instead of appear?

Animate transform or max-height with a transition. The advanced lessons cover transitions.

Progress is saved in this browser.