Sub topic 10
Utility classes and when to use them
This chapter assumes you know CSS selectors and specificity, have read the earlier chapters on BEM and @layer, and can edit HTML and CSS files.
What a utility class is
A utility class does one job. .p-0 sets padding: 0. .text-center sets text-align: center. The class name describes the effect, not the thing it is applied to.
BEM classes describe what an element is (.card__title). Utility classes describe what an element looks like for one property (.mt-4). Both are useful, and they solve different problems.
Why teams use them
- A utility is reusable. You write
.mt-4once and use it on any element. - A utility does not grow the stylesheet. Reusing existing classes adds no new CSS, where a new component class adds rules.
- A utility is a small, local change. You can adjust one element's spacing without opening the component's stylesheet.
The trade-offs
- Long
classattributes. A heavily styled element can carry ten or more classes, which makes markup harder to read. - Repeated combinations. If five buttons each list the same eight utilities, a change means editing five places. A component class handles this better.
- Weaker meaning.
.flex .gap-2says how an element is laid out, not what it is. - Override fights. A utility is meant to win over a component rule. If it does not, developers reach for
!important.
The last point is where earlier chapters apply. Utilities need to beat component styles reliably, and cascade layers make that possible.
Combining utilities with components
Put utilities in the last layer. Later layers win regardless of specificity, so a utility overrides a component rule without !important.
@layer reset, base, components, utilities;
@layer components {
.card {
padding: 1rem;
border: 1px solid #ddd;
}
}
@layer utilities {
.p-0 { padding: 0; }
.mt-4 { margin-top: 1rem; }
}<article class="card p-0 mt-4">
<h2 class="card__title">Release notes</h2>
</article>Here .card and .p-0 have the same specificity. Without layers, whichever rule came later in the file would win. With layers, .p-0 wins because utilities is declared after components. Moving the rules around in the file does not change the result.
Build utilities from tokens
Do not hard-code values in utilities. Use the design tokens from the earlier chapter so utilities and components share one scale.
:root {
--space-2: 0.5rem;
--space-4: 1rem;
}
@layer utilities {
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
}If the spacing scale changes, the utilities change with it. Name utilities after the token step (mt-4 for --space-4), not the pixel value, so names stay valid after a rescale.
Deciding when to use one
Use a utility when:
- the change is a single property, such as spacing, alignment or visibility;
- the change is a layout adjustment specific to one place;
- the value comes from your token scale.
Use a component class when:
- the same set of styles repeats on several elements;
- the styles express what the element is, such as a card or a button;
- the styles include states, pseudo-elements or media queries that utilities handle poorly.
A practical rule is to keep the utility set small. Offer spacing, display, text alignment and visibility. Each extra utility is another name to learn and another rule to keep consistent.
Common mistakes
- Putting utilities outside a layer. Unlayered styles beat all layered styles, so an unlayered component rule would override your utilities.
- Using utilities to rebuild a component inline. Extract a class when a combination repeats.
- Adding
!importantto utilities as a precaution. Layers already provide the priority, so it only makes later overrides harder. - Creating a utility for every value. Generate them from a fixed scale instead.
Exercise
A .card rule in the components layer sets padding: 1rem. You add .p-0 in the utilities layer, but the card keeps its padding. The .card rule is the only one that sets padding. What is the most likely cause, and how do you fix it?
Show answer
The .card rule is probably declared outside any layer, since unlayered rules beat layered ones. Move it into @layer components (or the correct layer is not listed in the @layer order statement). Then .p-0, in the later utilities layer, wins without !important.