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
  1. How wide is that card?
  2. Shorthand
  3. Margin collapse
  4. display changes the box
  5. Borders and rounded corners
  6. Overflow
  7. Try it

Every element the browser draws is a rectangle. From the inside out, that rectangle has four layers:

  1. Content: the text or image itself
  2. Padding: space between the content and the border
  3. Border: a line around the padding
  4. 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