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,fixedand percentage sizes resolve against. Forabsolute, it is the nearest ancestor with apositionother thanstatic. - Stacking context: the scope where
z-indexvalues compete. Created by a positioned element withz-index,opacitybelow 1,transform,filter,will-change,isolation: isolateorcontain: paint. - Block formatting context (BFC): an isolated region that contains floats and stops child margins collapsing through it. Created by
display: flow-rootand by anyoverflowother thanvisible. - Specificity: the (a,b,c) score, compared after origin and layer.
:where()adds nothing;:is()takes its most specific argument. - Cascade layer: an
@layergroup. For normal declarations a later layer beats an earlier one regardless of specificity, and unlayered styles beat all layers.
Formulas and complexity
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, andpadding: 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-shrink0/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.25remis 20px. Inside it,1.5emis 30px, then0.8emis 24px. A label withpadding-left: 0.5emresolves 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.
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
| Need | Reach for | Avoid |
|---|---|---|
| One axis, content sets size | Flex | Grid for a single row |
| Two axes, aligned tracks | Grid | Nested flex faking alignment |
| Reusable card in varied slots | Container query on a wrapper | Viewport media query |
| Page shell changes (sidebar to drawer) | Media query | Container query |
| Element anchored to a positioned parent | absolute | fixed inside a transformed ancestor |
| Text wraps around an image | Float | Page layout with floats |
Traps interviewers probe
z-index: 9999loses to a sibling. The value only competes inside its stacking context, and an ancestor withopacity,transform,filterorwill-changetraps it. Fix: raise the ancestor, or remove the property that created the context.- A flex child overflows its container. A flex item's
min-width: autoequals its min-content size, so a long unbreakable word blocks shrinking. Fix:min-width: 0on the item, or anoverflowother thanvisible. - 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. - A 100vh hero hides its button on iOS.
vhuses the largest viewport, so the bottom sits behind the URL bar. Fix:100svhfor a hero that must stay put on first paint. - 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-starton 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-fitdiffer fromauto-fill? (auto-fitcollapses empty tracks so the remaining items stretch.) - Why does a user's
!importantbeat 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: hiddendo beyond clipping? (It creates a scroll container and a BFC, which stops margin collapse.)
30-second outline:
- Name the mechanism: cascade order, then layout in the containing block.
- Give one number, such as 250px for the content-box card.
- State the fix and the rule behind it.
- 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.