Ever opened a website on your phone and found you could drag the page a little sideways, though there was nothing out there? The cause is nearly always the same: some box is wider than its CSS says.
On a web page, every element (a part of the page, like a heading, a paragraph or a button) is a box. Every box has four layers: content (what's inside), padding (space inside), border (the edge line) and margin (space outside). Picture a framed photo on a wall. The photo is the content. The white card around the photo is the padding. The wooden frame is the border. The empty wall between two frames is the margin. If you say "the frame is 30 cm", people need to know: is that 30 cm the photo alone, or up to the frame? CSS has the same question for width: is the width the content only, or everything up to the border? box-sizing sets the answer.
The four layers of the booking card
This is Mrs. Sri's booking card. The card is <div class="card">, and under it is the cancellation callout:
<div class="card">
<h3>Terrace Room</h3>
<p class="total">2 nights × $35 = $70</p>
<button class="book">Book the room, pay on arrival</button>
</div>
<p class="callout">Free cancellation up to 3 days before check-in.</p>
<style>
.card {
width: 300px;
padding: 24px; /* space between the content and the edge line */
border: 1px solid #e2e8f0; /* the edge line */
margin-bottom: 16px; /* the gap to the callout below */
background: #fff;
}
.book { width: 100%; padding: 12px 16px; }
</style>
From the inside out: content → padding → border → margin. The card's white background (background) paints the content, the padding and the area under the border. The margin is always transparent: what shows there is the page background.
content-box or border-box
The booking card above is written width: 300px; padding: 24px; border: 1px solid #e2e8f0;, with no other rules. Before looking at the table, guess: how wide is the card on screen?
box-sizing decides which layer width measures.
content-box (the browser default) |
border-box |
|
|---|---|---|
width: 300px means |
the content width only | the width out to the outside of the border |
| Card width on screen | 300 + 2 × 24 + 2 × 1 = 350px | 300px |
| Content width | 300px | 300 − 48 − 2 = 250px |
A width: 100% button with 16px left and right padding |
300 + 32 = 332px, 32px wider than the content, through the card's padding and border | exactly 250px |
The answer is 350px. The browser default is content-box, so padding and border are added outside the 300px. The card you thought was 300px is 50px wider. The width: 100% button (as wide as the card's content) inside it also sticks out past the card's right edge. That is why almost every project starts its CSS with this rule:
*, *::before, *::after {
box-sizing: border-box;
}
*selects every element.::beforeand::afterare included so that the extra elements CSS creates useborder-boxtoo.- After that, the
widthyou write is the width you see. Twowidth: 50%cards with padding still fit side by side instead of dropping to a new line. box-sizingonly matters when you write awidthorheight. A card with nowidthfills its container (the element around it) in both modes.
Padding or margin?
- Padding is space inside the box. It takes the background colour, and it can be clicked. The book button with
padding: 12px 16pxis about 12 + 19 (one line of text) + 12 = 43px tall, and that whole area can be pressed. - Margin is space outside the box, for gaps between elements. It is transparent and can't be clicked.
A practical rule: the space between something's content and its own edge is padding, and the gap between two elements is margin. On Mrs. Sri's card, the 24px inside the card is padding, and the 16px between the card and the callout is margin.
Margin collapse: gaps that don't add up
The cancellation callout under the card gets margin-top: 24px (a top margin). The card has margin-bottom: 16px (a bottom margin). How big is the gap? It depends on the container around both of them. The container can be an ordinary block, an element that takes a line of its own, like <div> and <p>. It can also use flex or grid, two other ways to arrange what is inside a container:
| The container | Gap from card to callout |
|---|---|
| An ordinary block | 24px, the larger of 16 and 24 |
display: flex in a column, or grid |
16 + 24 = 40px |
This is called margin collapse. Two vertical margins (top and bottom) that meet between two ordinary block elements merge into one, and the larger one is used. Left and right margins never collapse, and inside flex or grid margins don't collapse either. If a gap feels "smaller" than what you wrote, check this first.
Spacing system: multiples of 4 or 8
Don't pick gaps at random. Use multiples of one base number, usually 8px, with 4px for very small gaps. Each gap gets a short name, called a token:
| Token | Value | On Mrs. Sri's card |
|---|---|---|
| xs | 4px | the gap from the room name to its total, the gap between night buttons |
| sm | 8px | the gap from a heading to its paragraph |
| md | 16px | the card's margin to the callout, the button's left and right padding |
| lg | 24px | the card's padding |
| xl | 32px | the gap between sections of the page |
Every number on the card (4, 8, 12, 16, 24) is a multiple of 4, and the larger ones (16 and 24) are multiples of 8. When every gap comes from this list, the page looks tidy without measuring each one.
⚠️ Box sums that often come out wrong
- Forgetting
box-sizing: border-box.width: 100%plus padding makes an element wider than its container, and a sideways scroll appears on phones. - Adding up vertical margins. 16 + 24 between ordinary blocks becomes 24, not 40.
- Using margin to make a button bigger. The pressable area only grows with padding.
- Random gaps. 13px, 17px, 22px. Take them from multiples of 4 or 8.
TL;DR: Every element is a box with four layers: content, padding (space inside, takes the background and can be clicked), border (the edge line), and margin (space outside, transparent, for gaps between elements). The browser default, content-box, adds padding and border outside the width. So a card with width: 300px, 24px of padding and a 1px border is 300 + 2 × 24 + 2 × 1 = 350px wide. With *, *::before, *::after { box-sizing: border-box; }, width: 300px means 300px in total, and the content becomes 250px. Vertical margins between two ordinary blocks (elements that take a line of their own) collapse (merge) to the largest: 16 and 24 give 24. In flex and grid (other ways to arrange elements) margins don't collapse, so the gap is 16 + 24 = 40. Use a spacing system (a set of allowed gaps) with an 8px base, and 4px for small gaps: 4, 8, 16, 24, 32.
⚡ Four layers to every box
Meaning: Every element on a page is a box. From the inside out: content (what's inside), padding (space inside, takes the background and can be clicked), border (the edge line), then margin (space outside, transparent, for gaps between elements).
⚡ The width you write is the width you see
Write:
*, *::before, *::after {
box-sizing: border-box;
}
Guess first: without the code above, a card is written width: 300px; padding: 24px; border: 1px. How wide is it on screen?
350px. The browser default is content-box: width measures only the content, so padding and border are added outside it, 300 + 2 × 24 + 2 × 1. A width: 100% button with padding inside that card also sticks out past the card's edge.
Meaning: * selects every element, and ::before and ::after (extra elements that CSS creates) are included so they use border-box too. With border-box, width: 300px means 300px in total out to the border, and that card's content becomes 250px. The width you write is the width you see.
⚡ Gaps in multiples of 8
Meaning: Don't pick random gaps like 13px or 17px. Use multiples of an 8px base (and 4px for small gaps): 4, 8, 16, 24, 32. On Mrs. Sri's booking card, the card's padding is 24px, and its margin to the callout below is 16px. One more thing: vertical margins between two ordinary blocks (elements that take a line of their own) don't add up. Margins of 16 and 24 give a gap of 24.