Skip to content
BENIBIX Learn

CSS Box Model

Development🌐 Web DevelopmentBrowser Code

Also known as Box Model, Margin and Padding, box-sizing

Each part of a page is a box. Space and a border wrap around what is in it.

Think of it like

Think of it like a framed photo on a wall. The photo, the mat around it, the frame, then the gap to the next frame.

Example

Emma sets a box to 300px wide with 20px of padding. It shows up 340px wide, until she adds box-sizing: border-box.

Why it matters

Every gap and every size on a page comes from these four layers. Knowing them ends most layout puzzles.

Definition

The CSS box model says every element is a box made of four layers. In the middle is the content, like text or a picture. Padding is the space inside, around the content. The border is the edge you can see. Margin is the space outside, between this box and the next. Width and height count only the content, unless you change box-sizing.

Example code

css
* {
  box-sizing: border-box;
}
With this rule, width includes the padding and border, which is easier to plan.
The content sits in the middle. Then come the padding, the border and the margin.

Common mistake

Mixing up padding and margin. Padding is inside the border, margin is outside it.

Key takeaways

  1. 1
    Four layers. Content, padding, border, margin.
  2. 2
    Inside or outside. Padding inside the border, margin outside.
  3. 3
    Watch the width. border-box makes sizes add up simply.

Learn it properly