Skip to content
Tutorials Center
My learning

Topics

Lesson 145 of 156 ยท Projects

Project: Dark Mode, Mobile Nav, Expanding Cards, Accordion

Build four interface patterns: a dark mode toggle that respects prefers-color-scheme, a responsive mobile menu, expanding cards and an accessible accordion.

Open almost any modern website and you will find the same four building blocks: a theme switch (light, dark or follow the system), a menu that collapses into a button on small screens, a row of cards that expand when you pick one and an accordion of questions and answers. They look different on every site, but the JavaScript underneath is tiny. The skill is in the details: respecting the visitor's system settings, announcing state to screen readers, keeping the keyboard working and animating without breaking any of that.

This project builds all four. Each one follows the same principle: CSS draws every state, JavaScript only flips an attribute. The attribute (data-theme, aria-expanded, aria-pressed) is the single source of truth, so styles, screen readers and your script can never disagree.

You will use ideas from Styles and Classes, Events, Web Storage and Browser Object Model.

Project 1: Dark mode that respects the system

A good theme switch has three choices, not two: System (follow the operating system, and keep following it if it changes at sunset), Light and Dark. The visitor's explicit choice wins over the system.

Three pieces make it work:

  1. CSS variables hold every colour. The color-scheme property tells the browser which scheme to use for built-in controls and scrollbars, and light-dark(a, b) picks the first colour in a light scheme and the second in a dark one. With color-scheme: light dark the page follows the system automatically, with no JavaScript at all.
  2. An attribute on <html> (data-theme="light" or "dark") overrides that when the visitor chooses. In System mode the attribute is absent.
  3. Storage remembers the choice, wrapped in try/catch because storage can be blocked (private windows, strict settings and this lesson's sandbox).
Project 1: Theme switch with System, Light and Dark

Click Dark and the whole page, including the input's built-in styling, switches at once. Click System and it goes back to whatever your operating system says. Because aria-pressed tells assistive technology which button is on, and the status line spells out the result, nobody has to guess.

On a real site, put a tiny inline script in the <head> that sets data-theme from storage before the page paints. Otherwise a visitor who chose Dark sees a flash of the light theme on every load. The script is only one line: try { const t = localStorage.getItem("theme"); if (t && t !== "system") document.documentElement.dataset.theme = t; } catch {}.

Project 2: A mobile navigation menu

On wide screens the links sit in a row. On narrow screens they collapse behind a menu button. The accessible version needs:

  • a real <button> with aria-expanded (true or false) and aria-controls pointing at the list;
  • Escape to close, returning focus to the button;
  • closing when a link is chosen or when the visitor clicks elsewhere;
  • CSS (not JavaScript) deciding which layout to show.

To let you see both layouts without resizing the whole browser, the demo wraps the navigation in a box with a resize handle (drag the bottom-right corner). It uses a container query, which responds to the width of the box instead of the screen: @container (max-width: 520px). For a normal site you would use a media query, @media (max-width: 40rem), with the same rules inside.

Project 2: Responsive navigation (drag the corner to resize)

The JavaScript never says "show" or "hide". It only changes aria-expanded, and the CSS rule .toggle[aria-expanded="true"] + nav .menu shows the list. That has two benefits: the visible state and the announced state cannot drift apart, and when the box becomes wide again the CSS shows the full menu no matter what the attribute says.

When the menu is closed on a narrow screen, display: none removes the links from the Tab order and from the accessibility tree. Never hide a menu with only opacity: 0 or by moving it off-screen: keyboard users would tab into invisible links.

Project 3: Expanding cards

A row of panels where one is wide and the rest are narrow slivers, and the wide one changes with a click. It is pure CSS plus one line of state: flex: 1 for every card and flex: 5 for the active one, with a transition on flex. Because cards are real <button> elements, they work with the keyboard and expose aria-pressed. Animation respects prefers-reduced-motion.

Project 3: Expanding cards

One delegated listener handles all cards (see Bubbling, Capturing and Delegation). Notice that the label of the inactive cards is hidden visually but still part of each button's text, so screen readers can read every card's name. Each card also has a --hue custom property set inline, which the CSS turns into a gradient.

Project 4: An accessible accordion

An accordion shows a list of headings; selecting one reveals its panel. The modern HTML shortcut is <details> and <summary>, which needs no JavaScript at all, and adding the same name attribute to several <details> makes them exclusive (opening one closes the others). Use it whenever it fits.

When you want smoother animation or extra behaviour, build it from buttons. The pattern from the ARIA authoring practices is:

  • each heading contains a <button> with aria-expanded and aria-controls;
  • each panel has an id, role="region" and aria-labelledby pointing back at its button;
  • Up/Down arrows, Home and End move between headers (optional but pleasant).

The animation trick: a CSS grid whose single row goes from 0fr to 1fr. That animates smoothly to the panel's natural height, which plain height: auto never can. A visibility change with a delay keeps closed panels out of the Tab order.

Project 4: Accordion with one-at-a-time mode

Open a panel with the mouse, then use Tab to reach a header and the arrow keys to walk through them. Tick Allow several panels open and open two at once. The Tab key never lands on text in a closed panel, because its content is visibility: hidden.

The CSS uses :has() to look from the heading to the panel that follows it, so a single aria-expanded attribute on the button styles both. Without :has() you would also toggle a class on the panel from JavaScript, which is perfectly fine but gives you two things to keep in step.

Ideas to extend it

  • Remember the accordion state in the URL hash (#p2), so a shared link opens the right panel.
  • Theme transitions: add transition: background-color .2s to body so switching feels gentle, and disable it for prefers-reduced-motion.
  • Submenus: add a nested list under one nav item, controlled by its own aria-expanded button.
  • Hover to expand cards as well as click, using pointerenter, while keeping the click and keyboard paths.
  • A "contrast" theme (data-theme="contrast") with prefers-contrast: more support.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the theme switch, what does choosing 'System' do to the page?

    Without the attribute, color-scheme: light dark and light-dark() make the page follow the operating system, and it keeps following if the system changes.
  2. 2. Why does the storage code sit inside try/catch?

    Accessing storage can throw a SecurityError. The page should still work, just without remembering the choice.
  3. 3. How does the mobile menu decide whether the list is visible?

    One attribute drives both the visual state and the announced state, and CSS handles the layout, so they can't disagree.
  4. 4. Why does the accordion hide closed panels with visibility: hidden after the animation?

    Content with zero height can still receive focus and be read aloud. visibility: hidden (delayed until the animation ends) removes it from both.

Summary

  • Let CSS draw every state; JavaScript flips one attribute (data-theme, aria-expanded, aria-pressed) and the attribute is the single source of truth.
  • A theme switch has System, Light and Dark. Use color-scheme and light-dark() so the system choice needs no JavaScript, wrap storage in try/catch, and set the theme in the <head> to avoid a flash.
  • A mobile menu is a real button with aria-expanded and aria-controls, closes on Escape, link click or outside click, and uses display: none when collapsed.
  • Container queries let a component respond to its own width.
  • Expanding cards animate flex; accordions animate a grid row from 0fr to 1fr.
  • Honour prefers-reduced-motion, keep every control keyboard-operable and use native elements (button, details) wherever they are enough.

Next: Project: Sortable Table and Nested Comments.