Lesson 3 of 7 · 5 min
The box model
Every element is a box of content, padding, border and margin. Learn how they add up, how display changes the box, and the one reset every project needs.
On this page
Every element the browser draws is a rectangle. From the inside out, that rectangle has four layers:
- Content: the text or image itself
- Padding: space between the content and the border
- Border: a line around the padding
- Margin: space outside the border, pushing other boxes away
.card {
width: 300px;
padding: 24px;
border: 1px solid #e5e5e5;
margin-bottom: 32px;
}
How wide is that card?
By default, width sets only the content width. Padding and border are added on top:
300 + 24 + 24 + 1 + 1 = 350px
That surprises everyone. The fix is one line at the top of every stylesheet:
*,
*::before,
*::after {
box-sizing: border-box;
}
With border-box, width: 300px means the whole box, border included, is 300px. The content shrinks to fit. This is the default on almost every modern site, including this one.
Shorthand
Padding and margin take one to four values, going clockwise from the top:
padding: 16px; /* all four sides */
padding: 16px 24px; /* top and bottom, left and right */
padding: 8px 16px 24px; /* top, left and right, bottom */
padding: 8px 16px 24px 32px; /* top, right, bottom, left */
You can also set one axis with logical properties, which follow the reading direction of the page:
padding-inline: 24px; /* left and right in English */
margin-block: 2rem; /* top and bottom */
Margin collapse
Vertical margins between two blocks do not add up; the larger one wins. A paragraph with margin-bottom: 24px followed by one with margin-top: 16px leaves a 24px gap, not 40px.
Margin collapse does not happen inside Flexbox or Grid containers, which is one reason modern layouts use gap instead of margins.
display changes the box
| Value | Behaviour |
|---|---|
block |
Starts on a new line and fills the available width. div, p, h1 are block by default. |
inline |
Flows inside text. Width, height and vertical margin are ignored. a, span, strong are inline. |
inline-block |
Flows inside text, but respects width, height and padding. |
none |
Removed from the page completely. |
flex / grid |
A block whose children are laid out with Flexbox or Grid. |
Borders and rounded corners
.button {
border: 1px solid #0a0a0a;
border-radius: 8px;
}
.avatar {
border-radius: 50%; /* a circle, if the box is square */
}
Overflow
When content is bigger than its box, overflow decides what happens:
.code { overflow-x: auto; } /* scroll sideways only when needed */
.clip { overflow: hidden; } /* cut it off */
Try it
Make three .card elements with width: 300px, padding: 24px and a border. Measure one in the developer tools (the box diagram in the Computed tab). Then add the box-sizing reset and measure again.
More from CSS Crash Course
- Lesson 5 · 5 minFlexboxLay out items in a row or a column, align them on both axes, share space between them, and build the navigation bars and card rows you see everywhere.
- Lesson 6 · 5 minCSS GridTwo-dimensional layout with rows and columns. Fixed and flexible tracks, the fr unit, auto-fill card grids, named areas, and when to choose Grid over Flexbox.
- Lesson 7 · 5 minResponsive design and motionMobile-first CSS, media and container queries, fluid widths, responsive images, and transitions and animations that respect people who prefer less motion.