Sub topic 3
Naming things so styles stay predictable
This chapter assumes you know CSS selectors, specificity and how to link a stylesheet to a page. It also assumes you have written enough CSS to have been confused by one of your own old class names. The next chapter covers BEM in detail. This chapter covers the principles behind any naming convention, so you can judge whether one fits your team.
Why names matter more as a codebase grows
CSS has no modules, no types and no compiler checks for class names. A class name is the only contract between your HTML and your stylesheet. If the name is vague, three things go wrong:
- Developers cannot tell what a class does without reading its rules.
- They cannot tell what is safe to delete, because any template might use the name.
- They reuse a name for a second purpose, and the two sets of rules collide.
A naming convention is a set of rules that makes names predictable. Given a piece of UI, two developers should arrive at the same name, and given a name, they should be able to guess what it styles and where its rules live.
Name by purpose, not by appearance
A class named after its look breaks when the look changes. Suppose a team ships this:
.red-text {
color: #c00;
}
.big-box {
padding: 2rem;
}Later, the brand colour for errors changes to orange. Now .red-text styles text orange, or every template must be edited. Either way the name is wrong. Name the role the element plays instead:
.error-message {
color: #c00;
}
.promo-panel {
padding: 2rem;
}A role name survives a redesign because only the rules change, not the markup.
| Appearance-based name | Purpose-based name | Why the second holds up |
|---|---|---|
| `.red-text` | `.error-message` | The colour can change without a rename. |
| `.left-column` | `.sidebar` | The sidebar can move to the right. |
| `.big-box` | `.promo-panel` | The size is an implementation detail. |
| `.bold-link` | `.nav-link--current` | The name says why it is bold. |
The exception is utility classes, which are deliberately presentational (.p-0, .text-center). They are a separate, clearly marked category, covered in a later chapter. The rule is not "never describe appearance". It is "never describe appearance in a name that claims to be a component".
Name by what it is, not where it sits
Descendant selectors such as .sidebar ul li a tie your CSS to the exact HTML structure. Renaming a wrapper or swapping a ul for a nav breaks the styles without any warning. Giving each styled part its own class removes that dependency:
<nav class="site-nav">
<ul class="site-nav__list">
<li class="site-nav__item">
<a class="site-nav__link" href="/docs">Docs</a>
</li>
</ul>
</nav>.site-nav__link {
color: var(--color-text);
text-decoration: none;
}The selector is one class, so its specificity is the same as every other rule in the file. That is the connection to the previous chapter: flat, single-class selectors keep specificity low, and good names are what make flat selectors possible. A selector like .site-nav__link also names its owner, so a reader knows which component to open.
Use prefixes to show a class's job
On a large project, a bare class like .header could mean a page header, a card header or a table header. Prefixes (also called namespaces) add a one-letter or short category to the name. The exact set varies by team. This one is common:
| Prefix | Meaning | Example |
|---|---|---|
| `c-` | A component | `.c-card` |
| `l-` | A layout structure | `.l-grid` |
| `u-` | A utility with one job | `.u-hidden` |
| `is-`, `has-` | A state set by script or the server | `.is-open`, `.has-error` |
| `js-` | A hook for JavaScript only, never styled | `.js-menu-toggle` |
Prefixes do two things. They make the category visible in the markup, so a reviewer sees u-hidden and knows it is a blunt override. They also make search and linting easier, because you can grep for every u- class or enforce a pattern.
Pick prefixes once and write them down. A project that uses c-card in some files and card in others has the cost of prefixes without the benefit.
Keep JavaScript hooks separate from style hooks
If JavaScript selects elements by the same classes that CSS styles, a designer who renames .c-menu__button for tidiness silently breaks the behaviour. The fix is a dedicated hook class that carries no styles:
<button class="c-menu__button js-menu-toggle" type="button">
Menu
</button>const toggle = document.querySelector(".js-menu-toggle");
const menu = document.querySelector(".c-menu");
toggle.addEventListener("click", () => {
menu.classList.toggle("is-open");
});The CSS side of that state looks like this:
.c-menu {
display: none;
}
.c-menu.is-open {
display: block;
}Now each class has one owner. Designers own c- classes, developers own js- classes, and is-open is the agreed contract between them. Data attributes such as data-menu-toggle serve the same purpose and are a good alternative for hooks.
Note that .c-menu.is-open raises specificity to two classes. That is acceptable for a state rule, but it is why many teams keep state selectors short and consistent.
Name states and variants differently
A state is temporary and set at runtime (is-open, is-loading, has-error). A variant is a fixed alternative chosen when the markup is written (a danger button, a featured card). Mixing the two in one naming style makes it unclear who is responsible for the class.
A practical split:
- Variants attach to the component name, for example
.c-button--danger. - States use
is-orhas-and are toggled by scripts. - Neither describes appearance:
--dangerand not--red.
Formatting rules that remove arguments
Formatting choices are arbitrary, but inconsistency is costly. Choose once and enforce it with a linter, which a later chapter covers.
- Use lowercase letters and hyphens. HTML class matching in standards mode is case-sensitive, so
.Cardand.cardare different classes, and one typo costs an hour. - Avoid underscores and camelCase unless a convention such as BEM or CSS Modules requires them.
- Keep names between one and four words. A name longer than that usually means the component is doing too much.
- Never use IDs for styling. An ID selector has far higher specificity than a class and cannot be reused on a page.
- Do not encode the element type into the name, such as
.div-wrapperor.ul-list. The tag can change.
Name custom properties with the same discipline
Custom properties (the --name variables) are part of your public vocabulary, and the same principles apply. A token named for its value breaks the way .red-text does:
:root {
/* Fragile: the name is a value */
--blue: #0b5fff;
/* Durable: the name is a role */
--color-accent: #0b5fff;
--color-text: #1a1a1a;
--space-2: 0.5rem;
--space-4: 1rem;
}If the accent becomes green, --blue: green is a lie that every reader must remember. A category prefix (color-, space-, font-) groups tokens and makes autocomplete useful. Design tokens get a full chapter later, but the naming rule starts here.
A decision flow for a new class name
When you add a class, a short checklist keeps the answers consistent.
Common errors
- Renaming for looks. Changing
.btn-blueto.btn-greenin the CSS but not in 40 templates. Name by role from the start. - Over-generic names.
.title,.itemand.contentwill collide. Scope them with the component name. - Long descendant chains.
.page .main .list li ais a name for a position, not a thing. Give the link its own class. - Prefixes used inconsistently. A half-adopted convention is harder to read than none.
- Styling
js-classes. Once a hook carries styles, nobody dares remove it. - Abbreviations only the author understands.
.hdr-bx-ltsaves four characters and costs every future reader a lookup. Prefer.page-header.
Exercises
Exercise
A team has the classes .blue-button, .left-nav and .small-text. Rename each by purpose, and explain the risk in keeping the original names.
Show answer
For example, .primary-button (or .c-button--primary), .site-nav (or .sidebar) and .caption (or .u-text-sm if it is a utility). The original names describe appearance or position. When the brand colour, layout or type scale changes, the name becomes false, and the team must either live with a misleading name or edit every template that uses it.
Exercise
A script uses document.querySelector(".c-menu__button") to attach a click handler. A designer renames the class to .c-menu__toggle. What breaks, and how do you prevent it?
Show answer
The handler no longer finds the element, so the menu stops working with no CSS error. Prevent it by giving the element a separate js- hook class, such as js-menu-toggle, that carries no styles. Then the style class can be renamed freely without affecting behaviour.
Exercise
Why is --blue: #0b5fff a worse custom property name than --color-accent: #0b5fff?
Show answer
--blue names the value, so if the accent colour changes, the name becomes wrong or must be renamed everywhere it is used. --color-accent names the role and belongs to the color- category, so the value can change in one place without touching any rule that uses it.