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
Setting Margins for Individual Sides using CSS
CSS allows us to control the space around individual sides of an element using specific margin properties. The CSS margin property is a shorthand for the following properties: margin-top, margin-right, margin-bottom and margin-left.
Syntax
selector {
margin-top: value;
margin-right: value;
margin-bottom: value;
margin-left: value;
}
Possible Values
| Value | Description |
|---|---|
| length | Set a margin in px, pt, em, rem, etc. The default is 0 |
| % | Set a margin as a percentage of the containing element's width |
| auto | The browser calculates the margin automatically |
Understanding Margin Order
When using the shorthand margin property with four values, they are applied in clockwise order starting from the top −
margin: 25px 30px 50px 60px;
This is equivalent to −
margin-top: 25px; margin-right: 30px; margin-bottom: 50px; margin-left: 60px;
Example: Setting Individual Margin Properties
The following example demonstrates how to set margins for individual sides of elements −
<!DOCTYPE html>
<html>
<head>
<style>
.container {
background-color: #f0f0f0;
padding: 20px;
width: 400px;
margin: 20px auto;
}
.box1 {
background-color: #ff6b6b;
padding: 10px;
margin-top: 20px;
margin-right: 30px;
margin-bottom: 15px;
margin-left: 40px;
}
.box2 {
background-color: #4ecdc4;
padding: 10px;
margin-top: 10px;
margin-right: 20px;
margin-bottom: 25px;
margin-left: 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="box1">Box 1 - Custom margins on all sides</div>
<div class="box2">Box 2 - Different margin values</div>
</div>
</body>
</html>
Two colored boxes appear within a gray container, each with different spacing around all sides based on their individual margin settings.
Example: Using Auto Margins for Centering
The margin: auto value is commonly used to center block elements horizontally −
<!DOCTYPE html>
<html>
<head>
<style>
.centered-box {
width: 200px;
height: 100px;
background-color: #45b7d1;
color: white;
text-align: center;
line-height: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
margin-bottom: 30px;
}
.left-aligned {
width: 200px;
height: 80px;
background-color: #96ceb4;
color: white;
text-align: center;
line-height: 80px;
margin-left: 50px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="centered-box">Centered Box</div>
<div class="left-aligned">Left Margin 50px</div>
</body>
</html>
A blue centered box and a green box with left margin appear, demonstrating different margin behaviors. The first box is horizontally centered, while the second has specific left spacing.
Conclusion
Individual margin properties provide precise control over spacing around elements. Use margin-top, margin-right, margin-bottom, and margin-left when you need different spacing on each side, or use auto values for automatic centering.
