Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Usage of margin-bottom property with CSS
The margin-bottom CSS property specifies the bottom margin of an element, creating space below it. It accepts values in pixels, percentages, ems, or the keyword auto.
Syntax
margin-bottom: length | percentage | auto | initial | inherit;
Parameters
- length - Fixed value in px, em, rem, etc.
- percentage - Relative to parent element's width
- auto - Browser calculates the margin automatically
- initial - Sets to default value (0)
- inherit - Inherits from parent element
Example: Basic margin-bottom Usage
<!DOCTYPE html>
<html>
<head>
<style>
.margin-demo {
border: 2px solid red;
margin-bottom: 20px;
padding: 10px;
}
.margin-percent {
border: 2px solid green;
margin-bottom: 5%;
padding: 10px;
}
</style>
</head>
<body>
<div class="margin-demo">
This paragraph has a 20px bottom margin.
</div>
<div class="margin-percent">
This paragraph has a 5% bottom margin.
</div>
<div style="border: 2px solid blue; padding: 10px;">
This paragraph shows the spacing effect.
</div>
</body>
</html>
Different Values Example
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
padding: 15px;
border: 1px solid #333;
background-color: #f0f0f0;
}
.margin-px { margin-bottom: 30px; }
.margin-em { margin-bottom: 2em; }
.margin-auto { margin-bottom: auto; }
</style>
</head>
<body>
<div class="box margin-px">margin-bottom: 30px</div>
<div class="box margin-em">margin-bottom: 2em</div>
<div class="box margin-auto">margin-bottom: auto</div>
<div class="box">No bottom margin</div>
</body>
</html>
Key Points
- Negative values are allowed and pull elements upward
- Percentage values are relative to the parent's width, not height
- Adjacent margins collapse - the larger margin wins
- Use
margin-bottomto control vertical spacing between elements
Conclusion
The margin-bottom property is essential for controlling vertical spacing in layouts. Use pixel values for precise control or percentages for responsive designs.
Advertisements
