CSS Box Model
Learn about the CSS box model and how margin, padding, borders, and content affect layout.
CSS Box Model Overview
The CSS Box Model is a fundamental concept that describes how every HTML element is represented as a rectangular box with specific properties that determine its size, spacing, and layout.
Box Model Components:
- Content: The actual content of the element
- Padding: Space between content and border
- Border: The border surrounding padding
- Margin: Space outside the border
Total Element Size:
- Width: content-width + padding + border
- Height: content-height + padding + border
- Total Width: width + margin
- Total Height: height + margin
Key Concepts:
- Every element is a rectangular box
- Box model affects layout and spacing
- Understanding box model is crucial for precise layouts
- Modern CSS offers different box-sizing options
Box Model Components
Let's explore each component of the box model in detail.
| Component | Description | CSS Properties | Visual Effect |
|---|---|---|---|
| Content | The actual content area where text and images appear | width, height |
Determines the base size |
| Padding | Space between content and border (inside the element) | padding, padding-top, etc. |
Increases internal spacing |
| Border | The border surrounding the padding area | border, border-width, etc. |
Visible boundary |
| Margin | Space outside the border (between elements) | margin, margin-top, etc. |
Controls external spacing |
Box Model Calculation:
/* Total element dimensions calculation */
.total-width = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right
total-height = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom
/* Example with specific values */
.element {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
/* Calculation:
Total Width = 200 + 20 + 20 + 5 + 5 + 10 + 10 = 270px
Total Height = 100 + 20 + 20 + 5 + 5 + 10 + 10 = 170px
*/
Margin Properties
Margin is the space outside an element's border. It creates separation between elements.
| Property | Description | Values | Example |
|---|---|---|---|
margin |
Shorthand for all margins | length | % | auto | margin: 20px; |
margin-top |
Top margin only | length | % | auto | margin-top: 10px; |
margin-right |
Right margin only | length | % | auto | margin-right: 15px; |
margin-bottom |
Bottom margin only | length | % | auto | margin-bottom: 10px; |
margin-left |
Left margin only | length | % | auto | margin-left: 15px; |
Margin Shorthand Examples:
/* One value - all sides */
margin: 20px; /* top:20px, right:20px, bottom:20px, left:20px */
/* Two values - vertical | horizontal */
margin: 10px 20px; /* top:10px, right:20px, bottom:10px, left:20px */
/* Three values - top | horizontal | bottom */
margin: 10px 20px 15px; /* top:10px, right:20px, bottom:15px, left:20px */
/* Four values - top | right | bottom | left */
margin: 10px 15px 20px 25px; /* top:10px, right:15px, bottom:20px, left:25px */
/* Auto values for centering */
margin: 0 auto; /* top:0, right:auto, bottom:0, left:auto */
Margin Collapsing:
Result: The space between elements is 30px (not 50px) due to margin collapsing.
Margin Tips:
- Use
autofor horizontal centering - Negative margins can pull elements closer
- Margins collapse between adjacent elements
- Use percentage margins for responsive spacing
Padding Properties
Padding is the space between an element's content and its border. It increases the internal space of an element.
| Property | Description | Values | Example |
|---|---|---|---|
padding |
Shorthand for all padding | length | % | padding: 20px; |
padding-top |
Top padding only | length | % | padding-top: 10px; |
padding-right |
Right padding only | length | % | padding-right: 15px; |
padding-bottom |
Bottom padding only | length | % | padding-bottom: 10px; |
padding-left |
Left padding only | length | % | padding-left: 15px; |
Padding Shorthand Examples:
/* One value - all sides */
padding: 20px; /* top:20px, right:20px, bottom:20px, left:20px */
/* Two values - vertical | horizontal */
padding: 10px 20px; /* top:10px, right:20px, bottom:10px, left:20px */
/* Three values - top | horizontal | bottom */
padding: 10px 20px 15px; /* top:10px, right:20px, bottom:15px, left:20px */
/* Four values - top | right | bottom | left */
padding: 10px 15px 20px 25px; /* top:10px, right:15px, bottom:20px, left:25px */
Padding vs Margin:
Content has internal space
Element has external space
Border Properties
Borders create visible boundaries around elements and sit between padding and margin.
| Property | Description | Values | Example |
|---|---|---|---|
border |
Shorthand for border properties | width style color | border: 2px solid red; |
border-width |
Border thickness | length | thin | medium | thick | border-width: 3px; |
border-style |
Border line style | solid | dashed | dotted | etc. | border-style: dashed; |
border-color |
Border color | color value | border-color: blue; |
border-radius |
Rounded corners | length | % | border-radius: 10px; |
Border Style Examples:
Border Radius Examples:
/* Individual border properties */
.element {
border-width: 2px;
border-style: solid;
border-color: #333;
}
/* Shorthand border property */
.element {
border: 2px solid #333;
}
/* Individual side borders */
.element {
border-top: 3px dashed red;
border-right: 1px dotted blue;
border-bottom: 2px solid green;
border-left: 4px double purple;
}
/* Border radius */
.element {
border-radius: 10px; /* All corners */
border-radius: 10px 20px; /* top-left & bottom-right | top-right & bottom-left */
border-radius: 10px 20px 30px; /* top-left | top-right & bottom-left | bottom-right */
border-radius: 10px 20px 30px 40px;/* top-left | top-right | bottom-right | bottom-left */
}
Box Sizing Property
The box-sizing property controls how the total width and height of an element are calculated.
content-box (Default)
Width/height only apply to content
border-box (Recommended)
Width/height include padding and border
Box Sizing Comparison:
| Value | Description | Width Calculation | When to Use |
|---|---|---|---|
content-box |
Default behavior | width = content width only | Legacy layouts |
border-box |
Modern approach | width = content + padding + border | Modern web design |
/* Traditional box model (content-box) */
.traditional-element {
box-sizing: content-box; /* Default */
width: 200px;
padding: 20px;
border: 5px solid black;
/* Total width: 200 + 20 + 20 + 5 + 5 = 250px */
}
/* Modern box model (border-box) */
.modern-element {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
/* Total width: 200px (includes padding and border) */
}
/* Global border-box reset */
* {
box-sizing: border-box;
}
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
Box Sizing Best Practice:
- Use
border-boxfor predictable layouts - Apply it globally with a CSS reset
- Makes percentage-based layouts easier to manage
- Reduces math calculations for element sizing
Display Property & Box Model
The display property affects how the box model behaves for different elements.
| Display Value | Box Model Behavior | Width/Height | Margin/Padding |
|---|---|---|---|
block |
Full width, stacks vertically | Respects width/height | All sides work normally |
inline |
Content width only, flows inline | Ignored | Only horizontal margins work |
inline-block |
Flows inline but respects box model | Respects width/height | All sides work normally |
flex |
Flexible box layout | Respects width/height | All sides work normally |
grid |
Grid layout | Respects width/height | All sides work normally |
Display Type Examples:
/* Block elements - respect full box model */
div, p, h1-h6 {
display: block;
width: 100%; /* Default for block elements */
margin: 10px 0;
padding: 15px;
}
/* Inline elements - limited box model */
span, a, strong {
display: inline;
/* width and height are ignored */
margin: 0 10px; /* Only horizontal margins work */
padding: 5px 10px; /* Padding works but may overlap */
}
/* Inline-block - best of both worlds */
.button {
display: inline-block;
width: 120px;
height: 40px;
margin: 10px;
padding: 10px 20px;
/* Behaves like inline but respects box model */
}
/* Modern layout methods */
.container {
display: flex; /* or grid */
/* Children respect full box model */
}
Practical Box Model Examples
Let's see how the box model works in real-world scenarios.
Card Component:
Card Title
This is a card component demonstrating box model properties.
.card {
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 20px;
margin: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.card h5 {
margin-top: 0;
margin-bottom: 15px;
}
.card p {
margin-bottom: 15px;
}
.card button {
background: #4e73df;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}
Navigation Menu:
/* Navigation styling with box model */
.nav {
background: #333;
padding: 15px;
border-radius: 4px;
}
.nav a {
display: inline-block;
color: white;
text-decoration: none;
padding: 10px 20px;
margin: 0 5px;
border-radius: 3px;
}
.nav a:hover {
background: #4e73df;
}
.nav a.active {
background: #4e73df;
}
Common Box Model Issues:
- Unexpected scrolling: Check for extra margin/padding
- Elements too wide: Remember border-box vs content-box
- Spacing inconsistencies: Watch for margin collapsing
- Alignment problems: Verify display property values
Interactive Box Model Demo
Experiment with different box model properties to see how they affect elements in real-time:
Box Properties:
Generated CSS:
width: 200px;
height: 150px;
padding: 20px;
margin: 10px;
border: 5px solid #4e73df;
box-sizing: border-box;