Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Add a Theme Switcher Without JavaScript

In this lesson, you granted Bea’s two wishes for the Hawthorn Seed Library’s catalogue: a “Returns week” banner that stays dark in both themes, and a switch that lets visitors pick a theme, with no JavaScript. Both come from color-scheme, set on the element that needs it.

Key Points

  • light-dark() doesn’t read the page’s setting. It reads the color-scheme of the element that uses the color, and gives the matching half.
  • color-scheme inherits, so every element gets light dark from :root unless it sets its own. An element’s own color-scheme wins over the one it would inherit.
  • color-scheme: dark on .returns-banner makes its background role give the dark half, on the banner and nowhere else, while the page stays light.
  • A role is resolved on the element where the property is declared. The banner’s background-color is declared in its own rule, so it follows the banner’s scheme. Its color was declared only on the body, which resolved it in the light scheme, so the banner inherited that finished color, not the pair. So the banner sets color: var(--color-text) itself, and the role is resolved on the banner.
  • The switch is a <fieldset> with a <legend> and three radios that share a name: System, Light and Dark. System starts checked, so the system setting stays the default.
  • In these rules, :has() matches an element that contains whatever its parentheses describe. :root:has(#theme-dark:checked) matches the root while the Dark radio is checked, and sets color-scheme: dark there. While System is checked, neither rule matches, :root keeps light dark, and the system decides.
  • With the system set to dark and Light picked, the page’s background gets the light half, because the root’s own color-scheme is now light and the system no longer decides. The banner stays dark, because it sets its own.
  • From the keyboard, Tab moves focus to the checked radio, and the arrow keys check each radio in turn, switching the theme as you go. The site’s custom ring is only on the Borrow seeds buttons, so the radios keep the browser’s own focus ring, which follows the color scheme and stays visible in both themes.
  • The choice lasts only for the page you’re on, because remembering it across pages needs JavaScript or the server.

The Final Code

In index.html, the switch sits in the header, under the tagline:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The seed catalogue now follows each visitor’s system setting. Every color role uses light-dark() to hold a light and a dark value, and color-scheme on the root tells the browser that the page supports both.

00:12 Bea has two more wishes. She wants the Returns week banner to stay dark in both themes, so it stands out on the light page. And she wants a switch for visitors who’d like the other theme from their system’s.

00:25 Both come from a property you already have, color-scheme, set on the right element.

00:32 light-dark() doesn’t read the whole page’s setting. It reads the color scheme of the element that uses the color. And color-scheme inherits, so every element gets both schemes from the root, unless it sets its own.

00:45 So give the banner its own. At the top of the returns banner rule, set color-scheme to dark.

00:54 The page stays light, but the banner turns deep green. The banner’s background role now gives its dark half. But the words have almost disappeared. The text is still dark green.

01:07 The background is declared in the banner rule, so its role is resolved on the banner. The text color is declared only on the body. So the banner inherits the finished color the body picked in the light scheme, not the pair.

01:20 Point the banner’s color at the text role too, like its background.

01:26 Now the role is resolved on the banner itself, and the text turns pale.

01:32 Now for the switch. In the header of index.html, under the tagline, add a fieldset with the legend Theme. Inside it go three radio buttons with their labels: System, Light and Dark.

01:48 They share a name, so only one can be checked. The legend names the group. System starts checked. That keeps the system setting as the default.

01:59 Next, the CSS that listens to the radios. Here, :has() matches an element that contains whatever its parentheses describe. So the root matches whenever the Dark radio is checked, and this rule sets color-scheme to dark.

02:13 A second rule, under it, does the same for Light.

02:20 While System is checked, neither rule matches. The root keeps both schemes, and the system decides.

02:28 Click Dark, and the whole catalogue turns dark at once, even the radio buttons. Click System, and it follows your system again: light. Now turn on dark emulation in the Rendering panel, so the system asks for dark.

02:43 The page turns dark again. Click Light. Your turn: pause the video and predict which half the page gets, and which the banner gets.

02:58 The page gets the light half, the banner the dark. The root’s own color-scheme is now light, so the system no longer decides. The banner sets its own.

03:10 Last, try the switch the way a keyboard user would. Reload the page, and it’s back on System, still dark. Press Tab, and focus lands on the checked radio, System, with a ring around it.

03:23 The arrow keys move through the group, and each press checks the next radio. So the theme changes as you go. Light, then Dark. The ring stays visible in both themes.

03:35 The site’s custom ring is only on the Borrow seeds buttons. The radios keep the browser’s own ring, and it follows the color scheme too, dark on the light page and pale on the dark one.

03:48 Here’s the rule: light-dark() gives the half that matches the color scheme of the element using it. That’s the element’s own setting, or the one it inherits.

03:58 The banner stays dark, and visitors can pick a theme or leave it to their system. The choice lasts only for this page, because remembering it across pages needs JavaScript or the server.

04:10 Next, in Exercise: Theme the Seed Catalogue, you’ll give Bea’s whole catalogue both themes on your own, with the dark banner and the switch.

Become a Member to join the conversation.