Skip to content
Tech Interview Prep
Technical interview guide

CSS Fundamentals and Layout

How the box model, cascade, flexbox, grid, positioning, responsive design, and units decide what a browser actually renders, and how to debug when they don't.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: CSS Living Standard, current to 2025. Baseline browser support as of Chrome 131, Firefox 133, Safari 18 (late 2024/early 2025)..

Overview

Curated: · Written: · Reviewed:

CSS Fundamentals and Layout

In one line

CSS resolves the cascade to one computed value per property, then lays each box out inside its containing block. Use flex when one axis sets the size, grid when rows and columns must align, and positioning only for overlays.

Key definitions

  • Containing block: the box that absolute, fixed and percentage sizes resolve against. For absolute, it is the nearest ancestor with a position other than static.
  • Stacking context: the scope where z-index values compete. Created by a positioned element with z-index, opacity below 1, transform, filter, will-change, isolation: isolate or contain: paint.
  • Block formatting context (BFC): an isolated region that contains floats and stops child margins collapsing through it. Created by display: flow-root and by any overflow other than visible.
  • Specificity: the (a,b,c) score, compared after origin and layer. :where() adds nothing; :is() takes its most specific argument.
  • Cascade layer: an @layer group. For normal declarations a later layer beats an earlier one regardless of specificity, and unlayered styles beat all layers.

Formulas and complexity

Wouter=w+pL+pR+bL+bR(content-box)W_{\text{outer}} = w + p_L + p_R + b_L + b_R \quad \text{(content-box)} Wcontent=w−pL−pR−bL−bR(border-box)W_{\text{content}} = w - p_L - p_R - b_L - b_R \quad \text{(border-box)} shrinki=overflow⋅fi bi∑jfj bj\text{shrink}_i = \text{overflow} \cdot \frac{f_i \, b_i}{\sum_j f_j \, b_j}

Here $f_i$ is flex-shrink and $b_i$ is the flex base size. Numbers to quote:

  • Content-box card, width 200px, padding 20px, border 5px: rendered width 250px. A child with width: 50% is 100px, and padding: 10% resolves against the 200px containing block to 20px per side, so the child renders at 140px.
  • Flex row 500px, bases 300/200/300, flex-shrink 0/1/2. Overflow is 300px. Weighted factors are 200 and 600, so the items give up 75px and 225px. Rendered widths: 300, 125, 75.
  • repeat(auto-fit, minmax(280px, 1fr)) in 1200px with a 24px gap, four cards: each track is (1200 − 72) / 4 = 282px. Three cards: the empty fourth track collapses with its gutter, leaving 3 tracks and 2 gaps, so each is (1200 − 48) / 3 = 384px.
  • Three 100px columns, grid-auto-columns: 50px, 10px gap, child on lines 4 to 6: it covers two implicit 50px tracks, so the board is 300 + 100 + 4 × 10 = 440px. A child on lines 2 to 4 covers tracks 2 and 3: 100 + 10 + 100 = 210px.
  • Root 16px, 1.25rem is 20px. Inside it, 1.5em is 30px, then 0.8em is 24px. A label with padding-left: 0.5em resolves against its own 24px font-size: 12px.

How it works

The cascade sorts declarations by origin and importance, then layer, specificity and source order, and stops at the first tie-breaker that differs. The winner becomes the computed value. Layout then places each box inside its containing block.

Declarations Origin and importance Cascade layers Specificity Source order Computed value Layout in containing block

Non-inherited properties such as width, margin and border-width take their initial values, so a child gets a 0px margin unless a rule targets it. Inherited properties such as color and font-size flow down. Margins between in-flow block siblings collapse to the larger value. Flex and grid items, floats and absolutely positioned boxes never collapse.

When to use and when not to

NeedReach forAvoid
One axis, content sets sizeFlexGrid for a single row
Two axes, aligned tracksGridNested flex faking alignment
Reusable card in varied slotsContainer query on a wrapperViewport media query
Page shell changes (sidebar to drawer)Media queryContainer query
Element anchored to a positioned parentabsolutefixed inside a transformed ancestor
Text wraps around an imageFloatPage layout with floats

Traps interviewers probe

  1. z-index: 9999 loses to a sibling. The value only competes inside its stacking context, and an ancestor with opacity, transform, filter or will-change traps it. Fix: raise the ancestor, or remove the property that created the context.
  2. A flex child overflows its container. A flex item's min-width: auto equals its min-content size, so a long unbreakable word blocks shrinking. Fix: min-width: 0 on the item, or an overflow other than visible.
  3. A child's margin pushes its parent down. The child's top margin collapses through the parent. Fix: padding or a border on the parent, display: flow-root, or make the parent a flex or grid container.
  4. A 100vh hero hides its button on iOS. vh uses the largest viewport, so the bottom sits behind the URL bar. Fix: 100svh for a hero that must stay put on first paint.
  5. A sticky sidebar in a flex row never sticks. Flex items stretch to the row height, so the sidebar has no room to move. Fix: align-self: flex-start on the sidebar.

Interview tips

Opening: name the step that decides the answer, such as the cascade layer, the containing block or the stacking context, then give the arithmetic.

Expect these follow-ups:

  • Why does auto-fit differ from auto-fill? (auto-fit collapses empty tracks so the remaining items stretch.)
  • Why does a user's !important beat yours? (Important declarations reverse layer order, so the user origin wins.)
  • Why does a container query not match its own element? (It queries the nearest ancestor container.)
  • What does overflow: hidden do beyond clipping? (It creates a scroll container and a BFC, which stops margin collapse.)

30-second outline:

  1. Name the mechanism: cascade order, then layout in the containing block.
  2. Give one number, such as 250px for the content-box card.
  3. State the fix and the rule behind it.
  4. Name the trade-off: what the other choice would break.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.