edubun
English
1 / 13

4 / 9 · Basic

The box model

Give a box width: 200px, 16px of padding and a 2px border, and by default it takes up 236 pixels across the screen, not 200.

Jump to the 11 practice questions

You will learn

  • Name the four layers: content, padding, border, margin
  • Work out total width with content-box and border-box
  • Expect collapsing margins and inline boxes

Before this: Colour and units

1Explain

Every element is a box

The CSS box model describes every element as a box made of four layers: content, padding, border and margin. To lay out a page, the browser turns every element into a rectangle. Each box has four layers, from the inside out. The content area holds the text or image.

Content, padding, border and margin. With the default content-box sizing, width sets only the content: 200 + 16 + 16 + 2 + 2 = 236 pixels wide.

2Explore

Padding, border and box-sizing

With content-box the box grows when padding and border grow. With border-box it stays 160px and the content shrinks.

Tap a choice: the code and the preview change together.

padding
border
box-sizing
<style>
  .box { width: 160px; padding: 10px; border: 4px solid #1a5fb4; margin: 20px; background: #fff4d6; box-sizing: content-box; }
</style>
<div class="box">Rye loaf</div>
Preview

3Problem

From the inside out

Order the layers from the inside out.

  1. content
  2. margin
  3. padding
  4. border
Hint

The background shows in the content and the padding.

Solution

Answer:

  1. content
  2. padding
  3. border
  4. margin

Each layer wraps the one inside it: padding around the content, border around the padding, margin around the border.

4Problem

Four values, clockwise

padding, margin and border-width take one to four values, going clockwise from the top. padding: 16px sets all four sides. padding: 8px 16px sets top and bottom to 8 and left and right to 16. padding: 4px 8px 12px 16px sets top, right, bottom and left in that order. Each side can also be set on its own, as in margin-top. border: 2px solid #333 sets width, style and colour together; without a style such as solid or dashed no border is drawn at all.

padding: 1px 2px 3px 4px: how much is on the left?

px

Hint

Top, right, bottom, left.

Solution

Answer: 4 px

Clockwise from the top: top 1, right 2, bottom 3, left 4.

5Explain

box-sizing

By default, width and height set the size of the content area only, and padding and border are added on top. That is the content-box model, and it makes layouts awkward: two boxes of width: 50% with any padding no longer fit side by side. With box-sizing: border-box, the width includes the padding and border, and the content area shrinks to make room.

box-sizingWidth on screenContent width
content-box244px200px
border-box200px156px

6Problem

content-box total width

content-box: width 300px, padding 20px, border 5px. Total width?

px

Hint

Add padding and border on both sides.

Solution

Answer: 350 px

content-box adds padding and border on both sides to the width: 300 + 20 + 20 + 5 + 5 = 350.

7Problem

border-box content width

border-box: width 300px, padding 20px, border 5px. How wide is the content area?

px

Hint

Take padding and border away from 300.

Solution

Answer: 250 px

300 minus 2 times 20 minus 2 times 5 = 250.

8Explain

Margins that collapse

One rule surprises nearly everyone. When the bottom margin of one block meets the top margin of the next, they do not add up; the larger one wins. A paragraph with margin-bottom: 20px followed by a heading with margin-top: 30px ends up 30 pixels away, not 50. This is called margin collapsing, and it only happens to vertical margins between blocks in normal flow, not inside flex or grid containers.

Your code
<style>
  p { margin: 0; background: #e8eefa; }
  .a { margin-bottom: 20px; }
  .b { margin-top: 30px; }
</style>
<p class="a">Bottom margin 20px</p>
<p class="b">Top margin 30px: the gap is 30px, not 50px</p>
Preview

9Problem

20px meets 30px

A margin-bottom of 20px meets a margin-top of 30px. How big is the gap?

px

Hint

The larger margin wins.

Solution

Answer: 30 px

Vertical margins collapse: the larger one, 30, wins.

10Practice

A card exactly 240px wide

Make .card exactly 240px wide in total: box-sizing: border-box, width: 240px, padding: 16px and a 2px solid border. Centre it with margin: 0 auto.

Checks
  • border-box
  • width 240px
  • padding 16px
  • 2px border
  • solid border
Your code
<style>
  .card {
    background: #fff4d6;
  }
</style>

<div class="card">
  <h2>Rye loaf</h2>
  <p>Dark, dense and baked every morning.</p>
</div>
Preview
Hint

box-sizing: border-box keeps padding and border inside the 240px.

A solution
<style>
  .card {
    background: #fff4d6;
    box-sizing: border-box;
    width: 240px;
    padding: 16px;
    border: 2px solid #333;
    margin: 0 auto;
  }
</style>

<div class="card">
  <h2>Rye loaf</h2>
  <p>Dark, dense and baked every morning.</p>
</div>

11Practice

Tags with real height

The tags are inline, so vertical padding does not give them real height. Make them inline-block with padding: 4px 8px.

Checks
  • inline-block
  • padding top 4px
  • padding left 8px
Your code
<style>
  .tag {
    background: #e8eefa;
    border-radius: 4px;
  }
</style>

<p>Filed under <span class="tag">bread</span> <span class="tag">rye</span> <span class="tag">recipes</span></p>
Preview
Hint

display: inline-block keeps them in the line but gives them a real box.

A solution
<style>
  .tag {
    background: #e8eefa;
    border-radius: 4px;
    display: inline-block;
    padding: 4px 8px;
  }
</style>

<p>Filed under <span class="tag">bread</span> <span class="tag">rye</span> <span class="tag">recipes</span></p>

12Explain

Common mistakes

Mistake
<style>
  .card { border: 2px #333; padding: 8px; }
</style>
<div class="card">Rye</div>
Fixed
<style>
  .card { border: 2px solid #333; padding: 8px; }
</style>
<div class="card">Rye</div>

A border needs a style. Without solid (or dashed, dotted …) the style is none and nothing is drawn.

13

Summary

  • Content, padding, border, margin.
  • content-box adds padding and border to width; border-box includes them.
  • Shorthand goes clockwise from the top; touching vertical margins collapse.

Next unitTypography
Full text · The whole lesson as one text

Every element is a box

The CSS box model describes every element as a box made of four layers: content, padding, border and margin. To lay out a page, the browser turns every element into a rectangle. Each box has four layers, from the inside out. The content area holds the text or image. Padding is space inside the box, between the content and the border, and it takes the box's background colour. The border is a line around the padding. Margin is space outside the border that pushes other boxes away; it is always transparent. The browser developer tools draw exactly this diagram when you inspect an element, with the numbers filled in.

Content, padding, border and margin. With the default content-box sizing, width sets only the content: 200 + 16 + 16 + 2 + 2 = 236 pixels wide.

Shorthand for four sides

padding, margin and border-width take one to four values, going clockwise from the top. padding: 16px sets all four sides. padding: 8px 16px sets top and bottom to 8 and left and right to 16. padding: 4px 8px 12px 16px sets top, right, bottom and left in that order. Each side can also be set on its own, as in margin-top. border: 2px solid #333 sets width, style and colour together; without a style such as solid or dashed no border is drawn at all.

box-sizing: the setting almost everyone changes

By default, width and height set the size of the content area only, and padding and border are added on top. That is the content-box model, and it makes layouts awkward: two boxes of width: 50% with any padding no longer fit side by side. With box-sizing: border-box, the width includes the padding and border, and the content area shrinks to make room. Many stylesheets start by applying it to every element with *, *::before, *::after { box-sizing: border-box; }.

Worked example: two boxes sit side by side in a 600px container. Each has width: 50%, padding: 10px and a 1px border. With content-box each box is 300 + 20 + 2 = 322px wide, so the pair needs 644px and the second box wraps to the next line. With border-box each box is exactly 300px, and both fit.

Total width at a glance

box-sizingWidth on screenContent width
content-box244px200px
border-box200px156px

Both rows use width: 200px, padding: 20px and border: 2px. With content-box the padding and border are added outside the 200px; with border-box they are taken out of it. Margin is never part of this total. It is extra space outside the border, so add it separately when you work out how much room a box takes up in the layout.

Block and inline boxes

Boxes behave differently depending on their display value. Block boxes, such as p, h1 and section, start on a new line and stretch to fill the width of their container. Inline boxes, such as a, strong and span, sit inside a line of text; width and height do not apply to them, and their vertical padding and margin do not push other lines away. display: inline-block is in between: the box flows inside the line like a word but respects width, height and padding, which suits small labels and buttons.

Margins that collapse

One rule surprises nearly everyone. When the bottom margin of one block meets the top margin of the next, they do not add up; the larger one wins. A paragraph with margin-bottom: 20px followed by a heading with margin-top: 30px ends up 30 pixels away, not 50. This is called margin collapsing, and it only happens to vertical margins between blocks in normal flow, not inside flex or grid containers. To centre a block horizontally, give it a width or max-width and margin: 0 auto: the two automatic side margins share the free space equally.

Outlines are drawn outside the border and take up no space, which is why browsers use them for the keyboard focus ring. If you remove an outline with outline: none, replace it with another clear focus style.
Key points
  • Content, padding, border, margin.
  • content-box adds padding and border to width; border-box includes them.
  • Shorthand goes clockwise from the top; touching vertical margins collapse.

Practice questions from The box model on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17