Skip to content

The Box Model (padding · margin · box-sizing · overflow) ​

This page gathers box-related properties in one place: the basic structure of the box model, setting padding/margin and margin collapsing, and border, box-sizing and overflow.

The CSS Box Model ​

Every element on a web page is made of an invisible rectangular box. This is called the Box Model. A single box is made of 4 areas layered from the inside out.

┌─────────────────── margin (outer space) ─────────────────┐
│  ┌───────────────── border (edge) ───────────────────┐  │
│  │  ┌─────────────── padding (inner space) ─────────┐ │  │
│  │  │           content (actual content)          │ │  │
│  │  └─────────────────────────────────────────┘ │  │
│  └───────────────────────────────────────────────┘  │
└───────────────────────────────────────────────────────┘
  • content: the area that holds the actual content, such as text or images
  • padding: the inner space between the content and the border
  • border: the edge that wraps the box
  • margin: the outer space that pushes other boxes away

If you compare a box to packing an item in a shipping box, content is the item, padding is the cushioning, border is the wall of the box, and margin is the gap you leave between boxes.

You can set a box's size directly with the width and height properties, and padding/margin can be set differently for top, bottom, left and right. In the Computed tab of the developer tools, you can see a diagram of how many px each of the 4 box model areas of the selected element actually is.

padding, margin and Margin Collapsing ​

Setting padding and margin in one line ​

For both padding (inner space) and margin (outer space), you can write the top/right/bottom/left values in order on a single line. The order is clockwise: top → right → bottom → left.

css
padding: 16px 8px 24px 10px;

Writing it out with the per-direction properties means exactly the same thing.

css
padding-top: 16px;
padding-right: 8px;
padding-bottom: 24px;
padding-left: 10px;

margin works the same way.

css
margin: 16px 8px 24px 10px;
/* same as below */
margin-top: 16px;
margin-right: 8px;
margin-bottom: 24px;
margin-left: 10px;

Margin Collapsing ​

Vertical margins (margin-top, margin-bottom) overlap instead of adding up. When the vertical margins of two neighboring elements meet, only the larger of the two is applied. This is called margin collapsing.

If you don't get as much vertical spacing as you wanted, it's most likely because of margin collapsing, not because you miscalculated the margins.

It happens between parent and child too ​

html
<div id="a">a</div>
<div id="b">
  <div id="c">c</div>
  b
</div>
css
#a { margin: 30px; }
#b { margin: 20px; }
#c { margin: 40px; }

#b and #c have a parent-child relationship. If #b has no border or padding of its own, the child #c's top margin (40px) overlaps with the parent #b's top margin (20px), and the larger of the two, 40px, is applied. This 40px then overlaps with the bottom margin (30px) of the neighboring #a, so in the end the actual gap between #a and #b is 40px.

However, if the parent element has padding or a border, margin collapsing between parent and child does not happen. If vertical spacing comes out differently from what you expected, suspect margin collapsing first.

border, box-sizing, overflow ​

border and border-radius ​

border is the property that creates an edge (line) around an element.

css
.box {
  border: 2px solid #333333;
  /*      width style color */
}

To round the corners, use border-radius.

css
.box {
  border-radius: 12px;
}

border and border-radius are used everywhere, on cards, buttons and more, so they are well worth remembering.

box-sizing ​

In the box model, width/height by default refer to the size of the content area only. So even if you set width: 200px, once padding and border are added, the actual box becomes larger than 200px.

css
.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  /* actual width = 200 + 20*2 + 5*2 = 250px */
}

When building layouts, this calculation often makes boxes bigger than expected and breaks the arrangement. In that case, setting box-sizing: border-box; changes width/height to mean the total size including padding and border.

css
.box {
  box-sizing: border-box;
  width: 200px; /* exactly 200px in reality, including padding and border */
  padding: 20px;
  border: 5px solid black;
}

In short, because it makes layout calculations much more predictable, many projects start with box-sizing: border-box as if it were the default.

overflow ​

When you put long content into a small box (container), the content can spill out of the box. This is called overflow. The overflow property lets you decide how to handle the overflowing content.

css
.container {
  overflow: hidden;  /* cut off and hide the overflowing part */
  /* or */
  overflow: scroll;  /* add a scrollbar so the overflowing part can be viewed */
}

If you want a horizontal scroll, use overflow-x. By the way, mouse users can scroll horizontally by holding the Shift key while turning the wheel. This is a technique often used in practice to fit long text, such as terms and conditions, neatly inside a small box.

Summary ​

ConceptOne-line summary
Box modelcontent → padding → border → margin, from inside to outside
padding / marginInner/outer space; can be set at once in top-right-bottom-left order
Margin collapsingVertical margins don't add up; they overlap and only the larger one applies
border-radiusRounds the corners of the border
box-sizing: border-boxIncludes padding and border in width/height to make calculations easy
overflowHandles content larger than the box by cutting it off (hidden) or scrolling (scroll)

Reference: personal Notion study notes