Coding Standards

CSS Coding Standards

Foundational

The full reference for writing maintainable CSS: scope, specificity, naming, design tokens, units, layout, responsiveness, theming, accessibility, performance, and animation. Without discipline, CSS turns into piles of overrides and fights over !important. With it, the UI stays consistent, themeable, and easy to change. Pairs with the HTML & Markup, React, and Blazor standards and with Accessibility.

CSS's main hazards are global scope and cascading specificity. A rule meant for one place affects another, and "fixing" it with more specific selectors and !important makes the next change harder. The fix is discipline: scope styles to components, keep specificity flat, drive values from shared tokens, and build responsive and accessible by default.

We prefer component-scoped styles (CSS modules in React, scoped .razor.css in Blazor) plus a small set of global tokens. Whatever method a project uses, apply it consistently. Prettier and Stylelint enforce the mechanics.

Architecture & scope

Specificity & selectors

Specificity war, magic values, !important #sidebar div.menu ul li a.item { color: #3b82f6 !important; }
.item { padding: 13px; } /* magic number, overridden anyway */

A deep, ID-based selector plus !important means the next person can only override it with something even worse. The magic padding and hard-coded colour are not tokenised. This is how CSS becomes unmaintainable.

Flat, scoped, tokenised .menu-item { /* single class, scoped to component */
color: var(--color-link);
padding: var(--space-2);
}
.menu-item:focus-visible { outline: 2px solid var(--color-focus); }

Low specificity (easy to override), values from tokens (themeable and consistent), and a visible focus style for accessibility. Maintainable and correct.

Naming & methodology

Design tokens & values

Units & sizing

Layout & responsiveness

Theming, typography & dark mode

Accessibility & performance

Self-review checklist

Why it matters: Unmanaged CSS goes bad fast. Specificity fights, piles of !important, and magic numbers make the UI fragile and slow to change, and careless CSS quietly breaks accessibility. Scoped, token-driven, low-specificity, accessible-by-default CSS keeps the interface consistent, themeable, and maintainable as the product and team grow.