Every element on a page is a box made of four nested layers, and nearly every layout bug you will meet comes from misjudging which layer you are looking at.
The model is easy to understand once you can picture it, because the four layers always sit in the same order.
This is the first lesson in the CSS Box Model module, and the next lesson in this module is about "Why Does Almost Every Project Set border-box?".
What are the four layers?
From the inside out, you have the content, then the padding that surrounds it, then the border around the padding, and finally the margin outside the border. Nothing you write in CSS will change that order.
The width and height you set apply to the content area only. The border and the margin are added on top of those numbers, so a box never ends up as big as the value you typed.
.card {
width: 300px; /* the content width */
padding: 20px; /* inside the border, outside the content */
border: 2px solid #333;
margin: 16px; /* outside the border */
}Add them together and the space the element occupies is 300 + 40 + 4 + 32, which is 376 pixels wide.
Those extra 76 pixels are the source of the classic surprise. A row of three cards at 300 pixels each is 900 pixels wide and not 300, so a parent that is set to 300 pixels cannot hold it. That overflow is not a fault in your grid. It is the box model doing exactly what it was asked to do.
margin (16)
┌───────────────────────────┐
│ border (2) │
│ ┌───────────────────────┐ │
│ │ padding (20) │ │
│ │ ┌───────────────┐ │ │
│ │ │ content 300 │ │ │
│ │ └───────────────┘ │ │
│ └───────────────────────┘ │
└───────────────────────────┘What do the width and the height actually measure?
By default the width and height set the size of the content box only. The padding and the border are added outside it, and the margin sits outside everything.
This default surprises a lot of people, and it is also the behaviour that the next lesson in this module exists to overturn.
Two related properties change what those numbers mean.
The min-width and max-width put limits on the size of the content, and that is how you make a fluid image that never grows past its column.
A percentage value for width on an element resolves against the content box of the parent. That is the other reason why a percentage width plus padding can overflow.
.card {
width: 100%; /* 100% of the parent's content width */
padding: 20px; /* plus 40px — so it overflows by 40px */
}How do the padding and the margin differ?
The padding sits inside the border, and it is part of the background of the element. A padded area takes the background colour of the element, and the background of a border-box includes the padding.
The margin sits outside the border, and it is always transparent. It creates space between elements, and whatever is behind it shows through.
.box {
background: #f0f0f0; /* this paints the padding too */
padding: 16px;
margin: 16px; /* the area around it shows the page behind */
}That difference explains a common source of confusion.
A gap between two cards that is transparent and that shows the background of the page is a margin.
A gap that looks like part of a card and that is tinted with its background is padding.
If you want a visible band of colour between two elements, use padding on a wrapper or use a border, and not a margin.
What do the auto margins do?
An automatic margin takes up whatever space is left over. In a block context that pushes the element to the centre horizontally.
.container {
max-width: 60rem;
margin-inline: auto; /* centred, and it stays centred at any width */
padding: 1rem;
}This is the standard way to centre a page. Give the element a maximum width, set the margins on the inline axis to auto, and leave the block axis alone.
The logical margin-inline property is the modern spelling of the same idea. It means the left and right margins in a left-to-right language, and it flips automatically in a right-to-left one. That is why the logical properties are worth preferring over margin-left and margin-right.
How do you debug a box that is the wrong size?
Turn on the box model view in the developer tools, then hover over the element.
The four layers are drawn over the page, and the numbers in the tooltip tell you exactly which layer is larger than you expected.
content 300 x 180
padding 20 / 20 / 20 / 20
border 2 / 2 / 2 / 2
margin 16 / 16 / 16 / 16Then ask yourself three questions.
Is the width I set the content width, or the total width?
Is the padding included in the space the parent offers?
And are two margins being added into the same gap?
Those three questions account for most overflow bugs, and the box model overlay in your browser answers them in a second.
How do you size a box to what is inside it?
The width and height on a block box mean the size of the content area. That is why a box with padding and a border ends up wider than the number you wrote.
Three sizing values cover most needs: auto, fit-content, and max-content. Each one answers a different question.
width: auto is the default for a block box. It means fill the available width and then subtract the padding and the border. It is not the same as setting no width at all, because the declared width and the used width are different things once padding is involved.
width: fit-content means as wide as the content needs, but never wider than the space available. Use it for a button, a tag, or a card in a flex row that should shrink to its text instead of stretching.
width: max-content means as wide as the content would be with no wrapping at all. It suits a dropdown panel, and any text where you want the longest line to decide the width.
<div class="row">
<span class="tag">free</span>
<span class="tag">open source</span>
</div>.row {
display: flex;
gap: 0.5rem;
}
.tag {
/* shrink to the text, but never grow past the row */
width: fit-content;
padding: 0.2rem 0.6rem;
border: 1px solid #b9c6d2;
border-radius: 999px;
}For the opposite problem, a box that should never grow past a certain point, max-width is a better tool than a fixed width.
A max-width: 60ch on a paragraph keeps the lines readable on a wide monitor, while it still lets the text shrink on a narrow phone, and it needs no media query to do it.
Quick reference
- Content, padding, border, margin. Always in that order, from the inside out.
- By default the width sets the content width, and the padding and the border are added on top of it.
- The padding is inside the border and it takes the background, while the margin is outside and it is transparent.
- Percentage widths resolve against the content box of the parent, so the padding makes them overflow.
- The
margin-inline:auto centres a block, and the logical property also works from right to left.
Try it yourself: set a card to 300 pixels wide with 20 pixels of padding, and give it a border. Measure it in the developer tools and confirm that the total is 376. Then change the box sizing to border-box and watch the total stay at 300 while the content area shrinks.