What is the CSS Box Model?
Every element on a web page is a rectangular box. The CSS box model describes the space around and within each element. Understanding the box model is absolutely essential for creating proper layouts and spacing in CSS. It's one of the most important concepts in CSS.
The box model consists of four layers, from outside to inside:
- Margin: Space OUTSIDE the border
- Border: The edge of the element
- Padding: Space INSIDE the border
- Content: The actual content (text, images, etc.)
1. Margin (Outer Spacing)
Margin creates space OUTSIDE the element's border. It pushes other elements away:
.box {
margin: 20px; /* 20px on all sides */
margin-top: 10px; /* 10px on top only */
margin-bottom: 15px; /* 15px on bottom only */
margin-left: 5px; /* 5px on left only */
margin-right: 5px; /* 5px on right only */
}
Margin Shorthand (saves typing):
/* All four sides: top, right, bottom, left */
margin: 10px 20px 30px 40px;
/* Vertical (top/bottom) and horizontal (left/right) */
margin: 10px 20px; /* 10px top/bottom, 20px left/right */
/* All four sides the same */
margin: 15px;
Real Example:
.card {
margin: 20px; /* Pushes other cards 20px away */
background-color: white;
}
2. Border (The Edge)
The border is the line around the element. It sits between margin and padding:
.box {
border: 2px solid black; /* Width, style, and color */
}
Border Styles:
border: 2px solid black; /* Solid line */
border: 2px dashed red; /* Dashed line */
border: 2px dotted blue; /* Dotted line */
border: none; /* No border */
Round Corners with border-radius:
.box {
border: 2px solid black;
border-radius: 8px; /* Slightly rounded corners */
}
3. Padding (Inner Spacing)
Padding creates space INSIDE the element, between the content and the border. It's part of the clickable area for buttons:
.button {
padding: 10px 20px; /* 10px top/bottom, 20px left/right */
background-color: blue;
color: white;
}
Padding Shorthand (same as margin):
padding: 15px; /* All sides */
padding: 10px 20px; /* Top/bottom, left/right */
padding: 10px 20px 30px 40px; /* Top, right, bottom, left */
Real Examples:
.card {
padding: 20px; /* Space inside the card */
background-color: white;
border: 1px solid gray;
}
.button {
padding: 10px 15px; /* Comfortable space around button text */
background-color: green;
color: white;
border-radius: 5px;
}
4. Content (The Actual Content)
The content is the actual text, images, or other elements inside the box. The size of the content area is determined by width and height properties.
Putting It All Together - Visual Diagram
┌─────────────────────────────────────────┐ ← Margin edge
│ Margin (20px) │
│ ┌───────────────────────────────────┐ │ ← Border edge
│ │ Border (2px solid) │ │
│ │ ┌─────────────────────────────┐ │ │ ← Padding edge
│ │ │ Padding (15px) │ │ │
│ │ │ ┌───────────────────────┐ │ │ │ ← Content edge
│ │ │ │ CONTENT │ │ │ │
│ │ │ │ (Text, images, etc) │ │ │ │
│ │ │ └───────────────────────┘ │ │ │
│ │ └─────────────────────────────┘ │ │
│ └───────────────────────────────────┘ │
└─────────────────────────────────────────┘
The box-sizing Property (Very Important!)
By default, width and height only apply to the content area. Padding and border are added ON TOP of the width/height. This can be confusing! Use box-sizing to fix this:
/* Default behavior - confusing! */
.box {
width: 100px;
padding: 10px;
border: 2px solid black;
/* Actual size: 100 + 10 + 10 + 2 + 2 = 124px! */
}
/* Better - padding and border are included in width */
.box {
width: 100px;
padding: 10px;
border: 2px solid black;
box-sizing: border-box;
/* Actual size: 100px (padding and border included!) */
}
Best Practice: Use box-sizing: border-box on everything. Add this to the top of your CSS:
* {
box-sizing: border-box;
}
This makes width calculations much simpler and more intuitive. Most professional CSS projects start with this rule.
Practical Examples
Example 1: Creating a Card
.card {
width: 300px;
padding: 20px; /* Space inside the card */
margin: 15px; /* Space outside the card */
background-color: white;
border: 1px solid #ddd;
border-radius: 8px;
box-sizing: border-box;
}
Example 2: Styling a Button
.button {
padding: 12px 24px; /* Comfortable padding */
margin: 10px; /* Space between buttons */
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
box-sizing: border-box;
cursor: pointer;
}
Example 3: Spacing Between Elements
.section {
margin-bottom: 30px; /* Space below each section */
}
.section h2 {
margin-top: 0; /* Remove default h2 margin */
margin-bottom: 15px; /* Space between heading and content */
}
Common Margin Problems and Solutions
Margin Collapse
When two elements with margin touch, sometimes they don't add together—the larger margin wins. This is called margin collapse:
/* These margins don't add to 40px—the 30px wins */
.box1 { margin-bottom: 20px; }
.box2 { margin-top: 30px; }
/* Solution: Use padding instead */
.box1 { margin-bottom: 20px; }
.box2 { margin-top: 0; padding-top: 30px; }
Summary
You now understand:
- The four layers of the box model: margin, border, padding, and content
- How to use margin to create space outside elements
- How to use padding to create space inside elements
- How box-sizing: border-box simplifies width calculations
- How to apply these concepts to real elements like cards and buttons
The box model is the foundation of CSS layout. Once you master it, creating properly spaced and sized elements becomes much easier. Next, we'll learn about colors and typography.