The CSS box model
Change a control, or edit the CSS. The preview uses that same CSS.
Numeric changes ease so you can watch them.
You changed padding and box-sizing on one declared width. The border edge grew under content-box because padding and border were added outside that width, and it stayed on the declared width under border-box.
+Go deeper
Four layers
From the outside in, a box is margin, border, padding, then content. Backgrounds paint the content and padding, and by default they also sit under the border. Margin is outside the border and stays transparent. This is the .
Two meanings of width
From the inside out the layers are content, padding, border, and margin. Padding is space inside the border. Margin is space outside it, between this box and its neighbors. The initial value of box-sizing is content-box. Width and height then measure the content only, so padding and border are added outside. A declared width of 200px with 20px of padding on each side takes 240px before the border is even counted. border-box makes the declared width include content, padding, and border, so that 200px box stays 200px and the content shrinks to make room. Margin stays outside both models. Many resets set border-box on every element because the declared size is then the size you meant. A later rule can still lose on the if a heavier selector sets the other model. The playground uses 140px and 36px of padding so the same idea is large enough to see.
| box-sizing | What width measures | Where padding and border go |
|---|---|---|
| content-box | The content box only | Added outside the declared size |
| border-box | Content, padding, and border | Included inside the declared size |
Margin stays outside
Switching to border-box does not pull margin inside the width. Two blocks with width 100 percent and a horizontal margin still overflow. Vertical margins of adjacent blocks in normal flow can collapse into the larger of the two, so 16 plus 16 often becomes 16.
Percent needs a reference
A percentage width is a percentage of the containing block width. A percentage height often computes to auto unless the parent has a specified height. The percentage is not broken. The reference length is missing.
Read the box in the tools
The Elements panel draws margin, border, padding, and content with used pixel values. If a width rule is crossed out, it lost the cascade from the previous lesson. The playground readout is the small version of that diagram: declared width against the border edge.
Guided challenge
The CSS box model
The style attribute already says content-box. The script still logs border-box. Read the first declaration from the attribute instead of printing a fixed word.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
content-box 140px 36px Notes
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.