Skip to content
BENIBIX Learn

The CSS Box Model

Level 3 · CSS basics

Why every part of a page is a box with padding, a border and a margin. How box-sizing changes its width, and which unit to size it in.

Course contentsLesson 8 of 14

After this lesson you can

  • Name the four layers of the box
  • Work out the real width of a box
  • Pick px, rem or % for a size

12 mincovers 3 glossary entries

Worth reading first: CSS Selectors and Specificity

What the four layers of a box are

Every element on a page is a box with four layers. In the middle is the content. Around it is the padding, then the border, then the margin. By default, width sets only the content. So a box with width: 300px, 20px of padding and a 2px border is really 344 pixels wide.

Each layer has its own job. Padding is space inside the border, and it takes the box's background colour. The border is the line you can see. Margin is space outside the border that pushes other boxes away. It is always clear, so the page behind shows through it.

That extra width catches everyone out. Two boxes set to width: 50% will not fit side by side once they have padding, because each is now more than half. The fix is one line: box-sizing: border-box. Then width covers the content, the padding and the border together, and 50% means 50%.

Most sites put box-sizing: border-box on every element at the top of their CSS. It makes sizes work the way people expect. Margins sit outside it either way. A margin is the gap between two boxes, not part of one.

Sizes can be set in different units. A px is a fixed screen pixel. A % is a share of the box around it. A rem is a share of the page's base text size, which starts at 16 pixels in most browsers, so 1.5rem is 24 pixels. A vw is one hundredth of the window's width.

The choice matters for the reader. Someone who sets a larger text size in their browser gets bigger text where you used rem, and no change where you used px. So use rem for text and spacing, % or fr for widths, and px for thin lines such as borders.

When a box is the wrong size, guessing wastes time. Browser DevTools open with F12 in most browsers. They draw every box as four coloured layers. Hover over an element and you see its content, padding, border and margin, each with its size in pixels.

css
*, *::before, *::after {
  box-sizing: border-box;   /* width now includes padding and border */
}

.card {
  width: 300px;
  padding: 1.25rem;         /* 20px when the base size is 16px */
  border: 2px solid #ccc;
  margin-bottom: 1rem;      /* space between this card and the next */
}
With border-box, the card is 300 pixels wide, padding and border included.
Content, padding, border and margin, from the inside out. Width covers only the middle unless you change it.

Worked example

Emma's two columns will not sit side by side

Emma sets two columns to width: 50% with 24px of padding each. The second column keeps dropping below the first.

DevTools shows each column is 50% plus 48 pixels. She adds box-sizing: border-box, and both columns fit on one line.

The words for this lesson3

Check yourself

How wide is a box with width 300px, 20px padding and a 2px border, by default?

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.