Skip to content

Advanced CSS ​

Use the CSS that shipped across evergreen browsers since 2023, and say in an interview what each feature replaced.

In this chapter: custom properties as a runtime API · perceptual colour with oklch · :has() · aspect-ratio · native nesting · subgrid

💡 The Core Idea ​

A cluster of features landed between 2022 and 2024. Together they removed most of the reasons to reach for a preprocessor or for JavaScript. Custom properties made CSS values dynamic. oklch made colour maths behave the way eyes do. :has() let a parent respond to its children. Subgrid let independent components share one set of tracks. The interview value is not naming them. It is saying what each one deleted, which is why it exists.

The question behind each of these is "what did people do before this?" Answer that and the feature explains itself.

⚠️ Moving target: the list of CSS features that are safe to ship only grows, and it grows every few months. Anchor positioning, @scope and container style queries are the next set to cross the line. The durable principle is the check, not the list. Confirm a feature's Baseline status (the MDN and web.dev label for "works in every major browser") before you rely on it. Wrap anything short of Baseline in @supports, so the fallback is a deliberate design, not a broken layout.

How It Works ​

Custom properties are a runtime API, not variables ​

A preprocessor variable is a constant that disappears at build time. A custom property lives in the DOM and inherits. It responds to media queries and pseudo-classes, and JavaScript can read and write it. That last point matters most for architecture. It is the only way to pass a value from JavaScript into CSS without writing inline style strings.

css
:root {
  --brand: #2563eb;
  --space: 1rem;
}

.card {
  /* Scoped override: only this subtree sees the larger value. */
  --space: 1.5rem;
  padding: var(--space);
  color: var(--brand, #000); /* second argument is the fallback */
}

/* The value responds to context, which a build-time variable cannot do. */
@media (prefers-color-scheme: dark) {
  :root { --brand: #60a5fa; }
}
typescript
const root: HTMLElement = document.documentElement;
root.style.setProperty('--brand', '#ef4444');
// Reading needs getComputedStyle — the property may be inherited rather than set here.
const current: string = getComputedStyle(root).getPropertyValue('--brand');

Colour that behaves the way eyes do ​

hsl is readable but not perceptually uniform. Equal numbers do not look equal: yellow at 50% lightness looks far brighter than blue at 50% lightness. So any ramp built by stepping HSL lightness looks uneven. Dark-mode inversion by flipping lightness also gives muddy results.

css
.button {
  --brand: oklch(60% 0.2 250);
  background: var(--brand);

  /* Derive states from one token instead of declaring five hex values. */
  border: 1px solid oklch(from var(--brand) calc(l - 0.15) c h);
}

.button:hover {
  background: color-mix(in oklch, var(--brand) 85%, white);
}
FunctionReach for it when
hsl()A human is picking one colour by hand
oklch()Design tokens, ramps, gradients — anywhere steps must look even
color-mix()Hover and active states derived from a single token
oklch(from …)Borders and shadows that must track a base colour

:has() — the parent selector ​

Twenty years of "CSS cannot do that" ended here. :has() styles an element based on what it contains or what follows it. That moves a whole category of logic out of JavaScript.

css
.card:has(img) { display: grid; grid-template-columns: 100px 1fr; }
label:has(input:invalid) { color: #dc2626; }
body:has(dialog[open]) { overflow: hidden; }
.list:not(:has(.item)) { display: none; }

Each of those replaced an effect that toggled a class. The last two are the most valuable. "Is the modal open" and "is the list empty" were state that React had to hold purely so CSS could see it.

⚠️ Browsers implement :has() with invalidation tracking (they re-check only the elements a change can affect), so it is production-safe. The one shape to avoid is an unqualified *:has(…) over a very large tree. There, the engine has no cheap way to narrow the set of elements to check.

aspect-ratio ​

css
/* ❌ The old hack: a percentage padding whose value nobody could read. */
.video { position: relative; padding-bottom: 56.25%; }

/* ✅ */
.video { aspect-ratio: 16 / 9; }
.avatar { aspect-ratio: 1; width: 48px; } /* height follows */

Reserving the ratio before the asset loads also stops the image from causing layout shift.

Native nesting ​

Nesting shipped natively in 2023. That removes one of the last standing arguments for a preprocessor.

css
.card {
  padding: 1rem;

  & .title { font-size: 1.25rem; }
  &:hover { background: #f9fafb; }

  @media (min-width: 768px) { padding: 2rem; }
}

The readability rule from Sass carries over unchanged: two or three levels, no more. Deep nesting produces long descendant selectors, and those are specificity you did not intend to create.

Subgrid ​

Picture three cards in a row, each with a title, body and footer of different lengths. Before subgrid, lining those up across the row meant measuring in JavaScript.

css
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto 1fr auto; /* title, body, footer */
}

.card {
  display: grid;
  grid-row: span 3;
  /* Adopt the parent's rows instead of creating new ones. */
  grid-template-rows: subgrid;
}

When to Use It ​

NeedChooseWhy
Runtime theming, or a value JavaScript setsCustom propertiesThe only bridge from JavaScript into CSS without inline styles
A colour ramp, or dark-mode inversionoklchEven steps in lightness look even
Styling a parent from its children's state:has()Removes state React held only so CSS could see it
Aligning rows across sibling componentsSubgridThe alternative is measuring in JavaScript
A value that never changesWrite the valueA custom property adds indirection for nothing
Deeply nested component stylesTwo levels, then a new classNesting depth becomes specificity

Common Mistakes ​

❌ Wrong — a custom property read as if it were a value:

typescript
// Empty string: inline styles do not include inherited custom properties.
const brand = document.documentElement.style.getPropertyValue('--brand');

✅ Right — read the computed value:

typescript
const brand = getComputedStyle(document.documentElement).getPropertyValue('--brand').trim();

❌ Wrong — treating custom properties as typed. By default a custom property is a token stream (untyped text). So --space: 1rem cannot be animated, and calc() silently accepts nonsense from it. @property declares a syntax and an initial value. That makes the property animatable and validated.

❌ Wrong — a ramp stepped in HSL. hsl(60 100% 50%) and hsl(240 100% 50%) claim the same lightness but differ hugely in perceived brightness. A palette generated that way needs correcting by hand at every step.

🔑 Key Takeaways ​

  • Custom properties live in the DOM, inherit, and respond to context. That makes them a runtime API, not build-time variables.
  • oklch is perceptually uniform, so equal steps in lightness look equal. That is what makes ramps and dark-mode inversion work.
  • :has() moves parent-from-child styling into CSS and deletes the class-toggling effects that used to do it.
  • Native nesting removes one of the last reasons to run a preprocessor, with the same depth discipline as before.
  • Subgrid aligns tracks across sibling components, replacing JavaScript measurement.

Interview Questions ​

Q: Why prefer oklch over hsl for design tokens?

Because HSL's lightness is not perceptual. Yellow and blue at the same HSL lightness look nothing alike, so a ramp stepped evenly looks uneven and needs correcting by hand. oklch lightness tracks perceived brightness. That makes generated ramps, dark-mode inversion by flipping lightness, and contrast reasoning all behave. It also reaches colours outside sRGB on wide-gamut displays.

Q: What did :has() let you delete?

The class-toggling layer. Think of body scroll locking when a dialog opens, or error styling on a field that contains an invalid input. Add empty-list states, and layout variants that depend on whether a card has an image. All of those were React state and effects that existed only to make child state visible to CSS.

Q: What is the difference between a custom property and a Sass variable?

Sass substitutes its variable at build time, so it does not exist in the shipped stylesheet. A custom property is a live DOM value. It inherits, and you can override it per subtree. It responds to media queries and pseudo-classes, and JavaScript can read and write it. Only the custom property can support runtime theming.

Q: When is a custom property the wrong tool?

For a value that never varies. Every var() is an indirection that a reader has to resolve, and a small amount of work for the engine. A token that is the same everywhere buys nothing for that cost. The test is whether anything ever changes it: a theme, a breakpoint, a subtree or a script.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.