Organize CSS So You Can Find Things Later
Split styles by layout, components and utilities, name classes so they mean something, and leave comments that help the next edit. After this, a six-month-old stylesheet is still navigable.
Course content
Sign in to save your progress.
-
Layout Without Float Hacks
-
Flexbox for Real Rows and Columns
-
CSS Grid When Flexbox Isn't Enough
-
-
-
Pages That Survive Phones
-
Responsive Layout With Media Queries That Make Sense
-
Mobile-First Without Building Everything Twice
-
Images and Typography That Scale Without Breaking
-
-
-
Components You Can Reuse
-
Navigation That Still Works on a Narrow Screen
-
Cards, Buttons and Shared Styles Across Pages
-
Custom Properties (CSS Variables) You Won't Regret
-
-
-
Forms and States That Look Finished
-
Focus, Hover and Disabled States People Can See
-
Accessible Forms: Labels, Errors and Keyboard Paths
-
-
-
Keep Layouts From Fighting You
-
Fix Overlap, Overflow and Z-Index Without Guessing
-
Organize CSS So You Can Find Things Later
-
-
- Final Check: HTML and CSS Intermediate
Skip ahead: If your CSS is already split into base, layout, components and utilities with names that mean something, skip to "Comments that help the next edit", then continue.
A 2,000-line styles.css with no sections is a trap. You will re-declare .btn three times and wonder which one wins. Organisation is not bureaucracy. It is how you find the rule you need before the cascade finds you.
A simple split that scales past one page
- Base — reset/box-sizing, typography, links, forms defaults.
- Layout — page shells, grids, headers, footers.
- Components —
.card,.btn,.field,.site-nav. - Utilities — rare helpers (
.u-sr-only,.u-hidden) if you need them. - Tokens —
:rootcustom properties first.
One file with clear comment banners is fine at this level. Multiple files come when the team or the build step justifies them.
/* === tokens === */
:root { --color-brand: #034bff; }
/* === base === */
*, *::before, *::after { box-sizing: border-box; }
/* === layout === */
.page { display: grid; ... }
/* === components === */
.card { ... }
.btn { ... }
Names that mean something
.cardbeats.box1..site-navbeats.n.- State:
.is-open,.field--error— pick one pattern.
If a class describes a page (homeSidebarLeft), it will not survive a redesign. Describe the role.
Comments that help the next edit
/* Sticky header height is also used in scroll-padding-top (see base). */
.site-header { position: sticky; top: 0; height: 3.5rem; }
Comment the why, not the what. /* set colour to blue */ wastes space. /* matches brand token; do not hard-code hex here */ saves a future mistake.
⚠ Warning: Do not invent a full methodology name if the team is two people and one site. Consistency beats ceremony.
Common mistakes
- Alphabetical classes with no grouping. Related rules live far apart.
- !important to "win" across sections. Fix order and specificity instead.
- Dead CSS left forever. If you delete a component from HTML, delete its rules.
A naming smoke test
Ask: “If I see .x7 in HTML, can I guess what it styles?” If not, rename it. Short is good. Cryptic is not.
Specificity budget
Prefer .card over .page .content .wrapper .card. Deep chains make overrides painful. If you need to win a fight, fix the conflicting rule or the order of the stylesheet sections — do not start a !important collection.
Dead code pass
When you delete a section from HTML, search the class names in CSS and remove orphans. Dead rules confuse the next reader into thinking they still matter. A five-minute pass after each feature keeps the file honest.
What happens if you skip this
Every change feels risky. You duplicate rules because you cannot find the original. The cascade looks random. It is not. Your file is.
Try it
Open your current stylesheet. Add three section banners. Move one duplicated button rule into .btn. Delete the orphans. Reload.
If you cannot find the rule that styles the checkout button in under a minute, what will you do under a deadline?
About
Split styles by layout, components and utilities, name classes so they mean something, and leave comments that help the next edit. After this, a six-month-old stylesheet is still navigable.
- Duration
- 5 min
- Last update
- 08/11/2026
Comments (0)
Sign in to commentNo comments yet.