CSS layout mastery

Flexbox, Grid, stacking contexts, containing blocks, the cascade and specificity. "Why is z-index not working" separates users from understanders.

About 28 min read3-question knowledge check

The mental model

CSS is not a drawing API. It is a constraint solver. You do not say "put this box at x=340"; you declare relationships — "this column takes the remaining space", "these items sit on one line and shrink before they wrap", "this element is positioned relative to whichever ancestor claimed the job" — and the engine solves for a layout that satisfies them at the current viewport, font size, and content length.

The analogy that sticks: a spreadsheet. You do not type computed totals into cells; you type formulas and the sheet recalculates when inputs change. flex: 1, minmax(200px, 1fr), and clamp(1rem, 2.5vw, 1.5rem) are formulas. A layout built from pixel values is a spreadsheet full of hard-coded numbers: correct once, wrong forever after.

What breaks without this model is the daily frustration of the job. "Why is z-index: 9999 not working" is never about z-index; it is about **stacking contexts** — the element is winning inside a context that itself loses. "Why is my position: fixed modal clipped inside a card" is never about fixed; it is about **containing blocks** — an ancestor with a transform quietly became the reference frame. "Why does overflow: hidden on a wrapper break my sticky header" is about the scroll container sticky positioning needs. These are not quirks. They are three rules, each stated plainly in the spec, that produce hundreds of bug reports because most engineers learned CSS by pattern-matching Stack Overflow answers instead of learning the three rules.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision