CSS chapters

Chapter 12 of 17

The box model

Try this in our CSS Compiler →

The idea

Each element's box has content, padding, border and margin. With content-box, width sets the content width and padding and border add to it. With border-box, width includes content, padding and border. Margin remains outside both definitions.

Worked example

* { box-sizing: border-box; }
button { width: 200px; padding: 16px; border: 2px solid teal; margin: 8px; }

What to check

The button's border box is 200px wide with border-box. Its outer occupied space also includes the margins.

Practice task

Switch to content-box and calculate the new border-box width: 200 + 32 + 4 = 236px. Check it in browser DevTools.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on