Share reusable values using scope and inheritance.
A design token is a name for a reusable design decision, such as an accent color, card spacing, or corner radius. Reusing that name keeps related elements consistent.
In this lesson, CSS custom properties represent those decisions in a stylesheet. A custom property is the CSS mechanism; it becomes a useful token when its name communicates a design role. The Design Tokens Community Group format specification describes a format for sharing tokens between tools, but here you will work directly in CSS.
Start with three tokens:
:root {
--color-accent: green;
--space-card: 1rem;
--radius-card: 0.5rem;
}The names describe their purpose, rather than their current literal value:
--color-accent: The accent used by related interface elements.--space-card: The spacing used inside a card.--radius-card: The card's corner radius.For example, --color-accent can later change from green to purple without making its name misleading. A name such as --green describes the current value rather than its role.
Change a shared value in one place to update every declaration that uses it. For example, changing --space-card updates the spacing wherever var(--space-card) appears.
Consider two theme panels:
:root {
--color-accent: green;
--space-card: 1rem;
--radius-card: 0.5rem;
}
.card {
color: var(--color-accent);
padding: var(--space-card);
border: 2px solid var(--color-accent);
border-radius: var(--radius-card);
}Both cards use the root values: green text and borders, 1rem of padding, and 0.5rem corner radii.
The component describes how to use each decision. The token definitions supply the actual values. This separation is useful because the same .card rule can work in more than one theme without repeating its properties.
Add a local override:
.panel.featured {
--color-accent: purple;
}Only the featured card gets purple text and a purple border. Its padding and radius stay the same:
--color-accent value.The card rule stays reusable because it refers to the token. There is no need to repeat the text and border declarations for the featured version.
Follow color: var(--color-accent) for each card:
--color-accent is inherited from :root, so var() supplies green.--color-accent: purple declaration. That declaration wins on the panel and is inherited by its card, so var() supplies purple there.The .card rule does not need to change. The value changes because var() reads the custom property available at each card's position in the document tree.
Why can one .card become purple even though both cards match the same .card rule? Explain the roles of inheritance and var(); do not just say that the featured panel has a different color.
A custom property applies to the element where you define it and can pass down to its children. It does not pass up to parents or across to siblings.
If several declarations set the same custom property on the same element, the cascade chooses the value. A value declared directly on a child also takes precedence over one it would otherwise inherit.
That gives you a useful pattern:
:root.Try it yourself
HTML + CSS
-- values.