html
/

CSS Box Model

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

CSS Box Model

What is the CSS Box Model?

Every HTML element is represented as a box. The CSS box model defines how the size and spacing of elements are calculated.

Box Model Structure

  • Content – The actual content (text, image)
  • Padding – Space inside the element
  • Border – Surrounds padding and content
  • Margin – Space outside the element

Example

CSSRead-only
1
div {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}

Total Width Calculation

Total Width = Content + Padding + Border + Margin

TEXTRead-only
1
200px (content)
+ 40px (padding)
+ 10px (border)
+ 20px (margin)
= 270px total

Box Sizing

The box-sizing property controls how width and height are calculated.

CSSRead-only
1
div {
  box-sizing: border-box;
}

Box-Sizing Types

ValueDescription
content-boxDefault (padding & border added)
border-boxIncludes padding & border in width

Visual Understanding

Think of the box model as layers: content at the center, surrounded by padding, then border, and finally margin.

Best Practices

  • Use box-sizing: border-box for easier layout
  • Maintain consistent spacing
  • Understand margin vs padding usage
  • Avoid unexpected overflow issues

Common Mistakes

  • Forgetting padding affects total size
  • Confusing margin and padding
  • Not using border-box
  • Miscalculating layout width

Conclusion

The CSS box model is fundamental for layout design. Mastering it helps build accurate and responsive UI layouts.

Try it yourself

div {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  box-sizing: border-box;
}

Test Your Knowledge

Q1
of 3

Which is outermost?

A
Content
B
Padding
C
Border
D
Margin
Q2
of 3

Which property controls sizing?

A
display
B
position
C
box-sizing
D
overflow
Q3
of 3

Default box-sizing is?

A
border-box
B
content-box
C
box
D
auto

Frequently Asked Questions

What is the box model?

It defines layout using content, padding, border, and margin.

What is border-box?

It includes padding and border in total width.

Does margin affect size?

Yes, it adds space outside the element.

Previous

css width height

Next

css text

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.