css · level 3 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Utility classes do one job each, and live in the last layer.
Components like .card style a whole thing. A does one small job: .u-flush removes margins and .u-muted softens text. You add one in the HTML when a single spot needs a tweak, instead of writing a new rule.
A utility must always win, or it is useless. Put utilities in the last cascade layer, after components. Then .u-flush beats .card .title with no ids and no heavier selectors.
Keep each utility tiny and build it from tokens, like .u-gap-3 reading var(--space-3).
<div class="card">
<h2 class="title u-flush">Holiday plans</h2>
<p>Booked for August.</p>
</div> @layer components, utilities;
@layer components {
.card .title {
margin: 16px 0;
}
}
@layer utilities {
.u-flush {
margin: 0;
}
} Press show my page to see it here.
guess first
One class against two. Who wins the colour?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<div class="card">
<p class="note u-muted">Last updated today.</p>
</div> @layer components, utilities;
@layer components {
.card .note {
color: #111827;
}
}
@layer utilities {
.u-muted {
color: #6b7280;
}
}
your turn
Your turn. Write a stack utility.
Fill in the blanks: put .u-stack in the utilities layer, and give it a gap from the --space-3 token.
the page you’re styling
<div class="u-stack">
<p>Step one</p>
<p>Step two</p>
</div>your css
Press show my page to see it here.
- the items run down the pagenot checked yet
- the gap comes from
--space-3not checked yet - the utility lives in the last layernot checked yet
fix it
The utility loses to the card. Can you fix it?
the page you’re styling
<div class="card">
<p class="u-flush">No margin here, please.</p>
</div>your css
Press show my page to see it here.
- the utility removes the marginnot checked yet
- Both rules are in the same layer, so weight decides. Weigh each selector.
.card pweighs a class and a tag, more than.u-flush. The utility needs its own layer.- Change the second
@layer componentsto@layer utilities, then press run.
nailed it
you gave your system its finishing tools. :D
- A utility class does one small job.
- Keep utilities in the last layer so they always win.
- Build utilities from tokens, never raw values.
new words in your collection
- utility class
This lesson isn’t saved. A free account remembers every lesson you finish.