Normal flow, the box model and box-sizing
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Compute the rendered size of a box from its content, padding, border and margin.
- Explain what box-sizing: border-box changes.
Overview
#Every element generates a box. The box has content, padding, border and margin. By default, width and height apply to the content box only, so a width: 100px box with 10px padding and a 1px border occupies 122px. That surprises people and breaks layouts where two 50% columns plus padding overflow their parent.
.card {
box-sizing: border-box;
width: 100px;
padding: 10px;
border: 1px solid #333;
}
With box-sizing: border-box, the declared width includes padding and border, so the box occupies exactly 100px and the content area shrinks. Most modern codebases set this globally:
*, *::before, *::after {
box-sizing: border-box;
}
Normal flow is the default layout: block-level boxes stack vertically and fill the available inline size, while inline boxes flow within a line. Margins between adjacent vertical blocks collapse: the larger of the two margins wins rather than the sum. Margin collapsing is a frequent source of unexpected spacing, and it does not happen in a flex or grid container.
Understanding flow first makes every other layout system easier, because Flexbox and Grid are opt-in changes to how children are arranged, not replacements for the box model.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A box has width: 200px, padding: 16px, border: 2px and box-sizing: content-box. How wide is the visible box, and how wide is it with border-box?
starter.css.box { width: 200px; padding: 16px; border: 2px solid; }Reveal the reference answer
reference.csscontent-box: 200 + 16 + 16 + 2 + 2 = 236px. border-box: 200px, with the content area reduced to 164px.Authored expected output: 236px with content-box, 200px with border-box.
content-box adds padding and border outside the declared width; border-box includes them.
Exercise 2Implement
Write a global rule that makes every element size by border-box, including generated content.
starter.css/* add the rule */Reveal the reference answer
reference.css*, *::before, *::after { box-sizing: border-box; }Authored expected output: All elements and their pseudo-elements use border-box sizing.
Including ::before and ::after covers generated content that also generates boxes.
Common misconceptions
#- width always means the total visible width of the box.
- Adjacent vertical margins add together.
Follow-up practice
#- html-css-17-display-formatting-contexts
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.