← Back to HTML & CSS, page 9

html-css-0173 · Medium mid

How would you build a small utility class layer on top of design tokens without specificity problems?

Define tokens as custom properties, declare a layer order such as base, components, utilities, and write single-purpose classes in the last layer that use token values. Layer order lets utilities override components without !important, and a small scale keeps the set consistent.

Full answer

A utility class does one job, such as p-2 for padding or text-muted for a colour. A small set built on tokens gives speed without a large framework, and it can sit beside component styles.

Steps:

  1. Define tokens as custom properties: a spacing scale --space-1: 4px, --space-2: 8px, and colours with names by role.
  2. Declare the layer order once at the top: @layer reset, base, components, utilities;. Later layers win over earlier layers, whatever the specificity of the selectors, so utilities override components without !important and without long selectors.
  3. Write each utility as one class with a single property and a token value: .p-2 { padding: var(--space-2); }. Keep the set small and consistent. Generate repetitive ones, such as a spacing scale, from a list.
  4. Components set their normal look in the components layer, and the markup adds utilities for the exceptions.

Rules that keep it healthy:

  • Use utilities for layout and spacing exceptions, and keep component classes for repeated, meaningful parts. If the same ten utilities repeat across many places, make a component.
  • Do not add a utility for every possible value. A limited scale is the point.
  • Do not use !important inside utilities when layers are available.
  • Unlayered styles beat all layered styles, so keep third-party and old CSS in an early layer, or inside a named layer, to avoid surprises.
  • Document the scale so teams use the same names.

Compared with a full utility framework, a small layer gives less coverage, but it is easy to understand, needs no build step and uses the project's own tokens.

The first card has 16px padding from the component. The second has 8px padding and a muted colour, because the utilities layer comes after the components layer. The utility selectors have the same specificity as .card, and they still win by layer order.

Declare the order before any rules, so it does not depend on the order of the files.

html
<style>
  @layer base, components, utilities;
  :root { --space-1: 4px; --space-2: 8px; --space-4: 16px; --color-muted: #595959; }
  @layer components { .card { padding: var(--space-4); border: 1px solid #999; color: #111; } }
  @layer utilities {
    .p-2 { padding: var(--space-2); }
    .text-muted { color: var(--color-muted); }
    .mt-1 { margin-top: var(--space-1); }
  }
</style>
<div class="card" id="a">Default card</div>
<div class="card p-2 text-muted mt-1" id="b">Card with utilities</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Tokens give utilities their values.
  • Layer order beats specificity.
  • Each utility does one thing from a limited scale.
  • Unlayered styles beat layered ones.
  • Repeated utility groups should become components.

Common mistakes

  • Adding !important to utilities when a layer order would solve the problem.
  • Creating a utility for every possible value instead of a limited scale.
  • Pasting the same long list of utilities everywhere instead of making a component.

Follow-ups

  • Why do unlayered styles beat layered ones?
  • When would you turn a group of utilities into a component?

Tips

  • Describe the three ingredients: tokens, a layer order and single-purpose classes.
  • Say why the utilities win without extra specificity.