Sub topic 3
Naming things so styles stay predictable
This chapter assumes you know CSS selectors, specificity and the cascade, and that you can link a stylesheet to an HTML page. It covers how to name classes so that other people can predict what a style does before they open the file.
Why names matter in global CSS
Every class name lives in one global namespace. If two developers each write .title, the later rule in source order wins for both. A name is therefore a contract: it tells readers what the class is for and where its styles apply.
Good names do three jobs:
- They avoid collisions with classes written elsewhere.
- They show what a class does without a search through the codebase.
- They let you change the design without renaming classes in the markup.
Name by purpose, not appearance
A class named for how something looks breaks when the design changes. A class named for what the element is stays correct.
/* Fragile: the name describes the current look */
.red-text {
color: #c00;
}
/* Stable: the name describes the role */
.error-message {
color: #c00;
}If the brand later moves errors to orange, .error-message still tells the truth. .red-text would now be orange, and the name would mislead everyone.
Do the same with location. .sidebar-button fails the moment the button moves to a footer. Prefer .button, and use a modifier or a parent component for the context.
Avoid names that are too generic
Names like .title, .wrapper or .item will collide as the codebase grows. Add the owning component to the name, such as .card-title or .nav-item. The next chapter shows a formal structure for this pattern, BEM.
Pick one format and keep it
Choose a single case style and apply it everywhere. Lowercase with hyphens (kebab-case) is the usual choice, because CSS is case-insensitive for property names but class matching in HTML can be case-sensitive in standards mode.
| Pattern | Example | Use |
|---|---|---|
| Component name | `.search-form` | A reusable piece of UI |
| State | `.is-open`, `.has-error` | A condition that changes at runtime |
| JavaScript hook | `.js-toggle` | Behaviour only, never styled |
| Namespace prefix | `.c-card`, `.u-hidden` | Marks the kind of class |
Separate styling from behaviour
If JavaScript selects .card, then restyling the card can break the script. Give scripts their own js- classes and never write CSS rules for them.
<button class="button js-menu-toggle" type="button">Menu</button>The button class carries the styles. The js-menu-toggle class is only a hook for code, so designers can rename button freely.
Keep selectors tied to the class
Names only help if the selectors use them. Rules written against IDs or deep element chains have higher specificity and depend on markup structure.
/* Hard to override and tied to the HTML layout */
#sidebar ul li a {
color: #0b5fff;
}
/* Easy to override and independent of structure */
.nav-link {
color: #0b5fff;
}The second rule has the lowest useful specificity, one class. Anything that needs to override it can do so with a later rule of the same weight.
Common errors
- Mixing
camelCase,snake_caseandkebab-casein the same project. - Reusing a generic name in two files and getting silent overrides.
- Styling
js-classes, which ties behaviour to appearance. - Encoding values in names, such as
.mt-16inside a component, when a token would do.
Exercise
A team has a class .blue-box used for notices, and the design now makes notices green. Which name should replace it, and why?
Show answer
Use a role-based name such as .notice. It describes what the element is, so it stays correct when the colour changes again. To avoid a break, add .notice beside .blue-box first, move the styles, then remove the old class.