Currently Empty: ₹0.00
CSS Margins
CSS CODES & EXAMPLES
CSS Margins
Learn how to control the space outside HTML elements using CSS margins. Understand margin properties, shorthand, auto margins, individual sides, negative margins and practical website layouts.
01
What are CSS Margins?
Control the space outside an element
CSS margin properties are used to create space around the outside of an HTML element. Margin separates one element from another.
Important Properties:margin
margin-top
margin-right
margin-bottom
margin-left
margin: auto
02
Basic Margin
Create space around an element
The margin property creates space outside an element.
basic-margin.css
.box {
margin: 30px;
}
margin: 30px
03
Margin Top
Add space above an element
margin-top.css
.box {
margin-top: 40px;
}
Element
04
Individual Margin Sides
Control each side separately
You can set margin independently for the top, right, bottom and left sides of an element.
individual-margin.css
.box {
margin-top: 20px;
margin-right: 30px;
margin-bottom: 40px;
margin-left: 50px;
}
TOP
20px
20px
RIGHT
30px
30px
BOTTOM
40px
40px
LEFT
50px
50px
ELEMENT
05
Margin Shorthand — Two Values
Set vertical and horizontal margins together
margin-two-values.css
.box {
margin: 20px 40px;
}
20px
Top & Bottom
40px
Left & Right
When two values are used, the first value applies to top and bottom, while the second applies to left and right.
06
margin-three-values.css
.box {
margin: 10px 30px 50px;
}
10px
Top
30px
Left & Right
50px
Bottom
07
Margin Shorthand — Four Values
Set all four sides in one declaration
margin-four-values.css
.box {
margin: 10px 20px 30px 40px;
}
10px
TOP
20px
RIGHT
30px
BOTTOM
40px
LEFT
Four values follow the clockwise order: Top → Right → Bottom → Left.
08
Margin Auto
Center a block element horizontally
margin-auto.css
.box {
width: 300px;
margin: 0 auto;
}
margin: 0 auto
margin: 0 auto; is commonly used to center a
fixed-width block element horizontally.
09
Centered Website Container
A common real-world layout pattern
container.css
.container {
max-width: 1100px;
margin: 0 auto;
padding: 20px;
}CONTAINER
Professional Website Content
The container is centered automatically using
margin: 0 auto;.
10
Negative Margin
Move an element closer or overlap another element
negative-margin.css
.box {
margin-top: -20px;
}
Background Section
Negative Margin Element
Negative margins can pull an element toward another element. Use them carefully because excessive negative spacing can make layouts difficult to maintain.
11
Margin Collapse
Understand vertical margins between block elements
In normal block formatting, vertical margins of adjacent block elements can collapse. The larger margin generally determines the resulting space instead of simply adding both margins.
margin-collapse.css
.first {
margin-bottom: 30px;
}
.second {
margin-top: 20px;
}
First Element
Second Element
Vertical margins can collapse
12
card-spacing.css
.card {
margin-bottom: 20px;
}
.card:last-child {
margin-bottom: 0;
}
COURSE
Computer Fundamentals
Learn essential computer concepts and practical skills.
COURSE
Microsoft Word
Learn document creation and professional formatting.
COURSE
Microsoft Excel
Learn spreadsheets, formulas and data management.
13
Section Spacing
Create clean website sections
section-spacing.css
.section {
margin-bottom: 50px;
}Content of the first section.
Content of the second section.
14
Margin Auto with Flexbox
Push an element to the opposite side
flex-margin-auto.css
.navbar {
display: flex;
}
.right {
margin-left: auto;
}SNCEC
Home
Courses
In a flex container, margin-left: auto; can push
an item toward the right side.
15
Responsive Margins
Change spacing for smaller screens
responsive-margin.css
.section {
margin: 60px 30px;
}
@media (max-width: 600px) {
.section {
margin: 30px 15px;
}
}
Desktop Spacing
Mobile Spacing
16
CSS Margins Live Preview
Run a complete margin example
LIVE PREVIEW
17
CSS Margins Quick Reference
Important margin properties at a glance
| Property | Example | Purpose |
|---|---|---|
| margin | margin: 20px; | Set margin on all sides |
| margin-top | margin-top: 20px; | Space above |
| margin-right | margin-right: 20px; | Space on the right |
| margin-bottom | margin-bottom: 20px; | Space below |
| margin-left | margin-left: 20px; | Space on the left |
| margin: 0 auto | margin: 0 auto; | Center a block |
| Negative Margin | margin-top: -20px; | Pull an element closer |
18
Practice Task
Build a professional course section
🎯 Practice: Create a Course Layout
Create a responsive course layout using CSS margins.
-
Create a centered container using
margin: 0 auto;. - Add spacing between course cards.
- Use individual margin properties where required.
- Create different spacing for mobile devices.
-
Use
margin-left: auto;with Flexbox.



