Every stylesheet starts clean. Maintainable CSS stays that way. This is a checklist to review before you ship, and every month afterwards.
The checklist
- Selectors are short. One or two classes. No IDs, no long descendant chains. See Avoiding Specificity Problems.
- No
!importantexcept for deliberate utilities and accessibility overrides. - Names mean something. Purpose over appearance. See Naming Classes.
- Values come from variables. No same hex code typed in 20 places.
- No magic numbers. If
margin-top: 37pxis there to nudge something into place, find out why. A comment is better than a mystery. - Layout uses layout tools. Flexbox and grid, not margins and absolute positions pushing things around.
- Sizes are flexible.
max-width,rem,%,fr. Fixed widths and heights only when a design truly needs them. - Components are independent. A card does not rely on being inside
.sidebarto look right. - Responsive and accessible by default. Visible focus, enough contrast, reduced motion.
- Dead code is removed. If you comment something out “just in case”, delete it. The version history remembers.
Comment the surprising things
Comments are for why, not for what:
/* Not "make it flex". Instead: */
.nav { display: flex; }
/* The login button must stay inside the 44px touch target
required by the mobile design, so the padding is bigger than it looks */
.nav .login { padding: 14px 20px; }
Refactor as you go
When you touch a rule, tidy it. Replace a hard-coded color with a variable. Shorten a selector. Rename a vague class. Small steps are easier than a rewrite.
Test in more than one situation
- Narrow phone and wide desktop.
- Browser zoom at 200%.
- Longer text than your design assumed (translations are often 30% longer).
- Keyboard-only navigation.
- Dark mode, if supported.
Exercise: review this rule
div#main .content .box p.text { color: #d94a00; margin-top: 37px; } What would you change?
Show a possible solution
- Reduce the selector to a single class, such as
.box__text. 2. Move the color to a variable (var(--color-primary)). 3. Replace37pxwith a value from your spacing scale, or find out why it is needed. 4. Check that the component does not depend on#main.