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

Trace a Derived Token

The video picks up with the In This Issue list that the end of the previous lesson, Name Tokens by Role, had you add to your site.

In this video, Teo made the contents section denser by overriding a spacing token on the section, but the space between the list’s entries didn’t change. You traced where the browser fills in a var() inside a custom property, and then moved the derived token to the component that uses it. Here are the main points and the fixed rules.

Key Points

  • A var() inside a custom property is filled in on the element that declares the property. Elements below inherit the result, with the var() already replaced by a value.
  • --space-list: calc(var(--space-gap) * 2) declared on :root becomes calc(1rem * 2) there, and that’s what the list inherits. Nothing in it points at --space-gap anymore, so overriding --space-gap on .contents doesn’t change it, and the entries stay 32 pixels apart.
  • The Computed pane shows the real gap (row-gap: 32px), and the Styles pane shows that the override did reach the list.
  • A derived token follows an override only on its own element or on an element that contains it.
  • Declare a derived token in the component that uses it. Then it’s filled in with the value the component inherits, and the entries close up to 16 pixels. Moving the line is the fix. The new name, --poem-list-gap, only says that the token belongs to the list.

The Final Code

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 Your stylesheet for The Lantern Press now keeps its colors and spacing as tokens on the root element. Teo has put them to work on something new, an In This Issue list with four poems and their poets.

00:13 The list is a grid, and its gap property reads a token called --space-list. Teo declared it on the root, next to --space-gap, as --space-gap times two.

00:23 Teo’s idea is that the space between entries stays twice the space inside one. Inside each entry, the title and the poet sit in a row, with --space-gap between them.

00:34 On the page, that’s 16 pixels inside an entry and 32 pixels between entries.

00:42 Teo now wants the contents section denser, with half the spacing. With tokens, that looks like one line. In the components layer, add a rule for the contents section that sets --space-gap to --space-2, the half-rem step.

00:57 --space-gap is inherited, so Teo expects everything inside the section to tighten up. Your turn: pause the video and predict how far apart the entries are when you save.

01:14 Save. The entries are still 32 pixels apart. Only the title and the poet moved closer, to 8 pixels. Inspect the list, and open the Computed pane. The row-gap is 32 pixels, the same as before the change.

01:32 Switch to the Styles pane. The list’s gap reads the --space-list token. Further down, the contents rule sets --space-gap to --space-2, and the list inherits it.

01:42 So the override arrived. Why didn’t the list’s gap follow it?

01:49 The answer is where the browser fills in a var() reference. It does it on the element that declares the custom property. --space-list is declared on the root.

01:59 There, --space-gap is still 1rem, so the root fills it in, and --space-list becomes calc(1rem * 2). The list inherits calc(1rem * 2), and the root’s 1rem is already in place of the var() reference.

02:15 The contents section hands the list a --space-gap of 0.5rem. But nothing in --space-list points at --space-gap anymore, so --space-list can’t change.

02:26 So declare the derived token where it’s used. Move the line out of the root rule and into the poem list’s rule. That move is the whole fix. Then rename it --poem-list-gap.

02:38 The name changes nothing on the page. It tells anyone reading the stylesheet that the token belongs to the list. Last, point the list’s gap property at it.

02:49 Save, and the entries close up to 16 pixels apart. Now the list fills in --space-gap itself, with 0.5rem, which it inherits from the contents section.

03:01 Here’s the rule: a var() reference in a custom property is filled in on the element that declares it, and the elements below inherit the result.

03:11 So a token built from another one only follows an override made on its own element or on an element that contains it. Derive it in the component that uses it.

03:22 The contents section is now denser, and the rest of the page keeps its spacing. Next, in Exercise: Tokenize the Poem Card, you’ll restyle a poem card so it takes every color and space from the zine’s tokens.

Become a Member to join the conversation.