Skip to content

Dark Mode Toggle

You'll give your site a dark mode that follows the system setting, offers a switch, remembers the choice and never flashes white on load.

About

Dark mode done right is three things: it follows the system preference by default, it lets people override that, and it doesn't flash white on load. Done wrong, it's a theme switcher nobody asked for. If your colors already live in custom properties from the Type and Color badge, this is a weekend. If they don't, moving them there is the first job.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
5
Takes
Two weekends
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Web Building to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Define a dark palette with the same variable names. Inside @media (prefers-color-scheme: dark), redefine --text, --bg, --accent and --muted. Check the contrast again, because dark mode fails contrast more often than light does. Take a screenshot of the dark block with its contrast result. Photo
  2. 2
    Set color-scheme so form controls and scrollbars follow along. Putting color-scheme: light dark on :root tells the browser to style its native controls to match your page. Take a screenshot of a form in dark mode with the native inputs matching. Photo
  3. 3
    Add a three-way switch for light, dark and system. Put a small control in the header or footer. Use a button or a radio group, give it a label, and make sure it works from the keyboard. Film yourself toggling through all three settings. Video
  4. 4
    Store the choice and apply it before the first paint. Save the choice to localStorage, then read it back in a tiny inline script in the head before the stylesheet loads, so there's no white flash. Film yourself reloading the page in dark mode with no flash. Video
  5. 5
    Check every image and logo in both modes. A black logo on a dark background simply disappears. Use an SVG with currentColor, a picture element with a media query, or a subtle background behind it. Take one screenshot with light and dark side by side. Photo