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 thecolor-schemeof the element that uses the color, and gives the matching half.color-schemeinherits, so every element getslight darkfrom:rootunless it sets its own. An element’s owncolor-schemewins over the one it would inherit.color-scheme: darkon.returns-bannermakes 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-coloris declared in its own rule, so it follows the banner’s scheme. Itscolorwas 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 setscolor: 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 setscolor-scheme: darkthere. While System is checked, neither rule matches,:rootkeepslight 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-schemeis nowlightand 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: