CSS Basics

CSS Box Model

CSS Box Model is the fundamental layout concept that defines how element dimensions are calculated, consisting of four distinct areas: content (the actual content), padding (space around content), border (boundary line), and margin (space outside border) that together determine the total space an element occupies.

What is the CSS box model?

Every element is a rectangular box made of four layers: content, padding, border and margin. By default width sets only the content width, so padding and border are added on top. Setting box-sizing: border-box makes width include padding and border, which is far easier to work with.

*, *::before, *::after {
  box-sizing: border-box;
}
.card {
  width: 300px;      /* total width, including padding + border */
  padding: 20px;
  border: 2px solid #ddd;
  margin: 16px;      /* space outside the box */
}

Box Model Layers

1

Content

The actual content area where text, images, and other elements appear.

width × height
2

Padding

Space between content and border. Inherits the background color.

padding: 20px
3

Border

Line around the padding and content. Can be styled with color, width, and style.

border: 2px solid #333
4

Margin

Outermost layer that creates space between elements. Always transparent.

margin: 15px

Interactive Box Model Demo

Explore the box model with interactive examples. Hover over elements to see measurements, click to toggle sizes, and see the difference between content-box and border-box sizing!

Code Editor

<div class="box content-box">content-box: 200px + padding + border</div>
<div class="box border-box">border-box: exactly 200px wide</div>

Preview

Box Sizing Property

Understanding Box Sizing

The box-sizing property controls how the total width and height of an element is calculated.

/* Default behavior (content-box) */ .element { box-sizing: content-box; width: 200px; padding: 20px; border: 10px solid #333; /* Total width: 200 + 40 + 20 = 260px */ } /* Modern approach (border-box) */ .element { box-sizing: border-box; width: 200px; padding: 20px; border: 10px solid #333; /* Total width: 200px (includes padding and border) */ }

content-box (Default)

  • •Width/height applies only to content
  • •Padding and border add to total size
  • •Can lead to unexpected sizing

border-box (Recommended)

  • •Width/height includes padding and border
  • •More predictable and intuitive
  • •Easier for responsive layouts

Best Practice: Universal Border-Box

/* Apply border-box to all elements */ *, *::before, *::after { box-sizing: border-box; } /* This makes layout calculations much easier! */

This is considered a modern CSS best practice and makes responsive design much easier.

Your Learning Path

Master the CSS box model foundation

What You've Learned

  • ▶Four layers: content, padding, border, margin
  • ▶How total element size is calculated
  • ▶Difference between content-box and border-box
  • ▶Best practices for box-sizing
  • ▶Responsive box model considerations

What's Next

  • ▶CSS Text - Typography and text styling
  • ▶CSS Fonts - Font properties and web fonts
  • ▶CSS Display - Control element display types
  • ▶CSS Position - Advanced positioning techniques

Pro Box Model Tip

Always use border-box for modern layouts. It makes responsive design predictable and eliminates common sizing calculations that lead to layout bugs.

CSS Box Model — step by step

1 / 4
1

Content, Padding, Border, Margin

Every element is a rectangular box with four layers. Content is the innermost area holding text or children. Padding is transparent space inside the border. Border wraps padding. Margin is transparent space outside the border, separating elements from each other.

1 of 4
💡Set box-sizing: border-box Globally
Add a universal reset at the top of your CSS: all elements use border-box sizing. This single rule eliminates the most common source of layout miscalculation where adding padding to a fixed-width element causes it to exceed its container.

🎯 Quick Check

With box-sizing: border-box, what does a width of 200px include?

CSS Box Model: summary

Key points

  • From the inside out, the layers are content, padding (inside the border), border, and margin (outside the border).
  • With the default box-sizing: content-box, rendered width = width + left/right padding + left/right border.
  • With box-sizing: border-box, rendered width = width; padding and border fit inside it.
  • Margins are transparent and take no background. Padding shows the element's background.
  • Vertical margins between block elements collapse into the larger of the two; horizontal margins never do.

Common mistakes

  • Setting width: 100% plus padding on a content-box element, which overflows its parent.
  • Using margin when you want the background colour to extend. Use padding instead.
  • Being surprised by margin collapse between a parent and its first or last child.

Frequently asked questions

›What does box-sizing: border-box do?

It makes width and height include padding and border. A 300px wide box stays 300px no matter how much padding you add, which is why most projects apply it to every element.

›What is the difference between margin and padding?

Padding is the space inside the border, between the border and the content, and it shows the background. Margin is the space outside the border that separates the element from its neighbours, and it's always transparent.

›What is margin collapsing?

When two vertical margins meet, for example between stacked paragraphs, they merge into one margin the size of the larger. It doesn't happen in flex or grid containers, or with horizontal margins.

›How do I see the box model in the browser?

Open DevTools (F12), select an element, and look at the Computed tab. It shows a diagram of the content, padding, border and margin with their exact sizes.