Writing Clean CSS
Maintainable, scalable CSS through organization, naming conventions, and best practices.
How do I write clean, maintainable CSS?
Keep selectors short and single-class, avoid IDs and !important, use variables for repeated values, and group related properties in a consistent order. Delete unused rules regularly, and let a formatter (Prettier) and a linter (Stylelint) enforce the conventions so you don't have to.
Organized Structure
Use logical file organization, clear commenting, and consistent formatting for maintainable code.
Naming Conventions
Follow consistent naming patterns like BEM or functional CSS for clear, predictable class names.
Performance Focus
Optimize selectors, minimize specificity conflicts, and write efficient CSS for better performance.
Clean CSS in Action
Meaningful Class Names
/* Good */
.navigation-menu { }
.primary-button { }
.article-header { }Use descriptive names that explain purpose, not appearance
Consistent Formatting
.selector {
property: value;
another-property: value;
}Maintain consistent indentation and formatting
Organized Structure
/* Base styles */
/* Components */
/* Utilities */
/* Media queries */Group related styles and use clear comments
Avoid Deep Nesting
/* Good */
.nav-item { }
/* Avoid */
.header .nav .item .link { }Keep selectors simple and avoid over-specificity
Reusable Components
.btn { /* base button */ }
.btn--primary { /* variant */ }
.btn--large { /* modifier */ }Create modular, reusable component styles
Performance Best Practices
/* Minimize !important */
/* Use efficient selectors */
/* Reduce redundant code */Write efficient CSS for better performance
Code Editor
<button class="btn btn--primary">Save</button>
<button class="btn">Cancel</button>Preview
Clean CSS Principles
- Use meaningful, descriptive class names
- Follow consistent naming conventions (BEM, OOCSS)
- Group related styles together
- Use CSS custom properties for consistency
- Minimize nesting and specificity
Organization Tips
- Structure files by components or sections
- Comment your code clearly and consistently
- Use consistent indentation and formatting
- Remove unused CSS regularly
- Validate your CSS with tools
Clean CSS — step by step
Follow a consistent naming convention
Pick a naming convention like BEM, SMACSS, or utility-first and stick to it. Consistent names make stylesheets predictable and easy to navigate.
🎯 Quick Check
Which tool helps find CSS rules that are never applied to your page?
Writing Clean CSS: summary
Key points
- Prefer layout tools (
gap, Flexbox, Grid) over margin hacks and magic numbers. - Comment the why, not the what: explain non-obvious values like
z-index: 30. - Use logical properties (
margin-inline,padding-block) for direction-agnostic styles. - Remove styles you no longer use; DevTools' Coverage tab shows unused CSS.
Frequently asked questions
›What is Stylelint?
Stylelint is a linter for CSS that catches errors and enforces conventions, such as banning IDs or requiring a consistent property order, in your editor and CI.