skip to content

Box Model and Flow

How a single element computes its size and how boxes stack in normal flow. Interviewers open with box-sizing and then push into margin collapsing and formatting contexts, because those rules explain most "my layout jumped" bugs.

part ofCSSoverview, primer and where to startread it →
on this pageshow

explore

questions

30

In CSS, an element has `width: 100px`, `padding: 20px`, and `border: 2px solid`. How wide does it actually render under the default `box-sizing`, and what changes if you set `box-sizing: border-box`?

level: juniorimportance: must knowfreq 88%

answer

  1. which box does width describe
  2. padding and border sit outside
  3. 100 + 40 + 4
  4. border-box eats the padding inward
  5. margin is outside either way

basics

~20 s

Under the default box-sizing: content-box, width sizes only the content box, so 100px width plus 20px padding on each side plus 2px borders renders 144px wide. With box-sizing: border-box the box stays 100px and the content area shrinks to 56px.

solid answer

~40 s

Every element is four nested boxes: content, padding, border, margin. The initial value of `box-sizing` is `content-box`, which means `width` and `height` set the **content** box only, and padding and border are added outside it. So `width: 100px` with `padding: 20px` and `border: 2px` paints 100 + 40 + 4 = **144px** of visible box. Switching to `box-sizing: border-box` makes `width` describe the border box instead: the element paints exactly **100px**, and the content area is squeezed to 100 − 40 − 4 = **56px**. Margin is outside both models and is never included in `width` either way. Border-box is what most people expect intuitively, which is why nearly every codebase applies it globally — it makes `width: 100%` on a padded element stop overflowing its parent.

code

css · 15 lines
css
.content-box-demo {
  box-sizing: content-box; /* initial value */
  width: 100px;
  padding: 20px;
  border: 2px solid #333;
  /* painted width: 100 + 40 + 4 = 144px */
}

.border-box-demo {
  box-sizing: border-box;
  width: 100px;
  padding: 20px;
  border: 2px solid #333;
  /* painted width: 100px; content box: 56px */
}

go deeper

for a junior

Be ready to do the arithmetic out loud: 100px width, 20px padding each side, 2px borders gives 144px painted, and border-box makes it 100px with a 56px content area. Say plainly that margin is never part of width.

for a middle

Explain that box-sizing chooses which box width, height, and the min/max pair describe, and that background paints under padding and border. Mention that the content box floors at zero when padding exceeds the declared width.

for a senior

Show you know why the global border-box reset exists: percentage widths plus padding overflow under content-box, and the reset carries zero specificity so components can still opt out per element.

for a principal

Own the tradeoff of a project-wide sizing convention: a global reset is a cross-cutting contract that third-party widgets authored for content-box may violate, so decide up front whether it ships unscoped or inside a cascade layer components can escape.

## The four boxes Every element in CSS generates a rectangular box built from four nested layers, from the inside out: - **content box** — where text, child boxes, or a replaced element's pixels live; - **padding box** — content plus `padding`, transparent but painted with the element's background (the initial `background-clip` is `border-box`, so backgrounds cover padding and sit under the border); - **border box** — padding box plus `border-width`; this is the element's visible edge; - **margin box** — border box plus `margin`; always transparent, and it is the space the element reserves from its neighbours rather than part of the element itself. ## What `width` actually sets The question every interviewer is really asking is: *which of those boxes does `width` describe?* The answer depends on one property, `box-sizing`, whose initial value is `content-box`. Under `content-box`, `width` and `height` set the **content** box, and padding and border are laid on top: ```css .card { width: 100px; padding: 20px; border: 2px solid; } /* content 100 + padding 20+20 + border 2+2 = 144px painted */ ``` So the rendered element is 144px wide and, if the height were also declared, 144px tall by the same arithmetic. Nothing was ignored — `width: 100px` was honoured exactly, it just described the innermost box, not the one you see. ## What `border-box` changes `box-sizing: border-box` redefines `width` and `height` to describe the **border** box. The declared length now includes padding and border, and the content box is whatever is left over: ```css .card { box-sizing: border-box; width: 100px; padding: 20px; border: 2px solid; } /* painted box = 100px; content = 100 - 40 - 4 = 56px */ ``` The same redefinition applies to `min-width`, `max-width`, `min-height`, and `max-height` — they all measure whichever box `box-sizing` names, which is easy to forget when you mix a `max-width` with generous padding. What `border-box` does **not** change: - **Margin is still outside.** An element with `width: 100%` and a positive margin still overflows its parent horizontally; `border-box` only absorbed padding and border. - **Overflow still happens.** If the content needs more room than the shrunken content box gives it, it overflows or wraps as usual; `border-box` shrinks the content area, it does not shrink the content. - **`outline` and `box-shadow` are still outside layout entirely.** Neither takes part in the box model, so neither is included in `width` under either value. ## The degenerate case With `border-box`, padding and border can exceed the declared width. The used content width is floored at zero rather than going negative, and the element then paints wider than you asked for: `width: 100px; padding: 60px` yields a 0px content box inside a 120px border box. This is the one situation where `border-box` fails to hold the promise its name suggests. ## Why teams flip it globally The practical pain of `content-box` shows up the moment percentages meet padding. A three-column row of `width: 33.333%` items, each with `padding: 0 16px`, sums to 100% *plus* 96px, so it overflows or wraps. Under `border-box` the padding is carved out of the 33.333% and the row fits. That is why the standard reset appears near the top of almost every stylesheet: ```css *, *::before, *::after { box-sizing: border-box; } ``` It is deliberately unopinionated — the universal selector contributes zero specificity, so any later rule can override it per element. ## A little history Old versions of Internet Explorer, and any page rendered in quirks mode, sized boxes the `border-box` way. The standards-mode CSS box model chose `content-box`, and the ensuing decade of frustration is exactly why `box-sizing` was added and why the community converged on switching it back. The property takes only two values today, `content-box` and `border-box`; both have been supported everywhere for many years, so using the reset needs no fallback. ## How to answer it out loud State which box `width` describes under each value, do the arithmetic (144 vs 100 with a 56px content area), and add one sentence that margin is outside both. That is the complete answer — the follow-up is usually about the global reset or about percentages.

  • Does `box-sizing: border-box` also affect `min-width` and `max-width`?
    Yes. `box-sizing` governs which box every one of `width`, `height`, `min-width`, `max-width`, `min-height`, and `max-height` measures. Under `border-box`, a `max-width: 320px` caps the border box, so padding and border are counted inside that cap rather than pushing the element past it — a common surprise when a capped element with heavy padding looks narrower than expected.
  • With `border-box`, what happens if padding and border together exceed the declared width?
    The used content width is floored at zero instead of going negative, and the border box grows past the declared value. `width: 100px` with `padding: 60px` gives a 0px content box inside a 120px painted box. So `border-box` guarantees the width only while padding plus border fits inside it.
  • Does `box-sizing` have any effect on `outline` or `box-shadow`?
    No. Neither participates in the box model or in layout at all — they paint outside the border box without reserving space, so no `box-sizing` value includes them. That is why an `outline` on a focused element never shifts the layout, while adding a `border` on focus does.

Think of shipping a mug: the mug is the content box, the bubble wrap is padding, the cardboard is the border, and the gap you leave between parcels on a shelf is margin. content-box measures the mug; border-box measures the finished parcel.

saying these in an interview costs you the question

  • Says padding is included in width by default
  • Claims border-box also absorbs margin
  • Thinks border-box shrinks the content instead of the content box
  • Answers 140px by forgetting the two borders
  • Believes box-sizing is inherited from the parent

context

open as a page

In CSS, how do display: block, display: inline, and display: inline-block differ in the way width, height, and vertical margins apply to a box?

level: juniorimportance: must knowfreq 80%

basics

~20 s

A block box takes a whole line and honours width, height and all four margins. A non-replaced inline box flows inside a line of text and ignores width, height and vertical margins. inline-block flows inline but sizes like a block.

open as a page

In CSS, if an img has only its width set and height left at the default auto, what height does it render at and why?

level: juniorimportance: must knowfreq 58%

basics

~10 s

An img is a replaced element carrying an intrinsic width, height and ratio from the image file, so height: auto resolves through that ratio. The rendered height scales proportionally with whatever width you set.

open as a page

In CSS normal flow, one block element has margin-bottom: 30px and the block directly below it has margin-top: 20px. What vertical gap appears between them, and why?

level: juniorimportance: must knowfreq 78%

basics

~10 s

The gap is 30px. Adjoining vertical margins of block boxes in normal flow collapse into one margin equal to the larger value instead of adding up, so the 20px margin contributes nothing extra.

open as a page

In CSS, what is the difference between overflow: auto and overflow: scroll, and which overflow values turn an element into a scroll container?

level: juniorimportance: must knowfreq 70%

basics

~20 s

overflow: scroll always provides a scrolling mechanism whether or not content overflows; overflow: auto provides one only when it does. Both make the element a scroll container, and so does overflow: hidden, which clips and stays scrollable programmatically but shows no scrollbars.

open as a page

In CSS, why does height: 100% on a child element so often do nothing, while width: 100% works as expected?

level: middleimportance: must knowfreq 70%

basics

~20 s

A percentage height resolves against the containing block's height, and a normal-flow parent with height: auto has no definite height to resolve against, so the percentage behaves as auto. Widths work because a block's available inline size is always known.

open as a page

In CSS logical properties, what do the block and inline axes mean, and which physical sides do margin-block-start and margin-inline-start map to in a horizontal-tb, left-to-right document?

level: middleimportance: must knowfreq 55%

basics

~10 s

The inline axis runs along the text direction; the block axis is the direction lines stack. In writing-mode: horizontal-tb with direction: ltr, margin-inline-start resolves to margin-left and margin-block-start resolves to margin-top.

open as a page

A div in normal flow has no padding or border, and its first child has margin-top: 40px. The 40px of space appears above the parent rather than inside it. Why does CSS do that, and where does the parent's box actually start?

level: middleimportance: must knowfreq 66%

basics

~20 s

The child's top margin collapses with the parent's top margin, producing one margin outside the parent. The parent's top border edge ends up flush with the child's top edge, so the space is pushed above the parent instead of inside it.

open as a page

In CSS, what is a block formatting context, and which declarations establish a new one?

level: seniorimportance: must knowfreq 58%

basics

~20 s

A block formatting context is a self-contained region of block layout: floats inside it are contained, outside floats cannot intrude, and margins cannot collapse through its boundary. overflow other than visible, display: flow-root, floats, absolutely positioned boxes, inline-blocks, table cells and flex or grid items each establish one.

open as a page

A single CSS rule sets margin-left: 0 and then margin-inline-start: 16px on an element in a left-to-right document. Which one takes effect, and why is mixing physical and logical properties risky?

level: seniorimportance: must knowfreq 34%

basics

~20 s

The 16px wins: in a left-to-right horizontal document both declarations target the same physical side, so they cascade together and the later one at equal specificity applies. Swap the order and margin-left: 0 wins instead.

open as a page

In CSS, how does inline-size differ from width, and when do the two set different physical dimensions?

level: juniorimportance: should knowfreq 40%

basics

~10 s

width always sizes the horizontal dimension, while inline-size sizes the box along the text axis. They are identical in horizontal writing modes and swap roles in vertical ones, where inline-size controls height instead.

open as a page

In CSS, why does text-overflow: ellipsis on its own not truncate a long line, and what else must be declared for it to work?

level: juniorimportance: should knowfreq 58%

basics

~20 s

text-overflow only styles text that the box is already clipping, so it needs overflow set to something other than visible, white-space: nowrap so the text stays on one line, and a width the line can actually exceed.

open as a page

In CSS, an element has `box-sizing: border-box`, `width: 300px`, `padding: 16px`, `border: 1px solid`, and `margin: 24px`. How much horizontal space does it take up inside its parent, and how wide is its content box?

level: middleimportance: should knowfreq 46%

basics

~20 s

The painted box is 300px because border-box includes padding and border, leaving a 266px content box (300 − 32 − 2). Margins stay outside the declared width, so the element reserves 300 + 24 + 24 = 348px of horizontal space in its parent.

open as a page

For an in-flow block-level element in CSS, what does `width: 50%` resolve against, and what does a percentage in `padding` resolve against?

level: middleimportance: should knowfreq 52%

basics

~20 s

A percentage width resolves against the containing block's width — for an in-flow block element, the content-box width of its nearest block container ancestor. Percentages in padding and margin resolve against that same inline size, even for padding-top and padding-bottom.

open as a page

An image placed inside a div with no padding or margins still leaves a few pixels of space below it. Which CSS layout rule causes that gap, and what removes it?

level: middleimportance: should knowfreq 48%

basics

~20 s

The image is an inline-level box, so it sits on the text baseline of its line box, and the descender space reserved below that baseline shows as a gap. Set the image to display: block, or align it with vertical-align: bottom.

open as a page

Two boxes styled with display: inline-block sit side by side, but a few pixels of space appear between them even though both have margin: 0. In CSS, where does that gap come from and how do you remove it?

level: middleimportance: should knowfreq 55%

basics

~20 s

The gap is the whitespace between the two tags in the source. Inline-level boxes sit in an inline formatting context, so a newline or space collapses into one rendered space character sized by the parent's font. Remove the whitespace or stop using inline layout.

open as a page

In CSS, what do the sizing keywords min-content, max-content and fit-content mean when used as a value for width?

level: middleimportance: should knowfreq 42%

basics

~20 s

max-content is the width the content would take on a single line with no wrapping; min-content is the narrowest width without overflowing, roughly the longest unbreakable word; fit-content sits between them, using max-content when it fits the available space and shrinking no further than min-content.

open as a page

In CSS, an element has width: 400px, max-width: 300px and min-width: 500px. What is its used width, and what is the resolution order between the three properties?

level: middleimportance: should knowfreq 46%

basics

~20 s

The used width is 500px. CSS computes a tentative width from width, clamps it down to max-width if it exceeds it, then clamps it up to min-width if it falls below — so min-width always wins over max-width, which always wins over width.

open as a page

What does writing-mode: vertical-rl do to an element's block and inline axes, and which physical dimension does inline-size then control?

level: middleimportance: should knowfreq 28%

basics

~10 s

writing-mode: vertical-rl makes text run top-to-bottom in lines that stack right-to-left. The inline axis becomes vertical and the block axis horizontal, so inline-size sets the element's rendered height and block-size its width.

open as a page

When margins that collapse in CSS include negative values — for example margin-bottom: 30px meeting margin-top: -10px — how is the resulting margin computed?

level: middleimportance: should knowfreq 38%

basics

~20 s

Take the largest positive margin and the most negative margin and add them: 30px plus -10px gives 20px. When every value is positive the rule reduces to the largest wins; when every value is negative, the most negative wins.

open as a page

In CSS, what actually happens when you set overflow-x: hidden and leave overflow-y as visible on the same element?

level: middleimportance: should knowfreq 45%

basics

~20 s

You do not get "clipped horizontally, spilling vertically". Because one axis uses a scrolling value, the visible on the other axis computes to auto, so the element becomes a scroll container in both directions and vertical overflow is clipped and scrollable too.

open as a page

In CSS, what is the difference between overflow: hidden and overflow: clip?

level: middleimportance: should knowfreq 42%

basics

~20 s

Both clip content that does not fit, but overflow: hidden makes the element a scroll container that can still be scrolled programmatically, while overflow: clip creates no scroll container at all and can be paired per-axis with visible.

open as a page

Why do CSS resets apply `box-sizing: border-box` through `*, *::before, *::after` rather than on `html` alone, and when would you use the `box-sizing: inherit` variant instead?

level: seniorimportance: should knowfreq 45%

basics

~20 s

box-sizing is not an inherited property, so setting it on html alone changes nothing else; the reset must match every element, and the universal selector does not match pseudo-elements, hence ::before and ::after. The inherit variant lets a whole subtree be flipped back by one declaration on its root.

open as a page

How would you use the CSS aspect-ratio property to reserve space for a media embed, and in which cases is the declared ratio ignored?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Declare aspect-ratio on the box and leave one axis auto: aspect-ratio: 16 / 9 with a fluid width computes the height before any content loads, holding the space open. The ratio is ignored when both width and height are definite, and the box can still grow past it when content does not fit.

open as a page

After switching a page to direction: rtl, which CSS declarations keep pointing the same physical way even when every box property has been converted to logical form?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Logical properties only cover the box: margins, padding, borders, insets, sizes and corner radii. Shadows, transforms, background-position keywords, gradient directions and physical text-align values stay fixed to the screen and need a manual right-to-left pass.

open as a page

In CSS normal flow, which conditions stop vertical margins from collapsing, and which of those would you choose to keep a child's margin inside its parent?

level: seniorimportance: should knowfreq 52%

basics

~20 s

Margins stop collapsing when something separates them (padding, border, content, clearance) or when the box is not a plain in-flow block: floats, absolutely positioned boxes, flex and grid items, and boxes establishing a block formatting context. For a leaking child margin, display: flow-root is the surgical fix.

open as a page

A scrollable panel inside a modal keeps scrolling the page behind it once it reaches its own end. Which CSS property stops that, and how do its values differ?

level: seniorimportance: should knowfreq 40%

basics

~10 s

overscroll-behavior controls scroll chaining. Setting overscroll-behavior: contain on the panel keeps the scroll inside it once it hits an edge; none does the same and additionally suppresses the platform's overscroll effect such as pull-to-refresh.

open as a page

For a product adding an Arabic locale, how would you choose between refactoring the stylesheet to CSS logical properties and generating a mirrored stylesheet with a build-time RTL post-processor such as rtlcss or CSSJanus?

level: principalimportance: should knowfreq 20%

basics

~20 s

Logical properties resolve per element at style time and need one stylesheet, but require touching every rule. A post-processor flips physical declarations at build time with no refactor, yet ships two bundles and cannot handle both directions on one page.

open as a page

An empty div with margin-top: 20px and margin-bottom: 30px, and no height, padding, border or content, sits between two paragraphs in normal flow. How much vertical space does it contribute?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

About 30px, not 50px. With no height, padding, border or content, the div self-collapses: its own top and bottom margins are adjoining and merge, and that single margin then collapses with the neighbouring paragraphs' margins too.

open as a page

In CSS scroll snapping, how do scroll-snap-type: y mandatory and y proximity differ, and where do the snap positions come from?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Snap positions come from scroll-snap-align on the children of a scroll container. With mandatory the scroller must always rest on one of them, so it snaps from anywhere; with proximity it snaps only when the resting position is already close to one.

open as a page