Explore programmers.im
css.programmers.im

The official box model

The CSS box model

“Everything in CSS has a box around it, and understanding these boxes is key to being able to create more complex layouts with CSS, or to align items with other items.” [source]

margin
border
padding
content

Content box. The area where your content is displayed; size it using properties like width and height. [source]

Tap a layer to read its official job. The diagram is ours; the wording is from MDN’s box-model lesson.

The standard model

By default, browsers use the standard box model. If you set width and height, those values size the content box. Any padding and borders are then added to get the total size taken up by the box. [source]

MDN works an official example:

 .box {
  width: 350px;
  height: 150px;
  margin: 10px;
  padding: 25px;
  border: 5px solid black;
} 

The actual space taken up by the box is 410px wide (350 + 25 + 25 + 5 + 5) and 210px high (150 + 25 + 25 + 5 + 5). The margin is not counted towards the actual size of the box. It affects the space the box takes up on the page, but only the space outside it. The box’s area stops at the border. [source]

The alternative model

In the alternative box model, any width is the width of the visible box on the page. The content area is that width minus the padding and the border. MDN calls this convenient, because you do not have to add the border and padding to get the real size of the box. To turn it on for an element, set box-sizing: border-box. [source]

Next: a short official vocabulary

Keep the lights on

Enjoyed the style?

No pop-ups. No intrusive advertising. Just interesting stuff. A little support helps the next programmers site get built.