⬇️ CSS margin-bottom
: Adding Space Below Elements
Vertical rhythm and spacing are essential for good web design. Whether you’re separating paragraphs, buttons, sections, or cards — the margin-bottom
property helps you control the space below an element precisely.
It’s one of the four directional margin properties (margin-top
, margin-right
, margin-bottom
, margin-left
) used to push elements away from each other vertically or horizontally.
🧾 What is margin-bottom
?
The CSS margin-bottom
property defines the amount of space below an element’s border, separating it from the content that follows.
It creates transparent space and does not affect the content within the element itself.
🧬 Syntax
selector {
margin-bottom: <length> | <percentage> | auto | inherit;
}
Value Types:
Value | Description |
---|---|
<length> | Fixed spacing like px , em , rem , etc. |
<percentage> | Percentage of the containing element’s width (not height!) |
auto | Browser-calculated spacing (rarely used for margin-bottom) |
inherit | Inherits value from the parent element |
🎯 Example Usage
p {
margin-bottom: 20px;
}
This gives each paragraph 20px
of space below it, which helps with readability and visual structure.
🧠 How It Works
margin-bottom
only affects the external spacing below an element.- It does not collapse horizontally (horizontal margins never collapse).
- Vertical margins (top & bottom) may collapse with adjacent elements — a concept known as margin collapsing.
Example of Collapsing Margins:
If two block elements are stacked and both have bottom/top margins, only the larger of the two margins will be used.
📐 Practical Example
<div class="card">
<h2>Title</h2>
<p>Description text here.</p>
<button>Read more</button>
</div>
<style>
.card p {
margin-bottom: 16px;
}
</style>
This adds spacing between the paragraph and the button, making the layout cleaner and more user-friendly.
🛠️ Best Practices
- Use consistent
margin-bottom
values across your project to create a visual rhythm. - Avoid using
margin-bottom
for fine-tuning positioning inside components — usepadding
instead for internal spacing. - When using percentages, remember they’re based on the width of the parent element.
- Use negative values sparingly — they can cause overlapping content and layout issues.
✅ Browser Support
CSS margin-bottom
is fully supported across all modern and legacy browsers:
- ✅ Chrome
- ✅ Firefox
- ✅ Safari
- ✅ Edge
- ✅ Internet Explorer 6+
🔚 Conclusion
The margin-bottom
property is simple but incredibly effective. It gives you precise control over how much vertical space appears beneath an element, ensuring your content is visually balanced and easy to read.