Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

CSS
CSS
0/10 completed
Beginner
Selectors and propertiesColors and fontsThe box modelDisplay and positioning
Intermediate
FlexboxCSS GridResponsive design
Advanced
Transitions and animationsCSS variables and architecture
Pro
Modern CSS: :has(), container queries and layers
Tracks / CSS / Advanced

CSS variables and architecture

Small sites can get away with messy CSS. Big ones can't: thousands of rules, many developers, and redesigns. This lesson covers custom properties (CSS variables) and the architecture habits that keep CSS maintainable.

Custom properties

HTML · CSS · live preview
Preview
  • Define them with --name: value;, usually on :root (the <html> element), so they're global.
  • Use them with var(--name), or var(--name, fallback).
  • Unlike Sass variables, they're live: they cascade, inherit, change in media queries, and JavaScript can update them.

Theming: dark mode in a few lines

HTML · CSS · live preview
Preview

This website uses exactly this technique for its light and dark modes.

Component-scoped variables

Variables can be set on a component to create variants without repeating rules:

HTML · CSS · live preview
Preview

Design tokens

Professional teams define a design system as tokens: a limited set of colours, spacing steps, font sizes, radii and shadows. Developers pick from the scale instead of inventing new values.

css
:root {
  /* spacing scale */
  --space-1: 4px;  --space-2: 8px;  --space-3: 12px; --space-4: 16px; --space-6: 24px; --space-8: 32px;
  /* type scale */
  --text-sm: 0.875rem; --text-base: 1rem; --text-lg: 1.25rem; --text-xl: 1.5rem; --text-2xl: 2rem;
  /* semantic colours */
  --color-surface: #fff; --color-text: #0f172a; --color-primary: #4f46e5; --color-danger: #dc2626;
}

Naming: the BEM convention

BEM (Block, Element, Modifier) gives every class a predictable name and keeps specificity flat:

css
.card { }                 /* Block: a standalone component */
.card__title { }          /* Element: part of the block */
.card__image { }
.card--featured { }       /* Modifier: a variant */
html
<article class="card card--featured">
  <img class="card__image" src="…" alt="…">
  <h3 class="card__title">…</h3>
</article>

Architecture rules that scale

  1. Keep specificity low and flat. Prefer single classes. Avoid ids and long chains like .page .sidebar ul li a.
  2. Avoid !important, except in utility classes or when overriding third-party CSS.
  3. Organise by layer, from generic to specific:
styles/
  1-settings/   tokens.css (variables)
  2-base/       reset.css, typography.css (element selectors)
  3-layout/     container.css, grid.css
  4-components/ button.css, card.css, navbar.css
  5-utilities/  .sr-only, .mt-4, .text-center
  1. Use a modern reset: box-sizing: border-box, margin: 0, and img { display: block; max-width: 100% }.

Popular approaches in the industry

Approach Idea Examples
BEM + plain CSS Naming conventions Many agencies and design systems
CSS Modules Class names scoped per component file automatically React and Vite projects
Utility-first Small single-purpose classes in the HTML Tailwind CSS
CSS-in-JS Styles written in JavaScript components styled-components, Emotion
Preprocessors Variables, nesting and mixins compiled to CSS Sass/SCSS

Native CSS now has variables, color-mix(), nesting and cascade layers, so much of what Sass did is built in.

css
/* Native CSS nesting */
.card {
  padding: 16px;
  & h3 { margin: 0; }
  &:hover { box-shadow: var(--shadow); }
  @media (min-width: 768px) { padding: 24px; }
}

Practice

Refactor this CSS to use variables for every repeated value, add a .btn--danger modifier that only changes a variable, and add a dark theme with [data-theme="dark"].

HTML · CSS · live preview
Preview
✎ Check your understanding

1.How do you use a custom property named --gap?

2.In BEM, what does card--featured represent?

← Transitions and animations