EthicsComputerCourses
Join training +91 90532 10052

Responsive Navigation Menus

CSS · Lesson 128 of 240 Intermediate 11 min read Updated

By the end of this lesson you will be able to choose a navigation pattern from the number and type of links a site has, build four common responsive patterns with CSS, and check each one for keyboard, screen reader and touch problems.

You should know CSS Navigation Bars, which covers the HTML for a menu, aria-current, focus styles and a basic popover menu, and Media Queries. This lesson builds on both and compares the patterns side by side.

One menu does not fit every screen

Think of a busy dhaba. The wall board shows the whole menu; the small card on your table shows only the bestsellers. Same food, shown differently because the space is different.

A laptop header has room for eight links in a row; a phone has room for about four short words. Every responsive menu answers one question: what happens to the links that do not fit? There are five common answers:

  • Wrap them onto a second line.
  • Scroll them sideways in a strip.
  • Hide the least important ones behind a "More" button (priority+).
  • Move the main ones to a tab bar at the bottom of the screen.
  • Hide all of them in a drawer behind a menu button (off-canvas).

Count the links first, then ask how people use them.

Top-level linksHow people use themGood pattern
2 to 5, short labelsAnyWrapping links
5 to 12Browsing equal categories (Mobiles, Fashion, Grocery...)Horizontal scroll strip
6 to 10A few links are used far more than the restPriority+
3 to 5App-like site, people switch between sections all dayBottom tab bar on phones
10 or more, or nested sectionsMostly reached by search or deep linksOff-canvas drawer

These are starting points. Many sites combine two, such as a bottom tab bar plus a drawer for settings and help.

You already built this in CSS Navigation Bars: a flex list with flex-wrap: wrap and gap. Nothing is hidden, so it is the most accessible pattern of all. Every link is visible, in order, at every width.

Its limit is height. Two lines of links on a phone are fine; four lines push the content down on every page. Once your links wrap to more than two lines at 320px wide, move to another pattern.

Pattern 2: a horizontal scroll strip

Shopping apps often show categories as one line that scrolls sideways, so the header stays short. Scroll Snap makes it stop neatly at a link.

Step 1: build the strip

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Scrolling category strip</title>
  <style>
    body { margin: 0; font-family: system-ui, sans-serif; }
    header { padding-top: 0.75rem; background: #0f766e; color: white; }
    header strong { display: block; margin: 0 1rem 0.5rem; }
    .strip ul {
      display: flex;
      gap: 0.5rem;
      margin: 0;
      padding: 0 1rem 0.75rem;
      list-style: none;
      overflow-x: auto;
      scroll-snap-type: x proximity;
      scroll-padding-inline: 1rem;
    }
    .strip li { flex: none; scroll-snap-align: start; }
    .strip a {
      display: block;
      padding: 0.5rem 0.9rem;
      border-radius: 999px;
      background: #ccfbf1;
      color: #134e4a;
      text-decoration: none;
      white-space: nowrap;
    }
    .strip a[aria-current="page"] { background: #134e4a; color: white; font-weight: 700; }
    .strip a:focus-visible { outline: 3px solid #fde047; outline-offset: 2px; }
    @media (min-width: 60rem) {
      .strip ul { flex-wrap: wrap; overflow-x: visible; }
    }
    main { padding: 1rem; }
  </style>
</head>
<body>
  <header>
    <strong>Bazaar Kochi</strong>
    <nav class="strip" aria-label="Categories">
      <ul>
        <li><a href="#">Mobiles</a></li>
        <li><a href="#" aria-current="page">Kitchen</a></li>
        <li><a href="#">Fashion</a></li>
        <li><a href="#">Grocery</a></li>
        <li><a href="#">Books</a></li>
        <li><a href="#">Toys</a></li>
        <li><a href="#">Spices</a></li>
        <li><a href="#">Handloom</a></li>
        <li><a href="#">Pooja items</a></li>
        <li><a href="#">Garden</a></li>
      </ul>
    </nav>
  </header>
  <main><h1>Kitchen</h1><p>Steel tiffin box, 3 tier: ₹449.</p></main>
</body>
</html>

Make the preview narrow. The pills stay on one line, run edge to edge, and the last visible one is partly cut off. That cut-off pill is useful: it tells people "there is more, swipe me". Scroll sideways and the strip settles with a pill at its start edge. Now make the preview wider than 60rem: the media query turns scrolling off and lets the pills wrap, because there is room to show them all.

How it works:

  • overflow-x: auto makes the list a scroll container, and flex: none stops the pills from shrinking to fit.
  • white-space: nowrap keeps "Pooja items" on one line inside its pill.
  • scroll-snap-type: x proximity snaps only when you stop near a pill. mandatory would fight people who want to stop between pills.
  • scroll-padding-inline: 1rem makes the snap position line up with the list's side padding, so a snapped pill does not touch the screen edge.

Step 2: check it with the keyboard

Click in the preview and press Tab. Browsers scroll each focused link into view, so keyboard users can reach every link. Two risks remain:

  • Never hide the scrollbar on desktop, or mouse users may not know the strip scrolls.
  • The current page may be out of view when the page loads. A line of JavaScript such as currentLink.scrollIntoView({ inline: "center", block: "nearest" }) fixes that.

Pattern 3: priority+

Priority+ (priority plus) shows the most important links in the bar and moves the rest into a "More" menu. As the screen grows, links move out of "More" and into the bar, like reserve players called in when there is space.

The trick is to give each link a priority class. Mobile first, the bar shows only priority 1. The "More" list holds copies of the lower-priority links. A media query shows a group in the bar and hides its copy in "More" at the same time.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Priority plus navigation</title>
  <style>
    body { margin: 0; font-family: system-ui, sans-serif; }
    .site-header {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 0.5rem 1rem;
      padding: 0.5rem 1rem;
      background: #fff7ed;
      border-bottom: 1px solid #fed7aa;
    }
    .site-header strong { margin-inline-end: auto; color: #9a3412; }
    nav { display: flex; align-items: center; gap: 0.25rem; }
    nav ul { display: flex; gap: 0.25rem; margin: 0; padding: 0; list-style: none; }
    nav a, .more-btn {
      display: block;
      padding: 0.6rem 0.75rem;
      border: 0;
      border-radius: 6px;
      background: none;
      color: #7c2d12;
      font: inherit;
      font-weight: 600;
      text-decoration: none;
    }
    .more-btn { border: 2px solid #c2410c; }
    nav a:focus-visible, .more-btn:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 2px; }
    .more-list {
      inset: 3.75rem 1rem auto auto;
      margin: 0;
      padding: 0.5rem;
      border: 1px solid #fed7aa;
      border-radius: 8px;
      box-shadow: 0 8px 20px rgb(0 0 0 / 0.15);
    }
    .more-list ul { flex-direction: column; }

    /* Phones: only priority 1 sits in the bar */
    .bar .p2, .bar .p3 { display: none; }

    @media (min-width: 40rem) {
      .bar .p2 { display: block; }
      .more-list .p2 { display: none; }
    }
    @media (min-width: 60rem) {
      .bar .p3 { display: block; }
      .more-btn { display: none; }
    }
    main { padding: 1rem; }
  </style>
</head>
<body>
  <header class="site-header">
    <strong>Chai Corner</strong>
    <nav aria-label="Main">
      <ul class="bar">
        <li><a href="#">Menu</a></li>
        <li><a href="#">Order</a></li>
        <li class="p2"><a href="#">Offers</a></li>
        <li class="p2"><a href="#">Outlets</a></li>
        <li class="p3"><a href="#">Catering</a></li>
        <li class="p3"><a href="#">Careers</a></li>
        <li class="p3"><a href="#">Contact</a></li>
      </ul>
      <button class="more-btn" type="button" popovertarget="more">More</button>
      <div class="more-list" id="more" popover>
        <ul>
          <li class="p2"><a href="#">Offers</a></li>
          <li class="p2"><a href="#">Outlets</a></li>
          <li class="p3"><a href="#">Catering</a></li>
          <li class="p3"><a href="#">Careers</a></li>
          <li class="p3"><a href="#">Contact</a></li>
        </ul>
      </div>
    </nav>
  </header>
  <main><h1>Fresh chai in Pune</h1><p>Cutting chai ₹15, masala chai ₹30.</p></main>
</body>
</html>

Narrow the preview: you see Menu, Order and a "More" button holding the other five links. Past 40rem, Offers and Outlets move into the bar. Past 60rem the "More" button disappears.

Doesn't the copy of each link confuse screen readers? No. An element with display: none is removed from the accessibility tree and from the Tab order, so at any width each link exists only once for assistive technology. The popover gives you Esc to close and closing on an outside tap for free, as you saw in the Popover API lesson.

Fixed breakpoints suit known labels. If editors can rename links, a small script that measures the bar is more reliable, or you can tie the breakpoints to the header's own width with Container Queries.

Pattern 4: a bottom tab bar on phones

Your thumb rests near the bottom of a phone. App-like sites (tickets, banking, food delivery) therefore put three to five main sections in a bottom bar. On a wider screen, the same <nav> moves to the top.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <title>Bottom tab bar</title>
  <style>
    body { margin: 0; font-family: system-ui, sans-serif; padding-bottom: 5rem; }
    .tabs {
      position: fixed;
      inset: auto 0 0;
      z-index: 10;
      background: white;
      border-top: 1px solid #cbd5e1;
      padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    .tabs ul { display: flex; margin: 0; padding: 0; list-style: none; }
    .tabs li { flex: 1; }
    .tabs a {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 0.2rem;
      min-height: 3.5rem;
      justify-content: center;
      color: #475569;
      font-size: 0.8rem;
      text-decoration: none;
    }
    .tabs svg { width: 1.5rem; height: 1.5rem; fill: currentColor; }
    .tabs a[aria-current="page"] { color: #1d4ed8; font-weight: 700; }
    .tabs a:focus-visible { outline: 3px solid #1d4ed8; outline-offset: -3px; }

    @media (min-width: 48rem) {
      body { padding-bottom: 0; }
      .tabs { position: sticky; inset: 0 auto auto; border-top: 0; border-bottom: 1px solid #cbd5e1; }
      .tabs ul { justify-content: center; gap: 1rem; }
      .tabs li { flex: none; }
      .tabs a { flex-direction: row; padding-inline: 1rem; font-size: 1rem; }
    }
    main { padding: 1rem; }
    section { min-height: 60vh; }
  </style>
</head>
<body>
  <nav class="tabs" aria-label="Main">
    <ul>
      <li><a href="#"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 11 12 3l9 8v10h-6v-6H9v6H3z"/></svg>Home</a></li>
      <li><a href="#"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M10 3a7 7 0 1 0 4.2 12.6l5.6 5.6 1.4-1.4-5.6-5.6A7 7 0 0 0 10 3zm0 2a5 5 0 1 1 0 10 5 5 0 0 1 0-10z"/></svg>Search</a></li>
      <li><a href="#" aria-current="page"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 6h18v4a2 2 0 0 0 0 4v4H3v-4a2 2 0 0 0 0-4z"/></svg>Tickets</a></li>
      <li><a href="#"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0z"/></svg>Account</a></li>
    </ul>
  </nav>
  <main>
    <section><h1>My tickets</h1><p>Jaipur to Delhi, 14 Dec, coach B2, seat 31.</p></section>
    <section><h2>Past trips</h2><p>Lucknow to Kanpur, ₹180.</p></section>
  </main>
</body>
</html>

On a narrow preview the four tabs sit at the bottom and stay put while you scroll. Past 48rem the same links become a centred, sticky bar at the top.

Details that matter:

  • The <nav> comes before <main> in the HTML. It only looks like it is at the bottom, so Tab and reading order stay sensible.
  • padding-bottom on the body stops the fixed bar from covering the last lines of content.
  • env(safe-area-inset-bottom) adds space above the home indicator on phones with no home button. It needs viewport-fit=cover in the viewport meta tag.
  • Each tab is a full quarter of the width and 3.5rem tall, far above the 24 by 24 CSS pixel minimum target size in WCAG 2.2.
  • The icons have aria-hidden="true" and every tab has a visible text label, because icons alone are guesswork.

Pattern 5: the off-canvas drawer

For large sites, all links go into a panel that slides in from the side when a menu button is pressed. The core CSS keeps the panel off screen and hidden until it is opened:

CSS
.drawer {
  position: fixed;
  inset-block: 0;
  inset-inline-start: 0;
  width: min(20rem, 85vw);
  translate: -100% 0;      /* parked off the left edge */
  visibility: hidden;      /* closed links cannot be focused */
  transition: translate 0.3s, visibility 0.3s;
}
.drawer.is-open { translate: 0 0; visibility: visible; }

Without visibility: hidden, the links are only moved off screen, and keyboard users tab into invisible links. The button, the toggle script, Esc to close and focus handling are built in the next lesson, Hamburger Menu Styling.

Accessibility of each pattern

PatternMain riskWhat to do
Wrapping linksMenu becomes very tallSwitch pattern beyond two lines
Scroll stripPeople do not notice it scrollsLet the last link peek, keep the scrollbar on desktop
Priority+Important links hidden in "More"Order priorities by real usage data; label the button "More" in words
Bottom tab barCovers content; icon-only tabsBody padding, safe-area inset, visible labels
Off-canvas drawerHidden links still focusable; low discoverabilityvisibility: hidden when closed; a clear "Menu" label

At each breakpoint, Tab through the menu and zoom to 200%. At 200% a laptop browser uses your narrow-screen layout.

Common mistakes

  • Picking the hamburger for five links. Hiding a few short links costs clicks and discoverability. Wrap or scroll them instead.
  • overflow: hidden on a strip. Links beyond the edge become unreachable by touch. Use overflow-x: auto.
  • Hiding a duplicate link with opacity or off-screen positioning. It stays in the Tab order and is read twice. Use display: none.
  • A fixed bottom bar with no body padding. The last paragraph or the "Pay" button hides under it.
  • Breakpoints chosen by device names. Choose them where your links actually stop fitting, as Breakpoints & Device Widths explains.

Interview questions

1. How do you decide between a hamburger menu and visible links?

Count the top-level links and check how often each is used. With five or fewer short links, keep them visible, because hidden links are used less. A drawer suits large or nested menus.

2. What is the priority+ pattern?

It shows the most important links in the bar and puts the rest in a "More" menu. As the screen gets wider, links move from the menu into the bar. It keeps the key links visible on phones without losing the others.

3. How do you make a horizontally scrolling menu usable?

Use overflow-x: auto and flex: none on the items. Let the last item be partly cut off as a hint, add scroll-snap-type: x proximity, and keep the scrollbar for mouse users. Focused links scroll into view by themselves.

4. Why put a bottom tab bar's <nav> before <main> in the HTML?

Screen readers and the Tab key follow the HTML order, not the visual position. With the nav first, the reading order is navigation then content on every screen size. Placing it with position: fixed changes only where it is drawn.

5. How do you hide a closed off-canvas menu from keyboard users?

Add visibility: hidden (or display: none or inert) to the closed panel, not only a transform. A transform just moves the links off screen, so they can still receive focus. Transitioning visibility together with the transform keeps the slide animation smooth.

Key points

  • Every responsive menu answers one question: what happens to the links that do not fit.
  • Choose by link count and usage: wrap (2-5), scroll strip (many equal categories), priority+ (a few key links), bottom tab bar (3-5 app sections), drawer (10+ or nested).
  • A scroll strip needs overflow-x: auto, flex: none items and a visible hint that it scrolls.
  • In priority+, display: none removes duplicate links from the Tab order and accessibility tree.
  • A bottom tab bar needs body padding, env(safe-area-inset-bottom) and visible text labels.
  • A closed drawer must be hidden with visibility: hidden, not only moved off screen.

Exercise

Meera runs a school website in Lucknow with eight links: Home, Admissions, Results, Fee payment, Notices, Gallery, Transport and Contact. Parents mostly use Results, Fee payment and Notices. Build a priority+ header: on phones show those three links plus a "More" button with the other five; from 45rem show Admissions and Contact in the bar too; from 65rem show all links and remove the button. Mark Notices as the current page.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>School navigation</title>
  <style>
    body { margin: 0; font-family: system-ui, sans-serif; }
    header { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; padding: 0.5rem 1rem; background: #eef2ff; }
    header strong { margin-inline-end: auto; }
    nav { display: flex; align-items: center; gap: 0.25rem; }
    nav ul { display: flex; gap: 0.25rem; margin: 0; padding: 0; list-style: none; }
    nav a, nav button { display: block; padding: 0.6rem 0.7rem; color: #312e81; font: inherit; text-decoration: none; }
    nav a[aria-current="page"] { border-bottom: 3px solid #312e81; font-weight: 700; }
    #more { inset: 3.5rem 1rem auto auto; margin: 0; padding: 0.5rem; }
    #more ul { flex-direction: column; }
    .bar .p2, .bar .p3 { display: none; }
    @media (min-width: 45rem) {
      .bar .p2 { display: block; }
      #more .p2 { display: none; }
    }
    @media (min-width: 65rem) {
      .bar .p3 { display: block; }
      nav button { display: none; }
    }
  </style>
</head>
<body>
  <header>
    <strong>Gomti Public School</strong>
    <nav aria-label="Main">
      <ul class="bar">
        <li class="p3"><a href="#">Home</a></li>
        <li class="p2"><a href="#">Admissions</a></li>
        <li><a href="#">Results</a></li>
        <li><a href="#">Fee payment</a></li>
        <li><a href="#" aria-current="page">Notices</a></li>
        <li class="p3"><a href="#">Gallery</a></li>
        <li class="p3"><a href="#">Transport</a></li>
        <li class="p2"><a href="#">Contact</a></li>
      </ul>
      <button type="button" popovertarget="more">More</button>
      <div id="more" popover>
        <ul>
          <li class="p3"><a href="#">Home</a></li>
          <li class="p2"><a href="#">Admissions</a></li>
          <li class="p3"><a href="#">Gallery</a></li>
          <li class="p3"><a href="#">Transport</a></li>
          <li class="p2"><a href="#">Contact</a></li>
        </ul>
      </div>
    </nav>
  </header>
</body>
</html>

The links keep their normal order in the HTML, and the priority classes decide which ones are shown in the bar at each width. Each media query shows a group in the bar and hides its copy in "More" together, so no link is ever listed twice for a screen reader.

Quick Quiz

1. A site has four short top-level links. Which pattern is usually best on phones?

2. In a priority+ menu, why is it safe to have each low-priority link twice in the HTML?

3. What does env(safe-area-inset-bottom) add to a bottom tab bar?

4. A slide-in drawer is moved off screen with translate: -100% 0 when closed. What else does it need?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call