CSS layout
Build resilient layouts with normal flow, intrinsic sizing, Flexbox, Grid, containing blocks, and overflow control.

WHAT YOU WILL BE ABLE TO DO
Learning outcomes
- Explain css layout in plain language.
- Connect the behavior to the underlying browser or framework model.
- Implement the core pattern and reason through edge cases.
- Answer common follow-ups without relying on memorized phrases.
01Overview
An element's layout mode — normal flow, flex, grid, or positioned — determines how its box is sized and how it participates in placing its children.
02Mental model
- Flexbox distributes items along one axis (row or column) — best for a toolbar, a nav, or centering content.
- Grid places items in two dimensions at once, with explicit rows and columns — best for page-level or card-grid layouts where both axes matter simultaneously.
- Positioned layout (absolute/fixed/sticky) removes an element from normal flow relative to its containing block — best for overlays, tooltips, and sticky headers.
03Examples
.layout { display: grid; grid-template-columns: 240px 1fr; grid-template-areas: "sidebar main";}.sidebar { grid-area: sidebar; }.main { grid-area: main; }04Check understanding
A page needs a fixed sidebar, a header, and a main content area, all at once. Flex or grid?
Grid — it's a two-dimensional layout problem (rows and columns simultaneously), which is exactly what grid-template-areas is built for; flex would need nested containers to fake the second axis.
DDConcept deep dives
Deep dive 1
Normal flow is the resilient baseline
Block boxes stack, inline content wraps, and content contributes to size before special layout is added. Keeping meaningful document order in normal flow gives small screens, zoom, missing CSS, and assistive navigation a stable foundation. Positioning everything absolutely discards those intrinsic relationships and turns content changes into collisions.
- Use margin or gap to express spacing instead of coordinate offsets.
- DOM order should normally match visual and keyboard order.
- Absolute positioning fits overlays and anchored decoration, not ordinary page structure.
Deep dive 2
Flexbox and Grid solve different dimensions
Flexbox distributes items along one main axis and handles wrapping line by line. Grid defines coordinated rows and columns, including intrinsic and flexible tracks. A page commonly uses Grid for major two-dimensional composition and Flexbox inside toolbars or card rows; choosing one does not exclude the other.
- Flex-basis participates in initial main-axis size before free space distribution.
- minmax(0, 1fr) lets a grid track shrink below an item's min-content width.
- auto-fit and auto-fill differ in whether empty repeated tracks collapse.
Deep dive 3
Intrinsic minimums explain many overflow bugs
Flex and grid items often refuse to shrink below their content-based automatic minimum. An unbroken URL, preformatted code, or wide child can therefore force a flexible layout beyond the viewport. Allow the item to shrink with min-width: 0 or minmax(0, 1fr), then apply wrapping or local scrolling to the actual content.
- Find the first element whose scroll width exceeds its client width.
- Avoid overflow-x: hidden on the page as a substitute for diagnosis.
- Test long translations, 200% text zoom, and narrow viewports.
.layout { display: grid; grid-template-columns: 16rem minmax(0, 1fr);}.article { min-width: 0; }.article pre { overflow-x: auto; }The page track may shrink, while the code block owns horizontal scrolling instead of forcing the entire document wider.
QAInterview questions and model answers
Attempt each answer aloud before opening it. The model answer shows the depth and precision expected in an interview; it is not a script to memorize.
Intermediate · Coding · 1 min · Question 1How do you choose between normal flow, Flexbox, and Grid?Open model answer
Model answer
Start with normal flow because block and inline layout already adapt to content. Use Flexbox for one-dimensional distribution and alignment, and Grid when rows and columns must coordinate. They compose rather than compete.
/* One axis: a toolbar */.toolbar { display: flex; gap: 12px; align-items: center; }
/* Two axes: a page shell */.page { display: grid; grid-template-columns: 240px 1fr; grid-template-rows: auto 1fr;}Intermediate · Coding · 1 min · Question 2Why does min-width: 0 often fix a grid or flex overflow?Open model answer
Model answer
Flex and grid items commonly have an automatic minimum size based on their content. That can prevent a track from shrinking even when defined with 1fr. min-width: 0 allows the item to shrink and lets its own overflow strategy apply.
.layout { display: grid; grid-template-columns: 240px 1fr; }
/* the 1fr track won't shrink below its content without this */.layout > .main { min-width: 0; }.layout > .main pre { overflow-x: auto; }Intermediate · Coding · 1 min · Question 3What creates a containing block?Open model answer
Model answer
The containing block depends on positioning and ancestor properties. Static and relative elements usually use a content box from their formatting context; absolute and fixed positioning follow specific ancestor rules, and transforms can establish containing blocks.
.card { position: relative; } /* becomes the containing block */.card .badge { position: absolute; top: 0; right: 0; }
/* transform also makes an ancestor the containing block for fixed children */.parent { transform: translateZ(0); }Advanced · Coding · 1 min · Question 4What is intrinsic sizing?Open model answer
Model answer
Intrinsic sizes come from content: min-content is roughly the smallest width without avoidable overflow, while max-content is the width the content prefers without wrapping. Functions such as minmax, fit-content, and clamp combine intrinsic constraints with available space.
.tag { width: max-content; } /* as wide as the text wants */.sidebar { width: fit-content; max-width: 300px; }.grid { grid-template-columns: repeat(auto-fill, minmax(min-content, 1fr)); }Intermediate · Coding · 1 min · Question 5What causes margin collapsing?Open model answer
Model answer
Adjacent vertical margins of block boxes in the same block formatting context can combine rather than add. Flex and grid item margins do not collapse. Using gap for component spacing avoids relying on collapse behavior.
p { margin: 16px 0; }/* two stacked <p> are separated by 16px, not 32px — the margins collapse */
.stack { display: flex; flex-direction: column; gap: 16px; }/* gap does not collapse — always exactly 16px */Intermediate · Coding · 1 min · Question 6How would you debug unexpected overflow?Open model answer
Model answer
Identify the overflowing element, inspect its scroll width and computed constraints, then check unbreakable content, intrinsic minimums, fixed widths, transforms, and viewport units. Hiding overflow before finding the cause can mask inaccessible content.
/* quick visual audit in devtools console */[...document.querySelectorAll('*')].forEach((el) => { if (el.scrollWidth > document.documentElement.clientWidth) console.log(el);});Advanced · Conceptual · 1 min · Question 7What establishes a new block formatting context?Open model answer
Model answer
Several properties do, including flow-root, certain overflow values, floats, positioned elements, and flex or grid items. A BFC contains floats and changes margin interactions.
Open question page →Advanced · Conceptual · 1 min · Question 8How does position: sticky choose its scroll container?Open model answer
Model answer
It sticks relative to the nearest ancestor with a relevant scrolling mechanism and requires an inset. Overflow ancestors commonly explain apparently broken sticky behavior.
Open question page →Advanced · Conceptual · 1 min · Question 9What is subgrid for?Open model answer
Model answer
It lets a nested grid use track sizing from its parent along an axis, aligning internal content across sibling components without duplicating track definitions.
Open question page →Intermediate · Conceptual · 1 min · Question 10Why can 100vw cause horizontal overflow?Open model answer
Model answer
Viewport width units may include the scrollbar gutter while the document's content area does not. Width: 100% is often safer for ordinary full-width blocks.
Open question page →SCScenario questions
Scenario 1
A two-column grid uses 240px 1fr, but a code block forces the whole page wider on mobile.
- Inspect the grid item's automatic minimum size.
- Use minmax(0, 1fr) or min-width: 0 on the content item.
- Give the code block local horizontal overflow.
- Collapse the outer grid at a content-driven breakpoint.
Reveal worked answer
The 1fr track still honors the item's min-content contribution. I would define the flexible track as minmax(0, 1fr), set min-width: 0 on the article, and let the pre element scroll within its own bounds. On small screens the sidebar should stack or disappear without hiding essential navigation.