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
Content
The actual content area where text, images, and other elements appear.
Padding
Space between content and border. Inherits the background color.
Border
Line around the padding and content. Can be styled with color, width, and style.
Margin
Outermost layer that creates space between elements. Always transparent.
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.
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
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
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.
🎯 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 acontent-boxelement, 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.