Name Tokens by Role

Every rule in The Lantern Press’s stylesheet now has a layer, and the cascade between them is settled. The values inside those rules are another story. Teo’s dark brown, #2b2118, is typed out in three places, and the spacing is a scatter of 1rem, 1.5rem, 2rem, and 4rem. When Teo picks the spring issue’s colors, they’ll have to find every copy.

In this lesson, you’ll replace those raw values with design tokens, custom properties that hold the site’s design decisions, organized in three tiers. You’ll be able to say which tier a component should read and why. Start by giving each raw value a name.

Collect the Raw Values as Primitives

A value typed in five rules has to be changed in all of them, and one copy is always missed.

You already know the fix from custom properties. Declare the value once on :root, the <html> element, and read it with var(). Custom properties inherit, so every element on the page can read one declared there.

The first tier holds the raw values themselves. These are primitive tokens, named after what the value is, such as a shade of ink or a step of spacing.

You’ll add them to the issue page’s styles.css, picking up where Nest Layers and Roll Back With revert-layer left off. These are the same files you’ve been editing with your editor’s live preview, and they’re also in the course’s code download. It’s not the exercise’s stylesheet, so .issue-note and the base layer’s .masthead are there. Add this rule at the top of its @layer base block:

Language: CSS
  :root {
    /* Primitives */
    --ink-900: #2b2118;
    --paper-50: #fbf7f0;
    --brick-700: #9b2226;
    --space-4: 1rem;
    --space-6: 1.5rem;
    --space-8: 2rem;
    --space-16: 4rem;
    --text-1: 1rem;
    --text-2: 1.25rem;
    --text-3: 1.5625rem;
    --font-serif: Georgia, serif;
  }

The spacing and type tokens are scales like the ones you’ve used before, each a short list of steps to choose from instead of a new value each time. The spacing steps count quarter rems, so --space-4 is four quarters, or 1rem. The type steps grow by a factor of 1.25. The page uses only the first so far. A scale lists every step you allow, including ones that no rule uses yet.

Why does :root go in the base layer? Custom properties take part in the cascade like any other declaration, so the tokens need a layer too. Nothing else in the stylesheet sets these tokens, so no layer has to beat them, and a separate tokens layer would only lengthen the order statement. The base layer already holds the page-wide defaults, so it’s also a fitting place for the tokens.

Save, and nothing changes, because no rule reads the tokens yet.

Name Tokens by Role

You could now write color: var(--ink-900) everywhere #2b2118 was. But look at what that brown does on the page. It’s the color of the text, and it’s also the background of the masthead. If Teo wants a dark green masthead for spring, changing --ink-900 turns the text green too. The primitive says what the value is, not what it’s for.

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

Become a Member to join the conversation.