CyberStars

A Whole Theme in One Class

In a theatre, the same stage is a sunny beach in the first part and a dark forest in the second. Nobody builds a new stage. They change the lights and the picture at the back, and everything on stage looks different. Custom properties let a page change its whole look in the same way.

Custom properties follow the cascade

A custom property is a property like any other: a later or a stronger rule can give it another value. Then everything that uses it, through var(), takes the new value:

<style>
  /* show */
  html {
    --bg: #1d1d2b;
    --fg: #f5f5ff;
  }

  html.sunny {
    --bg: #fff6d6;
    --fg: #3b2a00;
  }
  /* /show */

  body {
    background-color: var(--bg);
    color: var(--fg);
    font-family: Verdana, sans-serif;
  }
</style>
<p>Tonight the moon is full.</p>

The page is dark, because <html> has no class. Now add the class to it in the HTML, <html class="sunny">, and look again: the page turns yellow, and the text dark brown.

html.sunny chooses the html element when it has the class sunny. It's more specific than html alone, so its values win. body didn't change at all: it still says var(--bg), and --bg now holds another color.

One class for the whole site

That's a theme: one class that gives every custom property a new value. With the colors of a whole site in custom properties, one class changes every page, every card, every heading. You never touch the rules that use them.

A theme should change every color that goes together. If the background turns light, the text must turn dark, or nobody can read it.

Break it on purpose

Put the class on <body> instead: <body class="sunny">, with the rule still html.sunny. Nothing changes: the rule looks for an html with the class, and there is none. Keep the theme's class and its rule on the same element. For a whole page, that's html, which holds everything else.

Then forget --fg in the sunny rule: a light yellow page, with the old light text on it. You can hardly read a word.


Mission: A light theme

What you will see: with <html lang="en" class="light">, the dashboard has a pale background with dark text, white cards, a dark orange title and blue headings. Without the class, the site is dark as before.

Some visitors find the dark site hard to read in daylight. The site gets a light theme, ready for the settings page to turn on later.

  • When <html> has the class light, the six colors get light values of your own.
  • That's a light page background with dark text, and cards whose background stands out from the page.
  • In the light theme, these are easy to read, with a contrast of at least 4.5: every paragraph of the dashboard's and the settings page's main part, the biggest headings of every page, the headings of the main part, the cards' numbers and the theme buttons.
  • Without the class, the site stays dark.

A light theme gives a new value to each of the six custom properties, the yellow too.