Skip to content
Tutorials Center
My learning

Topics

Lesson 126 of 139 ยท Projects

Project: Responsive Navigation Bar

Build a responsive site header in seven stages with flexbox, a details and summary mobile menu, clear link states, a media query and a sticky header.

Almost every website starts with the same strip across the top: a logo on one side, a few links on the other. On a laptop that is easy. On a phone there is no room for a row of links, so they fold away behind a "Menu" button that opens a panel. Building one header that does both, and works with a mouse, a finger and a keyboard, is a rite of passage for every CSS developer.

In this project you will build that header in seven stages. As in the profile card project, every stage has its own editor with the complete code so far, so you can start anywhere and compare your version with ours. Each stage adds one idea and links to the lesson that covers it in depth.

Our example site is Fernleaf, a fictional tea shop. The menu button is a native <details> element, so it opens and closes without a single line of JavaScript. We add one tiny script only at the end, for wide screens, and we will explain exactly why.

Stage 1: The markup

The header holds two things: the brand link and a <nav>. The aria-label="Main" gives the navigation landmark a name, which helps screen reader users jump straight to it (see Navigation).

Inside the <nav> is a <details> element. Its <summary> is the visible button, and everything after the summary is hidden until the button is pressed. Browsers give <details> keyboard support, focus handling and an "expanded/collapsed" state for assistive technology for free. Details and Summary covers the element itself.

The links live in an ordinary list. The current page is marked with aria-current="page", which screen readers announce and which you will soon use as a styling hook. The last link is the call to action, so it gets a class of its own.

The page below the header is only there for context. It has extra height so that you can scroll the preview in the final stage.

Stage 1: markup

Click Menu in the preview. The list appears and disappears, with no CSS and no JavaScript. Everything that follows is presentation on top of a header that already works.

Stage 2: A flexbox header bar

A header bar is the textbook flexbox pattern (Flexbox Patterns). Three declarations do almost all of the work:

  • display: flex puts the brand and the nav side by side.
  • justify-content: space-between pushes them to opposite ends.
  • align-items: center lines them up vertically, whatever their heights.

The gap guarantees a minimum distance between the two if the screen gets very narrow. See Flex Container Properties for every option.

The design tokens go on .site-header as custom properties right from the start. Every color you add later reads from them, so the whole header can be re-themed in one place.

The logo mark needs no image. The brand link is itself a flex container, and its ::before pseudo-element becomes a small gradient square. border-radius: 50% 0 rounds the top-left and bottom-right corners and leaves the other two sharp, which turns the square into a leaf (Rounded Corners).

Stage 2: header bar

The ::before element is empty (content: ""), but because it is a flex item it accepts a width and height like any block. Outside a flex container you would need display: inline-block for the same effect.

Stage 3: The menu button

The default <summary> shows a small triangle. It is a list marker, so list-style: none removes it in most browsers, and the ::-webkit-details-marker pseudo-element removes it in Safari. With the marker gone, the summary gets a border, padding and cursor: pointer to make it look and feel like a button.

The "hamburger" icon is drawn entirely in CSS. The empty <span> is the middle line. Its ::before and ::after pseudo-elements are the top and bottom lines, positioned absolutely and moved 6 pixels up and down with transform (2D Transforms).

When the menu is open, the <details> element gets an open attribute. The selector .menu[open] matches only then (Attribute Selectors), so it can turn the icon into a cross: hide the middle line, then rotate the other two by 45 degrees in opposite directions. Because both lines start from a translated position and end at a rotated one, the transition animates them smoothly between the two shapes.

Stage 3: menu button

The icon has aria-hidden="true" in the markup. It is decoration, and the word "Menu" next to it already tells everyone what the button does.

Change translateY(-6px) and translateY(6px) to -4px and 4px for a tighter icon, or change height: 2px to 3px for a bolder one. Remember to update both the span and the pseudo-elements.

Stage 4: The dropdown panel

Right now, opening the menu pushes the page content down and squeezes the links inside the header row. A dropdown should instead float over the page, directly under the header, at full width.

That is a job for absolute positioning. An absolutely positioned element is placed against its containing block: the nearest ancestor that is itself positioned. Neither <nav> nor <details> is positioned, so adding position: relative to the header makes the header the reference. Then:

  • top: 100% places the panel's top edge at the header's bottom edge.
  • inset-inline: 0 stretches it from the left edge to the right edge (a logical property for left: 0; right: 0).

The links become display: block with generous padding, so the whole row is clickable, not just the word. On touch screens, a tap target of at least 44 pixels tall is a good rule of thumb (Accessible CSS).

Stage 4: dropdown panel

The panel covers the page text without any z-index. Positioned elements paint above non-positioned ones, and the page content is not positioned. If you later position something in the page, z-index explains how to keep the menu on top.

Navigation links need to answer three questions at a glance: Where am I? What can I click? Where is my keyboard focus?

  • Hover gives each link a pale green background, so the target is obvious.
  • The current page is selected with a[aria-current="page"]. It is bold, green, and has a 3-pixel bar on the left, drawn with an inset box-shadow. A shadow doesn't take up space, so the text doesn't shift the way it would with a border.
  • Focus gets a thick amber outline on :focus-visible, so keyboard users can always see where they are (Pseudo-classes and Outlines). The brand, the summary and every link share one rule.
  • The call to action becomes a solid green button (Styling Buttons and Links).
Stage 5: link states

Styling the current page through aria-current instead of a class like .active has a nice side effect: the visual state and the accessible state can never disagree. If the attribute is missing, the styling is missing too, which is a bug you will notice right away.

Press Tab inside the preview: focus moves from the brand to the Menu button. Press Enter to open the menu, then keep tabbing through the links.

Stage 6: The wide-screen layout

On a wide screen there is room for the links in a row, so the menu button should disappear. This is where a media query comes in. Everything written so far is the small-screen design, and @media (min-width: 32em) layers the wide-screen changes on top. Building small first and adding rules for bigger screens is the mobile-first approach.

Inside the media query, the list goes back to position: static, turns into a flex row and drops its panel background and shadow. The current-page bar moves from the left side to the bottom, becoming an underline, and the call to action turns into a pill.

There is one catch. A closed <details> element hides its content, and CSS cannot reliably reveal it in every browser. So the wide layout needs the menu to be open. The small script at the end does exactly that, and nothing more: it uses matchMedia() with the same query as the CSS, sets menu.open to true on wide screens and false on narrow ones, and repeats whenever the width crosses the breakpoint. On wide screens, the CSS then hides the summary with .menu[open] > summary { display: none; }.

Stage 6: wide screens

The breakpoint is 32em (512 pixels), which suits this header's content: the brand plus four short links fit comfortably above that width. The preview pane on this page may be wider or narrower than that, depending on your screen. To see the other layout, temporarily change 32em to 20em (always wide) or 60em (always narrow) in both the media query and the script.

The script is an enhancement, not a requirement. Without it, wide screens show the Menu button; pressing it opens the menu, the summary hides itself, and the links appear in a row. Nobody is ever locked out of the navigation.

Stage 7: Sticky, frosted and gently animated

The last stage adds the polish that makes a header feel finished.

A sticky header. position: sticky with top: 0 keeps the header pinned to the top of the viewport while the page scrolls. Sticky elements count as positioned, so the header still acts as the containing block for the dropdown, and nothing else has to change. z-index: 10 keeps it above any positioned content that scrolls underneath.

A frosted background. The header background becomes 80% opaque white, and backdrop-filter: blur(10px) blurs whatever scrolls behind it. The text stays readable, and the page feels layered. See Filters for more backdrop effects.

A drop-in animation. On small screens, the panel fades in and slides down a few pixels each time it opens (Keyframe Animations). The @keyframes rule only defines a from frame, so the panel animates from that state to its normal styles. The media query combines two conditions: (width < 32em) limits it to the dropdown layout, and (prefers-reduced-motion: no-preference) leaves it out for people who have asked their system to reduce motion. A separate block also turns off the icon transitions for them (Motion and Performance).

Stage 7: the finished navigation bar

Scroll the preview: the header stays put and the text blurs as it passes underneath. Open the menu while scrolled halfway down, and the panel still drops neatly from the header's bottom edge.

(width < 32em) uses the media query range syntax, which reads like math and is supported in all current browsers. It is exactly the opposite of (min-width: 32em), so the two layouts never overlap.

Ideas to take it further

  • Add a dark theme by redefining the header's custom properties inside @media (prefers-color-scheme: dark) (Dark Mode).
  • Hide the header when scrolling down and show it when scrolling up. That needs a little JavaScript to toggle a class, but the movement itself can be a CSS transform with a transition.
  • Add a small badge with the number of items in the basket to the call to action, positioned in its top corner with position: absolute.
  • Turn the link row into a centered layout with the brand in the middle, using Grid Template Areas.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The dropdown list uses position: absolute and top: 100%. What is it positioned against?

    Absolutely positioned elements use the nearest positioned ancestor as their containing block. Neither <nav> nor <details> is positioned, so the header (with position: relative, later sticky) is the reference.
  2. 2. Which pair of rules removes the default triangle from a <summary>?

    The triangle is a list marker, so list-style controls it in most browsers. Safari draws it with the ::-webkit-details-marker pseudo-element, which needs its own rule.
  3. 3. Why does the project need a short script for wide screens?

    The script keeps menu.open in sync with the same media query as the CSS. Without it, the menu still works; it just stays behind the button until someone presses it.
  4. 4. Which media query runs the drop-in animation only in the dropdown layout, and only for people who haven't asked to reduce motion?

    and requires both conditions. A comma means "or", so the third option would also animate for people who prefer reduced motion on narrow screens.
  5. 5. What is the advantage of styling the current link with a[aria-current="page"] instead of an .active class?

    An attribute selector has the same specificity as a class. The benefit is that one attribute serves both purposes, so they can't drift apart.

Summary

  • Start with real structure: a <nav> landmark with a label, a list of links, and aria-current="page" on the current one.
  • <details> and <summary> give you an accessible menu button with no JavaScript.
  • display: flex with justify-content: space-between and align-items: center is the classic header bar.
  • Pseudo-elements and transforms can draw a menu icon that turns into a cross when .menu[open] matches.
  • position: absolute with top: 100% and inset-inline: 0 hangs a full-width panel under the positioned header.
  • Write the small-screen design first, then add a min-width media query for the row of links.
  • position: sticky, backdrop-filter and a short animation guarded by prefers-reduced-motion add the final polish.

Next: Project: Pricing Page, where you lay out three plans with CSS Grid and make one of them stand out.