Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 139 ยท The Box Model

The Box Model

Learn the CSS box model, the four layers around every element, and how the browser adds content, padding and border together to get an element's real size.

Every element the browser draws is a rectangle. A heading, a paragraph, a button, even a single link inside a sentence: each one gets a box. The box model is the set of rules that decides how big that box is and how much space it takes up around it.

Most layout surprises in CSS come from the box model. You set width: 300px and the element ends up 342 pixels wide. You add a margin and the gap does not grow. Once you can see the four layers of a box and do the arithmetic yourself, those surprises turn into simple sums. This lesson gives you the whole picture; the rest of the chapter zooms in on each part.

The four layers of a box

Think of a box as four nested rectangles, from the inside out:

  1. Content: the text, image or child elements. Its size comes from width and height (or from the content itself).
  2. Padding: transparent space between the content and the border. The element's background shows through it.
  3. Border: a line (or nothing) drawn around the padding.
  4. Margin: transparent space outside the border that pushes other boxes away. The background never shows in the margin.

Here is a box with all four layers set:

CSS
.card {
  width: 200px;
  padding: 20px;
  border: 5px solid navy;
  margin: 30px;
}

In DevTools, the Computed panel draws this as a set of colored rings: content in the middle, then padding, border and margin. You met that diagram in Inspecting CSS with DevTools. The live example below draws the same thing with real elements, so you can see each layer.

See every layer of a box

The white strip is exactly the content area. The blue around it is padding, which is filled by the element's background. The navy ring is the border. The dashed line marks how far the margin reaches; nothing is painted there, it is just empty space that keeps neighbors away.

How the browser calculates size

By default, width and height set the size of the content area only. Padding and border are added on the outside. So the box from the first example is wider than 200 pixels:

Layer Left Right Running total
Content 200px
Padding 20px 20px 240px
Border 5px 5px 250px
Margin 30px 30px 310px

That gives you two different numbers worth knowing:

  • The border box is content + padding + border. That is 250 pixels here, the part you can actually see.
  • The margin box is the border box plus margins. That is 310 pixels, the space the element claims in the layout.

Height works the same way, using top and bottom values instead of left and right.

You can ask the browser for these numbers in JavaScript. getBoundingClientRect() returns the border box, and getComputedStyle() returns every individual value. The next example prints the full sum, so you can change the CSS and watch the totals update.

Do the box model arithmetic

Try setting padding: 0 or border-width: 20px and running it again. The content width never changes, but the border box does. That is the default behavior, called content-box sizing. Most developers switch to a friendlier model with one line of CSS, which is the topic of the next lesson, box-sizing.

getComputedStyle() reports width as the content width only while box-sizing is content-box. With border-box it reports the border-box width instead. The DevTools box diagram always shows the content size in the middle, whatever the sizing mode.

Block boxes and inline boxes

Not every box uses all four layers in the same way. What matters is how the element is displayed.

Block boxes (<div>, <p>, <h1>, <section> and friends) start on a new line and, by default, stretch to fill the width of their container. Width, height, padding, border and margin all work as described above.

Inline boxes (<a>, <span>, <strong>, <em>) sit inside a line of text. They behave differently:

  • width and height are ignored. The content decides the size.
  • Horizontal padding, border and margin push neighboring text sideways.
  • Vertical padding and border are painted, but they do not push lines apart. They can overlap the line above or below.
  • Vertical margins have no effect at all.
Inline boxes ignore vertical space

The green span spills into the lines around it. The blue one uses display: inline-block, which keeps it in the flow of text but gives it a full box model, so its padding takes up real space. You will learn the details of each display type in The display Property.

Watching the box in DevTools

When a layout looks wrong, the fastest fix is usually to look at the box:

  1. Right-click the element and choose Inspect.
  2. Open the Computed tab. The box diagram at the top shows margin, border, padding and content with their exact numbers.
  3. Hover any layer in the diagram. The browser highlights that layer on the page: margin in orange, padding in green, content in blue.
  4. Double-click a number in the diagram to edit it live.

This is how you find the mystery gap that turns out to be a default margin on a <p>, or the extra width that comes from padding you forgot about.

A quick trick for seeing every box at once: temporarily add * { outline: 1px solid red; } to your stylesheet. Outlines do not take up space, so they show the boxes without changing the layout. You will learn why in Outlines.

Margins are different from the other layers

Padding and border belong to the element. Margin is space between elements, and that gives it a few special powers:

  • Margins can be negative, pulling an element toward its neighbors. Padding and border cannot.
  • margin: auto can center a block horizontally by sharing leftover space.
  • Vertical margins between blocks can collapse: two 20-pixel margins that touch may produce a 20-pixel gap, not a 40-pixel one.

Each of these gets a full explanation in Margins. For now, remember that margin is outside the border, is always transparent, and does not count toward an element's visible size.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A box has width: 300px, padding: 10px, border: 2px solid and the default box-sizing. How wide is its visible border box?

    With content-box sizing you add both sides: 300 + 10 + 10 + 2 + 2 = 324 pixels. Margin is not part of the border box.
  2. 2. Which layer shows the element's background color?

    Backgrounds paint the content and padding areas and extend under the border by default. The margin area is always transparent.
  3. 3. What does getBoundingClientRect().width measure on a normal, untransformed element?

    It returns the border box, which is content + padding + border. Margins are not included.
  4. 4. You set height: 50px and margin-top: 20px on a <span>. What happens?

    Inline boxes ignore width, height and vertical margins. Use display: inline-block or block if you need them.
  5. 5. Which statement about margins is true?

    Negative margins are allowed and pull elements closer together. Padding and border widths cannot be negative.

Summary

  • Every element is a box with four layers: content, padding, border and margin.
  • By default, width and height size the content only; padding and border are added on top.
  • The border box (content + padding + border) is what you see; the margin box adds the margins and is what the layout reserves.
  • getBoundingClientRect() reports the border box; getComputedStyle() reports each individual value.
  • Inline boxes ignore width, height and vertical margins, and their vertical padding does not push lines apart.
  • The Computed panel in DevTools shows and edits every layer of the box.

Next: box-sizing, the one-line change that makes width mean what you expect.