The box model
Overview
Every element on a page — a heading, a link, an image, a <div> — is laid out as a rectangular box. Understanding how that box is built is the foundation of all CSS layout. Most “why is this 20 pixels too wide?” questions are answered by the box model.
The four layers
From the inside out:
- Content — the text, image or child elements. Its size is set by
widthandheight(or by the content itself). - Padding — space between the content and the border. It’s inside the box, so the element’s background colour or image shows through it.
- Border — a line around the padding. It can have a width, style and colour, and it takes up space.
- Margin — space outside the border that separates the box from its neighbours. Margins are always transparent.
.card {
width: 20rem;
padding: 1rem;
border: 2px solid #2d5bd7;
margin: 1.5rem;
background: #eaf0fc; /* fills content and padding, not margin */
}Padding
Padding gives content breathing room inside its box — without it, text would touch the border.
padding: 1rem; /* all four sides */
padding: 1rem 2rem; /* top & bottom | left & right */
padding: 1rem 2rem 0.5rem; /* top | left & right | bottom */
padding: 1rem 2rem 0.5rem 0; /* top | right | bottom | left */
padding-top: 1rem; /* one side at a time */
padding-inline: 2rem; /* left and right (logical) */
padding-block: 1rem; /* top and bottom (logical) */The one-to-four value pattern goes clockwise from the top — top, right, bottom, left (remember “TRouBLe”). It’s shared by margin, border-width, border-style, border-color and inset.
Padding can’t be negative. Percentages in padding are calculated from the width of the containing block — even for top and bottom padding — which was once the standard trick for aspect ratios (now replaced by aspect-ratio).
Border
border: 2px solid #2d5bd7; /* width style colour */
border-bottom: 1px dashed #9a9ea7; /* one side */
border-radius: 12px; /* rounded corners */
border-radius: 50%; /* a circle, if the box is square */A border needs a style to appear — solid, dashed, dotted, double… The default style is none, so border: 2px #2d5bd7 shows nothing.
border-radius rounds the corners of the border and the background, and doesn’t change the box’s size.
Outline is not border
outline draws a line around an element without taking up space, so it never shifts the layout. Browsers use it for focus indicators. Never remove focus outlines without replacing them (see Layouts that work for everyone).
Margin
Margins push boxes apart:
h2 { margin-block: 2rem 0.5rem; } /* more space above a heading than below */
.button + .button { margin-inline-start: 0.5rem; }Margins have some special abilities:
auto— in normal flow,margin-inline: autoon a block with a set width centres it horizontally by sharing the leftover space equally on both sides.- Negative values — pull an element towards (or over) its neighbours. Useful occasionally; confusing often.
- Collapsing — adjacent vertical margins in normal flow merge into one. This surprises everyone and gets its own lesson, Margins and collapsing.
.page {
max-width: 60rem;
margin-inline: auto; /* centred column */
}In flex and grid layouts, the gap property often replaces margins for spacing between items — it’s cleaner because it only adds space between items, never at the edges.
Adding it up
With the default box-sizing: content-box, width and height set only the content area. Padding and border are added on the outside:
rendered width = width + padding-left + padding-right + border-left + border-right
.card { width: 20rem; padding: 1rem; border: 2px solid; }
= 320 + 16 + 16 + 2 + 2 = 356pxMargins don’t count towards the element’s own size, but they do take up space in the layout — the card above occupies 356px plus its margins.
This addition is the source of countless layout bugs: you set width: 50% on two side-by-side columns, add some padding, and suddenly they don’t fit. The next lesson shows the fix that almost every stylesheet uses: box-sizing: border-box.
Seeing the box model
In DevTools, select an element and open the Computed tab: the box model diagram shows each layer’s exact size, and hovering an element on the page colours its content, padding, border and margin. Use it whenever an element’s size surprises you.
Rule of thumb: use padding for space inside a component (between its border and its content) and margin or gap for space between components.
A pricing card
Style a .plan card so that: its content area is 18rem wide; it has 1.5rem of padding on all sides; a 2px solid border in #2d5bd7; rounded corners of 12px; and 2rem of space below it (so a second card sits 2rem lower). Using the default box-sizing, calculate the card’s total rendered width in pixels, then confirm it in DevTools.
<article class="plan">
<h2>Pro</h2>
<p>All courses, certificates and projects.</p>
</article>
<article class="plan">
<h2>Team</h2>
<p>Everything in Pro, for five people.</p>
</article>
<style>
/* your code here */
</style><article class="plan">
<h2>Pro</h2>
<p>All courses, certificates and projects.</p>
</article>
<article class="plan">
<h2>Team</h2>
<p>Everything in Pro, for five people.</p>
</article>
<style>
.plan {
width: 18rem; /* content area: 288px */
padding: 1.5rem; /* 24px on each side */
border: 2px solid #2d5bd7;
border-radius: 12px;
margin-bottom: 2rem; /* space before the next card */
}
/* Total width = 288 + 24 + 24 + 2 + 2 = 340px (content-box) */
</style>Summary
- Every element is a rectangular box made of content, padding, border and margin.
- Padding is inside the border and shows the background; margin is outside and is always transparent.
- With the default
box-sizing: content-box,widthsets only the content; padding and border are added on top. - Shorthands take one to four values in the order top, right, bottom, left (clockwise).
- Margins can be negative or
auto; padding can’t be negative.