Codeskill

Learn to code, step by step

CSS Best Practices: Writing Clean, Maintainable Code

Habits for writing CSS that stays readable, maintainable, and performant – organisation, naming, DRY principles, responsiveness, and testing.

1. Structure and organisation

A tidy stylesheet is easier to navigate and update.

Best practices:

  • Consistent naming: Pick a convention (BEM, OOCSS, or your own) and stick to it.
  • Group related rules: Keep typography together, layout together, and so on.

Example

/* Typography */
h1, h2, h3 {
    font-family: Arial, sans-serif;
}

/* Layout */
.container {
    max-width: 1200px;
    margin: 0 auto;
}

2. Use comments wisely

Comments explain why something exists, not what every line does.

Best practices:

  • Mark sections: Label blocks of related rules.
  • Document workarounds: If a rule exists to fix a browser bug, say so.

Example

/* Fix for IE11 flexbox alignment issue */
.ie-flex-fix {
    display: -ms-flexbox;
    display: flex;
}

3. DRY (Don’t repeat yourself)

Repeated values are harder to maintain and easier to get wrong.

Best practices:

  • Use variables: CSS custom properties work well for colours, fonts, and spacing.
  • Preprocessor helpers: Mixins and functions in SASS reduce duplication further.

Example

:root {
    --primary-color: #4CAF50;
}

.button {
    background-color: var(--primary-color);
}

.link {
    color: var(--primary-color);
}

4. Responsive design from the start

Plan for different screen sizes early rather than bolting responsiveness on later.

Best practices:

  • Relative units: ems, rems, and percentages scale better than fixed pixels.
  • Media queries: Define breakpoints for layout changes.

Example

.container {
    width: 90%;
    max-width: 1200px;
    margin: 0 auto;
}

@media (min-width: 768px) {
    .container {
        width: 80%;
    }
}

5. Use frameworks wisely

Frameworks speed things up but come with trade-offs.

Best practices:

  • Know the framework: Understand its grid, components, and limitations.
  • Override when needed: Your design may require custom rules on top.

Example

<!-- Bootstrap with custom styles -->
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="custom.css">

6. Prioritise performance

Smaller, simpler CSS loads faster and renders quicker.

Best practices:

  • Minify for production: Strip whitespace and comments from deployed files.
  • Avoid deep selectors: Overly specific rules are harder to override and slower to match.

Example

/* Instead of */
nav ul li a {
    color: blue;
}

/* Use */
.nav-link {
    color: blue;
}

7. Accessibility

CSS should support accessible experiences, not undermine them.

Best practices:

  • Semantic HTML first: Good markup gives CSS a solid base.
  • Sufficient contrast: Text must be readable against its background.

Example

.high-contrast-text {
    color: black;
    background-color: white;
}

8. Testing and cross-browser compatibility

Test in more than one browser before you ship.

Best practices:

  • Regular testing: Dev tools and services like BrowserStack catch differences early.
  • Autoprefixer: Adds vendor prefixes automatically in most build pipelines.

Example

/* Before Autoprefixer */
.grid-container {
    display: -ms-grid;
    display: grid;
}

/* After Autoprefixer */
.grid-container {
    display: grid;
}

9. Keep learning and refactoring

CSS changes over time. Old code can usually be simplified.

Best practices:

  • Stay informed: Follow spec updates and browser release notes.
  • Refactor old styles: Replace hacks with modern properties when support allows.

Clean CSS is easier to read, update, and hand off. These habits are worth building early – they pay off on every project.

PreviousThe Future of CSS: Emerging Trends and Techniques