CSS Box Model
Understand the CSS box model, including content, padding, border, and margin, and how they affect element sizing and spacing.
The CSS box model is a fundamental concept that describes how elements are structured and sized on a webpage. Every HTML element is essentially a rectangular box, and the box model defines how that box is composed.
The Box Model Components
The box model consists of four main parts from inside to outside:
- Content - The actual content of the element (text, images, etc.)
- Padding - Space between the content and the border
- Border - A line around the padding and content
- Margin - Space outside the border, separating the element from others
┌─────────────── Margin ───────────────┐
│ ┌─────────── Border ─────────────┐ │
│ │ ┌──────── Padding ────────┐ │ │
│ │ │ │ │ │
│ │ │ Content │ │ │
│ │ │ │ │ │
│ │ └──────────────────────────┘ │ │
│ └─────────────────────────────────┘ │
└───────────────────────────────────────┘Box Model Example
.box {
width: 300px;
height: 200px;
padding: 20px;
border: 5px solid black;
margin: 30px;
}In this example:
- Content area: 300px × 200px
- With padding: 340px × 240px (adds 20px on each side)
- With border: 350px × 250px (adds 5px on each side)
- Total space occupied with margin: 410px × 310px (adds 30px on each side)
Box-Sizing Property
The box-sizing property controls how the total width and height of an element is calculated.
content-box (Default)
.content-box {
box-sizing: content-box; /* Default */
width: 300px;
padding: 20px;
border: 5px solid black;
/* Actual width = 300 + 20 + 20 + 5 + 5 = 350px */
}border-box (Recommended)
.border-box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
/* Actual width = 300px (padding and border included) */
}Best Practice
Most developers use box-sizing: border-box globally for easier sizing calculations:
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}This approach is more performant and allows third-party components to opt-out if needed.
Visualizing the Box Model
You can see the box model of any element using browser DevTools:
- Right-click on an element and select "Inspect"
- Look at the "Computed" or "Layout" tab
- You'll see a diagram showing content, padding, border, and margin
Width and Height
The width and height properties set the dimensions of the content area (by default):
.dimensions {
width: 400px;
height: 300px;
min-width: 200px;
max-width: 600px;
min-height: 150px;
max-height: 500px;
}Practical Example
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.container {
width: 600px;
background-color: #f0f0f0;
padding: 20px;
}
.box {
width: 100%;
background-color: white;
padding: 20px;
border: 2px solid #3498db;
margin-bottom: 20px;
}
.box-info {
background-color: #e8f4f8;
padding: 10px;
border-left: 4px solid #3498db;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<h3>Box Model Example</h3>
<p>This box uses border-box sizing.</p>
<div class="box-info">
<strong>Width:</strong> 100% of container<br>
<strong>Padding:</strong> 20px all sides<br>
<strong>Border:</strong> 2px solid
</div>
</div>
</div>
</body>
</html>Common Issues and Solutions
Issue: Elements Overflow Container
/* Problem */
.container {
width: 300px;
}
.child {
width: 300px;
padding: 20px; /* This will overflow! */
}
/* Solution */
.child {
width: 100%;
padding: 20px;
box-sizing: border-box; /* Includes padding in width */
}Issue: Margin Collapse
Vertical margins between elements collapse:
.element1 {
margin-bottom: 20px;
}
.element2 {
margin-top: 30px;
}
/* Actual space between them: 30px (not 50px) */Overflow Property
The overflow property controls what happens when content is too large for its container:
.overflow-examples {
/* Clip content */
overflow: hidden;
/* Show scrollbars */
overflow: scroll;
/* Show scrollbars only when needed */
overflow: auto;
/* Let content overflow (default) */
overflow: visible;
}Display and the Box Model
Different display values affect the box model:
.block {
display: block; /* Full width, creates new line */
}
.inline {
display: inline; /* Only as wide as content, no width/height */
}
.inline-block {
display: inline-block; /* Inline but accepts width/height */
}Best Practices
Box Model Tips
- Use
box-sizing: border-boxglobally for predictable sizing - Be aware of margin collapse for vertical spacing
- Use padding for internal spacing, margin for external spacing
- Remember that inline elements don't respect vertical margin/padding fully
- Use browser DevTools to debug box model issues
- Consider using percentages or flexible units for responsive designs
Summary
Understanding the box model is crucial for:
- Accurate element sizing
- Proper spacing and layout
- Responsive design
- Debugging layout issues
- Creating predictable designs
Master the box model, and you'll have much better control over your layouts!
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
CSS Borders
Master CSS border properties to add, style, and customize borders around elements with colors, styles, widths, and rounded corners.
CSS Margin
Master CSS margins to create space around elements, control layout, center elements, and manage spacing between components.
© 2026CoderrShyamAll Rights Reserved.