Thuta Learning
CSS
IntermediateWeb DevelopmentBeginner

Box Model Intro

CSSLesson 1025 min

What you'll walk away with

  • Name the four layers of the box model in order from the inside out
  • Calculate an element's real width under content-box and border-box
  • Explain why width: 100% plus padding overflows, and fix it
  • Read the DevTools box model panel to diagnose sizing problems

Explain it simply

Explain it simply

Picture a parcel. In the middle is the item you are shipping (the content). Foam wrapped around it keeps it safe (the padding). A cardboard box holds everything (the border). And when you put it on a shelf, you leave some empty space between it and the other parcels (the margin).

In CSS every element, whether a div, a paragraph, a button or an image, is a box with these same four layers. Most layout bugs come from not knowing whether a mysterious 20px is padding or margin.

Problem → Why → Solution

The problem: width: 100% that still overflows

Inside a 300px sidebar, a search input gets width: 100% so it fills the sidebar. Then padding: 12px and a 2px border are added to make it nicer to type in. The input is now 328px wide, sticks 28px out of the sidebar's right edge, and on a phone the whole page starts scrolling sideways.

sidebar.css
.sidebar { width: 300px; }

.sidebar .search {
  width: 100%;        /* 300px ... of CONTENT */
  padding: 12px;      /* + 24px */
  border: 2px solid;  /* + 4px  = 328px on screen */
}

Why does this happen?

Under the default box-sizing, content-box, width means the content area only. Padding and border are added outside it, so 300 + 24 + 4 = 328px. Switch to box-sizing: border-box and width measures to the outer edge of the border, the padding and border are carved out of the inside, and the input stays exactly 300px.

Core concept: four layers and box-sizing

Read from the inside out, a box is content, padding, border, margin. The content area holds the text or image. Padding is the space between the content and the border, and the element's background extends into it. The border is the box's edge line, and by default the background paints underneath it too. Margin is always-transparent space outside the border that pushes boxes apart; the element's background never reaches it.

text
box-model.txt
-------------
+-------------------- margin (transparent) --------------------+
|   +---------------- border ---------------------------+      |
|   |   +------------ padding (background) ---------+   |      |
|   |   |                                           |   |      |
|   |   |        content  (width x height)          |   |      |
|   |   |                                           |   |      |
|   |   +-------------------------------------------+   |      |
|   +---------------------------------------------------+      |
+--------------------------------------------------------------+
content-box: width = content only
border-box:  width = content + padding + border

box-sizing decides which layer width and height measure. Under the default content-box, width: 200px means a 200px content area, with padding and border added outside it. Under border-box, width: 200px is the distance from outer border edge to outer border edge, and the content gets whatever is left. In both modes margin is never part of width; it is always added outside.

One more detail saves a lot of trouble: a block element such as a div or p with the default width: auto already stretches to fill its parent, and its padding and border fit inside that space automatically, in either mode. The overflow in the problem above only happens because width: 100% was set explicitly. So before writing width: 100% on a block element, ask whether you need it at all; inputs and buttons are the exception, since they do not stretch on their own.

Most modern projects put a one-line reset at the top of the stylesheet: *, *::before, *::after { box-sizing: border-box; }. Then when the design says "the card is 320px", you write width: 320px and the size no longer changes when padding does. The Computed tab in browser DevTools draws the box model with the pixel value of every layer, so you can see at once where a mystery gap comes from.

Mental model: the layers of a parcel
  1. Content: the item itself
  2. Padding: inner foam, has background
  3. Border: the cardboard edge
  4. Margin: empty shelf space outside

border-box means: the cardboard box has a fixed size, so more foam leaves less room for the item.

General pattern

css
*, *::before, *::after {
  box-sizing: border-box;   /* width = up to the border's outer edge */
}

selector {
  width: total-width;
  padding: inner-space;
  border: thickness style color;
  margin: outer-space;
}

The reset switches every element to border-box; each rule then lists width, padding, border and margin from the inside out. The rendered width equals width, and margin is added outside.

Example: one box with all four layers

This example gives each of the four layers a different value, so you can tell them apart when you open DevTools. Because of box-sizing: border-box, work out yourself how much width is left for the content.

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid red;
  margin: 30px;
  box-sizing: border-box;
}

Code breakdown

  1. Line 2width: 200px;

    Because of border-box, this 200px is the total from outer border edge to outer border edge.

  2. Line 3padding: 20px;

    20px on all four sides. Left plus right takes 40px out of the 200px.

  3. Line 4border: 5px solid red;

    Another 5px on each side, leaving 200 − 40 − 10 = 150px for the content.

  4. Line 5margin: 30px;

    30px of transparent space outside the border on every side. It is not part of width, so the box occupies 260px horizontally.

  5. Line 6box-sizing: border-box;

    Delete this line and it reverts to content-box: the box grows to 200 + 40 + 10 = 250px.

You should see
A box with a 5px red border that is exactly 200px wide from border edge to border edge (150px content + 2 × 20px padding + 2 × 5px border). A 30px margin surrounds it, so it takes up 260px of horizontal space in total.

content-box and border-box side by side

Working out the same declarations in both modes makes the difference obvious. The table uses width: 300px; padding: 20px; border: 5px; margin: 10px;.

Measurementcontent-boxborder-box
Content width300px250px
Border edge to border edge350px300px
Total space with margin370px320px

Outline and box-shadow take no space

Unlike a border, outline and box-shadow are drawn outside the box without changing its layout size. That is why they are popular for hover and focus rings: nothing around them jumps.

In real projects

Where this shows up in real projects

CSS resets such as Tailwind's Preflight and modern-normalize, as well as Bootstrap, all ship the border-box reset by default. If you have used a framework, you were probably relying on border-box without knowing it. You meet content-box again in projects without a framework, or in places like email templates.

Day-to-day debugging leans on the box model as well. Questions like "why is the gap between these two cards so big?" or "why does the page scroll sideways on phones?" are answered by selecting the element in DevTools and reading its box model diagram. Hovering it highlights content in blue, padding in green and margin in orange right on the page. Form inputs and sidebars combine width: 100% with padding all the time, so without the reset, overflow bugs keep coming back.

Common mistakes

Common mistakes

1. Assuming width already includes padding

In a project without the reset, two columns with width: 50% and padding: 16px each become 50% + 32px wide and no longer fit on one row. Add the box-sizing: border-box reset, or move the padding onto an inner element.

2. Expecting margin to show the background

Increasing margin to make a card's colored area bigger seems to do nothing: margin is always transparent, so the parent's background shows there. To enlarge the colored area inside the box, increase padding.

3. Leaving pseudo-elements out of the reset

With only * { box-sizing: border-box; }, ::before and ::after stay content-box, and decorative icons with borders come out the wrong size. List *, *::before, *::after together.

Best practices

Best practices

  • Make the border-box reset the very first rule in every new project's stylesheet.
  • Do not write width: 100% on block elements out of habit; width: auto fills the parent by itself.
  • When a size looks wrong, open the DevTools box model panel before guessing at CSS changes.
  • For hover and focus lines, prefer outline or box-shadow over border so the layout does not jump.

Mini challenge

Mini challenge: a 320px signup card

The design says the signup card is exactly 320px wide including its border. The email input inside must fill the card's padded area without poking out on the right by even one pixel.

  • A reset that makes every element, pseudo-elements included, border-box
  • Card: width 320px, padding 24px, 2px border, margin 40px auto
  • Input: width 100%, padding 10px 12px, 1px border
  • Predict the input's rendered width, then confirm it in DevTools
signup.html
<!doctype html>
<html lang="en">
<style>
  /* TODO: box-sizing reset for all elements and pseudo-elements */

  /* TODO: .signup - 320px total width, 24px padding, 2px border, centered */

  /* TODO: .signup input - fill the card's content area,
     with its own padding and a 1px border */
</style>
<form class="signup">
  <label for="email">Email</label>
  <input id="email" type="email" placeholder="you@example.com">
</form>
</html>

Expected: The card is exactly 320px wide including its border and sits centered on the page. The input is 268px wide and its right edge lines up exactly with the inner edge of the card's padding.

Show hint

The input's width: 100% refers to the card's content area. Work out 320 − 2×24 − 2×2 to see how much is left.

Show solution
signup.html
<!doctype html>
<html lang="en">
<style>
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }
  .signup {
    width: 320px;
    padding: 24px;
    border: 2px solid #94a3b8;
    margin: 40px auto;
  }
  .signup input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #64748b;
  }
</style>
<form class="signup">
  <label for="email">Email</label>
  <input id="email" type="email" placeholder="you@example.com">
</form>
</html>

Quick quiz 1 / 3

With the default content-box, how wide is a box with width: 300px; padding: 20px; border: 5px solid; margin: 10px; measured border edge to border edge?

Quick quiz 2 / 3

With box-sizing: border-box; width: 300px; padding: 20px; border: 5px solid;, how wide is the content area?

Quick quiz 3 / 3

For a box with background-color: gold;, which layers does the background cover by default?

What to remember

  • Every box has four layers, inside out: content, padding, border, margin.
  • Under content-box width is the content only; under border-box it reaches the border's outer edge. Margin is never part of width.
  • The *, *::before, *::after { box-sizing: border-box; } reset prevents width: 100% plus padding from overflowing.
  • The background reaches under padding and border; margin is always transparent.

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Box Model Intro | Thuta Learning