CSS Architecture & Standards
These standards establish a maintainable CSS architecture that prevents specificity collisions and ensures scalable, resilient stylesheets.
Class Naming Conventions
To ensure that the relationship between markup and styles is immediately predictable, teams should select one of the following methodologies MAY. Choose one approach per project and maintain consistency across all components without mixing patterns.
1. BEM (Block, Element, Modifier)
Classes are segmented into blocks, constituent elements, and state or visual modifiers:
.card {} /* Block: Standalone component */
.card__title {} /* Element: Component constituent */
.card--featured {} /* Modifier: Visual variation */
.l-container {} /* Layout prefix */
.u-text-center {} /* Utility prefix */
.visually-hidden {} /* Visually hidden utility */2. Native CSS Nesting
The block carries a single class name, while child elements are styled through structural child selectors:
.card {
& > .title {}
& > .body {}
}Nesting Depth Constraint
When using native nesting, restrict hierarchy depth to a maximum of 3 levels. Deeper nesting inflates selector specificity and impedes code maintainability.
Naming & Selector Principles
- JavaScript Hooks via
data-*Attributes MUSTCD-R03 - DOM queries in JavaScript must target
data-*attributes (e.g.,data-accordion-trigger) rather than styling classes, preventing visual style updates from breaking functional behavior. - Check:
CD-03 - Scope state classes to their component MUSTCD-R04
- If a project uses state classes, never style them in isolation—always scope them to the component (e.g.,
.accordion.is-open, never.is-open). A bare state class leaks into every other component that uses it. - Check:
CD-04 - Express State via
data-*Attributes SHOULDCD-R05 - Manage dynamic state changes using attribute selectors like
[data-state="open"]or[data-invalid]rather than classes such as.is-openor.has-error. Isolating state management from the class namespace guarantees that visual refactoring cannot silently break behavioral scripts. - Check:
CD-05 - Consistent Prefixing
- Prefix layout-oriented classes with
.l-*and single-responsibility utilities with.u-*. - Semantic Class Names
- Names must reflect functional roles rather than presentation (e.g.,
.button--primary, not.button--green).
<div class="accordion" data-state="closed">
<button class="accordion__trigger" data-accordion-trigger>Toggle Accordion</button>
<div class="accordion__panel">Panel Content</div>
</div>.accordion[data-state='open'] .accordion__panel {
display: block;
}Design Tokens & CSS Custom Properties
Primitive values for colours, spacing, radii, font sizes, and transition durations MUST be derived from custom properties declared in the base layer. Raw hexadecimal literals must never appear directly inside component stylesheets.
@layer base {
:root {
--color-bg: oklch(100% 0 0);
--color-fg: oklch(20% 0.01 264);
--color-accent: oklch(55% 0.12 152);
--space-xs: 0.25rem;
--space-md: 1rem;
--radius-md: 0.5rem;
--duration-fast: 150ms;
}
}Tokens must use semantic names (--color-bg, --color-danger) rather than literal descriptions (--color-white, --color-red), ensuring durability when theming or introducing dark mode.
Colour Specification (oklch Function)
The oklch() color function is the default for color declarations MUST. Palette tokens in the base layer must be expressed using oklch() rather than HEX or rgb().
/* Syntax: oklch(<lightness> <chroma> <hue> / <alpha>) */
oklch(58% 0.12 152) /* Opaque */
oklch(58% 0.12 152 / 60%) /* With 60% alpha */Rationale for oklch
- 1. Perceptual Uniformity
- Adjusting the lightness parameter changes perceived visual brightness evenly. Unlike HEX and RGB, where identical numerical steps yield wildly differing perceptual shifts,
oklch()enables reliable contrast calculations. - 2. Predictable State Colors
- Hover and disabled variants can be derived systematically by adjusting lightness and chroma values without altering hue.
- 3. Wide Color Gamut (Display P3)
- Accesses vibrant, saturated colors available on modern screens that cannot be represented within the standard sRGB gamut.
Note: Fully supported across all modern browsers (Baseline); legacy fallbacks are unnecessary.
CSS Units & Responsive Behavior
Unit selection directly influences how layouts adapt to user zoom preferences and default browser font sizes.
| Use Case | Recommended Unit | Responsive & Accessibility Benefits |
|---|---|---|
| Font Size | rem (Never px) | Automatically scales with user-defined browser and operating system font size preferences. |
| Spacing (Margin / Padding) | rem (via spacing tokens) | Scales proportionally with typography, preserving layout balance across all zoom levels. |
| Borders & Dividers | px | Borders should remain crisp and uniform regardless of font size scaling SHOULD. |
| Media Queries | em (e.g., 48em) | Breakpoints adapt based on the user's base font size, ensuring layouts trigger based on content capacity rather than rigid device widths. |
| Line Height | Unitless (e.g., 1.7) | Multiplies against the element's computed font size, maintaining balanced spacing in nested text structures. |
| Container Queries | cqw, cqh, cqi, cqb | Scales relative to the component's immediate container rather than the viewport, enabling truly modular layouts across sidebars and main content areas. |
Additional Styling Rules
- Use Logical Properties SHOULDCD-R06
- Favor
margin-inline,padding-block, andinset-inline-startto support multi-directional writing modes (RTL and vertical text). - Check:
CD-06 - Avoid Fixed Heights
- Do not apply fixed
heightvalues to text containers; content must expand gracefully when font sizes or translations increase. - Container Context Requirement
- Container query units (
cqw, etc.) require an ancestor element declaringcontainer-type: inline-size.
Responsive Architecture
- Mobile-First Foundation
- Construct layouts starting with compact viewports, progressively enhancing via
min-widthmedia queries. - Tokenized Breakpoints
- Centralize breakpoint definitions within tokens rather than scattering magic numbers across files.
- Prioritize Intrinsic Web Design
- Leverage modern fluid layout techniques (
repeat(auto-fit, minmax(..., 1fr)),flex-wrap: wrap, andclamp()) before introducing rigid media query breakpoints.
Motion & Animation
- Constrain Animated Properties
- Animate only GPU-accelerated properties (
transformandopacity). Animating layout-triggering properties (width,height,top,margin) induces costly reflows. - Respect Reduced Motion MUSTCD-R07
- All animations and transitions must provide reduced-motion alternatives via
prefers-reduced-motion: reduce. - Check:
CD-07
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}Cascade Layers (@layer) for Specificity Management
To systematically eliminate specificity conflicts and explicitly define rule precedence, stylesheets are organized into cascade layers:
@layer reset, base, layout, components, utilities;| Layer | Purpose & Responsibility | Examples |
|---|---|---|
reset | Normalization and margin resets | box-sizing: border-box, universal resets |
base | Design tokens and HTML element defaults | :root custom properties, default typography styles |
layout | Page-level layout scaffolds | .l-container, .l-grid |
components | Encapsulated, reusable UI blocks | .card, .site-header |
utilities | Single-purpose utility overrides | .visually-hidden, .u-text-center |
Architectural Constraints
- Component Encapsulation MUSTCD-R08
- Components must style their internal structures while leaving outer positioning (margins, coordinates, grid placement) to the parent container.
- Check:
CD-08 - No ID Selectors or Arbitrary
!importantMUSTCD-R09 - ID selectors (
#main) introduce excessive specificity. The!importantdeclaration is strictly prohibited outside theutilitieslayer. - Check:
CD-09
/* Incorrect: ID selector creates insurmountable specificity */
#main .card { padding: 1rem; }
/* Incorrect: !important masks architectural issues */
.card { padding: 1rem !important; }
/* Correct: Defined within the appropriate cascade layer */
@layer components {
.card { padding: 1rem; }
}- Limit Selector Nesting (Maximum 3 Levels)
/* Incorrect: 4+ levels of nesting degrades readability */
.card {
& .body {
& .list {
& .item {}
}
}
}
/* Correct: Keep nesting under 3 levels; assign classes to deep elements */
.card {
& .body {
& .card__item {}
}
}Dark Theme Implementation
When a dark theme is required, build with light mode as the default and implement dark mode strictly by re-declaring tokens. Component rules must remain untouched.
:root {
color-scheme: light;
--color-bg: oklch(100% 0 0);
--color-fg: oklch(20% 0.01 264);
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme='light']) {
color-scheme: dark;
--color-bg: oklch(20% 0.01 264);
--color-fg: oklch(92% 0.005 264);
}
}
:root[data-theme='dark'] {
color-scheme: dark;
--color-bg: oklch(20% 0.01 264);
--color-fg: oklch(92% 0.005 264);
}- Define Default Tokens MUSTCD-R10
- Every color property must have a value on the root
:root. - Check:
CD-10 - Declare
color-schemeMUSTCD-R11 - Synchronizes browser-native form elements and scrollbars.
- Check:
CD-11 - Verify Contrast
- Ensure both themes satisfy accessibility contrast requirements (minimum 4.5:1).
Methodology Flexibility
The @layer and BEM architecture detailed here represents our recommended baseline to facilitate seamless team handovers.
Projects MAY adopt alternative methodologies (such as CUBE CSS, ITCSS, or utility-first frameworks like Tailwind CSS) where project requirements dictate. However, the chosen approach must be fully documented and applied consistently across the codebase. Arbitrary mixing of disparate CSS paradigms within a single project is strictly prohibited.