CSS Margins and Padding
CSS spacing comes in two types: padding and margin. They are key properties for controlling "inner whitespace" and "outer spacing" of elements.
Spacing Properties Quick Reference
| Property | Common Values | Purpose |
|---|---|---|
padding |
16px, 10px 20px, 10px 5px 15px 20px |
Padding (top, right, bottom, left) |
padding-top/right/bottom/left |
16px, 5% |
Single-side padding |
margin |
20px, 0 auto, 10px 0 20px 0 |
Margin (top, right, bottom, left) |
margin-top/right/bottom/left |
10px, auto |
Single-side margin |
auto |
margin: 0 auto |
Center block elements horizontally |
Padding
padding is the space between the content area and the border — the element's "inner whitespace."
Example
<style>
.box {
padding: 20px; /* 20px on all sides */
background: #fff3e0;
border: 1px solid #ffb74d;
}
</style>
<div class="box">20px padding creates space between content and border</div>
Setting Individual Sides
Example
<style>
.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
background: #fce4ec;
border: 1px solid #e91e63;
}
</style>
<div class="box">Different padding: top 10px, right 20px, bottom 30px, left 40px</div>
Padding Shorthand Rules
/* One value: all sides equal */
padding: 20px;
/* Two values: top+bottom, left+right */
padding: 10px 20px;
/* Three values: top, left+right, bottom */
padding: 10px 20px 30px;
/* Four values: top, right, bottom, left (clockwise) */
padding: 10px 20px 30px 40px;
Common Padding Use Cases
Example
<style>
/* Add padding to button for better clickability */
.button {
padding: 12px 24px;
background: #3498db;
color: white;
border: none;
border-radius: 4px;
}
/* Card content whitespace */
.card {
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background: #f9f9f9;
}
/* Navigation item tap area */
.nav-item {
padding: 10px 16px;
background: #e8eaf6;
display: inline-block;
}
</style>
<button class="button">Button</button>
<div class="card">Card content with comfortable spacing from border</div>
<nav class="nav-item">Nav Item</nav>
Padding Notes
<span>, <a> and other inline elements' padding only works horizontally. If you want perfect click areas on inline elements, set display: inline-block.
- Padding area shows the element's background color and images
- Padding makes elements "larger" (unless using
box-sizing: border-box) - Inline elements (like
<span>) only have effective padding horizontally
Margin
margin is the space between the element's border and other elements — the element's "outer spacing."
Example
<style>
.box {
margin: 20px; /* 20px on all sides */
background: #e8f5e9;
border: 1px solid #66bb6a;
padding: 10px;
}
</style>
<div class="box">20px margin maintains 20px spacing from other elements</div>
Margin Shorthand Rules
Same as padding:
margin: 20px; /* All sides equal */
margin: 10px 20px; /* Top+bottom, left+right */
margin: 10px 20px 30px; /* Top, left+right, bottom */
margin: 10px 20px 30px 40px; /* Top, right, bottom, left */
Common Margin Use Cases
Example
<style>
/* Center a container */
.container {
width: 1200px;
margin: 0 auto; /* Horizontal center */
background: #e3f2fd;
padding: 10px;
}
/* Spacing between elements */
.box {
margin-bottom: 20px;
}
/* Space below headings */
h1 {
margin-bottom: 16px;
}
</style>
<div class="container">Container element with child elements inside</div>
<div class="box">Box element demonstrating box model styling</div>
<h1>First Level Heading</h1>
Using margin: auto for Centering
margin: 0 auto can horizontally center a block element within its parent:
Example
<style>
.box {
width: 400px; /* Must specify width */
margin: 0 auto; /* Horizontal center */
}
</style>
<div class="box">Box element demonstrating box model styling</div>
Margin Collapse
overflow: hidden or padding: 1px to the parent element. Using a flex container also prevents this.
This is an important CSS concept — adjacent vertical margins collapse into one, taking the larger value.
Scenario 1: Adjacent Siblings
Example
<style>
.box1 {
margin-bottom: 30px;
}
.box2 {
margin-top: 20px;
}
/* Actual spacing is 30px (larger value), not 50px */
</style>
<div class="box1">Box1</div>
<div class="box2">Box2</div>
Scenario 2: Parent-Child Elements
When the parent has no padding, border, or content, the child's margin "penetrates" outside the parent:
Example
<style>
.parent {
background: #eee;
/* No padding or border */
}
.child {
margin-top: 20px; /* This margin makes parent "drop" too */
}
</style>
<div class="parent">Parent container</div>
<div class="child">Child content</div>
Avoidance methods:
- Add
padding: 1pxto parent (or any positive value) - Add
border: 1px solid transparentto parent - Add
overflow: hiddento parent - Use flex or grid layout
Scenario 3: Empty Elements
A block element with no content, padding, or border will also have its top and bottom margins collapse.
Cases Where Margins Don't Collapse
- Horizontal margins (left/right) don't collapse
- Flex layout children don't collapse
- Floated elements don't collapse
- Absolutely positioned elements don't collapse
Choosing Between Padding and Margin
| Scenario | Use Which |
|---|---|
| Keep button text from touching edges | padding |
| Keep two cards apart | margin |
| Keep content from border | padding |
| Center container on page | margin: auto |
| Control background area size | padding |
Simple decision method: Should the background cover this area? If yes, use padding; if no, use margin.
❓ FAQ
📖 Summary
- padding and margin are the most commonly used spacing properties in CSS layout
- padding controls inner whitespace, margin controls outer spacing
- Understanding their shorthand rules and the "margin collapse" trap is essential knowledge for reliable layouts
- Globally setting
box-sizing: border-boxat project start prevents padding from expanding elements and simplifies dimension calculations
📝 Exercises
- Create a "card group" page with 3 cards, using margin to control spacing between cards.
- Create a navigation bar using padding to control text-to-border distance in each nav item.
- Demonstrate "margin collapse" and solve it with at least two different methods.
- Use
margin: 0 autoto center an 800px-wide container horizontally on the page.



