Lesson 13 of 52CSS foundations
The Box Model
Every element is a box made of content, padding, border and margin. Learn how those layers add up and how to keep sizes predictable.
Open any web page, select everything in the devtools, and you will find that it is made entirely of rectangles. Headings, paragraphs, buttons, images, even single words inside a link: each one is a box. The box model is the set of rules that decides how big each box is and how much space sits around it. When a card is 40 pixels wider than you asked for, or two paragraphs are further apart than you expected, the box model is the answer. Learn it once and spacing stops being guesswork.
The four layers
Every box has four layers, from the inside out. Think of a framed photo on a wall: the photo, the mat around it, the frame, and the empty wall space you leave before the next picture.
The content box holds the text or children. width and height size this box by default.
Each layer has a property for each side. padding-top, padding-right, padding-bottom and padding-left can be written separately, or as a shorthand that follows the clock from the top:
| Shorthand | Means |
|---|---|
padding: 16px; | 16px on all four sides |
padding: 8px 16px; | 8px top and bottom, 16px left and right |
padding: 8px 16px 24px; | top 8px, sides 16px, bottom 24px |
padding: 1px 2px 3px 4px; | top, right, bottom, left (clockwise) |
margin works the same way. border is usually written as border: 2px solid #222; with width, style and colour in one go.
Preview
Work it out first: the card is 220 + 20 + 20 + 6 + 6 = 272 pixels wide on screen, plus margin around it. Now try changing padding to 40px and watch the card grow. Then change margin: 24px to margin: 24px auto to centre it horizontally.
The fix: box-sizing: border-box
That arithmetic is the default behaviour, called box-sizing: content-box. width sets only the content, and padding and border are added on top. It made sense in 1996 and is a nuisance today: if you say a sidebar is 300px wide, you want it to be 300px wide, whatever its padding.
box-sizing: border-box changes the meaning of width and height to include padding and border. The browser shrinks the content area to make everything fit inside the size you asked for. Almost every modern project starts with this rule:
*, *::before, *::after {
box-sizing: border-box;
}How many pixels wide is this element on screen, not counting margin?
.panel {
box-sizing: border-box;
width: 300px;
padding: 24px;
border: 3px solid black;
margin: 10px;
}Block, inline and inline-block
The display property decides how a box sits next to other boxes. Three values matter here.
- Block boxes (
div,p,h1,section,ul) start on a new line and stretch to fill the width of their parent.width,height, padding and margin all work as you would expect. - Inline boxes (
span,a,strong,em) flow inside a line of text, like words. They ignorewidthandheight. Their horizontal padding and margin push neighbours apart, but vertical padding and margin do not move the lines above or below. Padding still paints, so a background may overlap the line above. - Inline-block boxes flow inside a line like inline elements but respect width, height and vertical spacing like blocks. They are useful for small badges or buttons that sit in a sentence.
Preview
Try this: the yellow padding spills over the lines above and below, and width does nothing. Change display: inline to inline-block and the tags push the lines apart and become 120px wide. Then try block and each tag takes its own full-width line.
width: 200px and margin-top: 30px on a span with no other styles. What happens?Width, height, min and max
By default a block is as wide as its parent and as tall as its content. Usually that is exactly what you want, and setting a fixed height on a box that holds text is a common source of bugs: when the text grows (a longer translation, a larger font setting) it overflows.
The min and max properties are safer. They set limits and let the box size itself within them:
max-width: 640pxlets a column shrink on a phone but stops it growing too wide on a big monitor.min-height: 200pxkeeps a card from looking empty while still growing when it has more to say.width: 100%; max-width: 480px;is a classic pair for form fields and images.
Percentages in width are relative to the parent's content width. width: 50% inside a 600px parent gives 300px.
Two margin tricks come up all the time. margin-inline: auto (or margin: 0 auto) on a block with a width or max-width splits the leftover horizontal space equally, which centres the block in its parent. This is how most pages centre their main column. Margins can also be negative, which pulls a box toward its neighbour instead of pushing it away. That is occasionally handy, for example to let an image break out of a padded card, but reach for it rarely, because it makes layouts harder to follow.
Overflow
When content is bigger than its box, it overflows. By default it stays visible and spills out over whatever is next to it. The overflow property decides what happens instead:
| Value | Behaviour |
|---|---|
visible | Content spills out (the default) |
hidden | Extra content is cut off |
scroll | Always shows scrollbars |
auto | Shows scrollbars only when needed |
overflow: auto is the usual choice for a box that should scroll, like a code sample or a chat history. Use hidden with care: anything cut off is gone for users who zoom in, including keyboard focus rings. You can also control one direction with overflow-x and overflow-y.
Preview
Try this: the text spills over the next paragraph. Change visible to auto and a scrollbar appears. Try hidden, then remove the height line entirely and see the box grow to fit, which is usually the best fix.
Margin collapsing
One rule surprises everyone. When two vertical margins touch, they do not add up. They collapse into a single margin the size of the larger one. If a heading has margin-bottom: 24px and the paragraph below has margin-top: 16px, the gap is 24px, not 40px.
Collapsing only happens with vertical margins between block boxes in normal flow. It happens in three situations: between adjacent siblings, between a parent and its first or last child when no padding or border separates them, and on an empty box whose top and bottom margins touch. The parent case causes the classic bug where a child's margin-top seems to push the whole parent down instead of creating space inside it. Adding padding or a border to the parent, or making it a flex or grid container, stops it. Margins never collapse inside flex and grid layouts, or horizontally.
Match each situation to the gap you get.
width: 300px include the padding and border?- box model
- The rule that every element is a rectangle of content, padding, border and margin.
- padding
- Space between the content and the border. The background shows through it.
- margin
- Transparent space outside the border that separates a box from its neighbours.
- border-box
- A box-sizing value where width and height include padding and border.
- inline-block
- A display value that sits in a line of text but respects width, height and vertical spacing.
- overflow
- What happens when content is larger than its box: visible, hidden, scroll or auto.
- margin collapsing
- Touching vertical margins between blocks merging into one margin the size of the larger.