Code has been added to clipboard!

The CSS Margin Property

Reading time 3 min
Published Aug 8, 2017
Updated Oct 15, 2019

Adding CSS margins

The CSS margin property allows you to add extra space around the chosen element:

Example
p {
   margin: 10px 15px 10px 15px;
}

Providing sufficient spacing around menu items, scrollbars, and other HTML elements clarifies and improves the overall structure of the page. Thus, CSS margins make it easier for the user to navigate and interact.

Note: don't get confused with CSS margin vs padding: margin sets the spacing outside the borders, and padding does so within the element.

Setting CSS margin values

When using the CSS margin property, you can define up to four values:

margin: value1 value2 value3 value4;

Based on how many values you specify, the property assumes the remaining values using different logic. Understand how to define the CSS margin order by reviewing the table below.

Syntax Example Explanation
One value margin: 5px; All four margins are the same.
Two values margin: 5px 10px; The first value defines the top and bottom margins.
The second value defines the left and right margins.
Three values margin: 5px 10px 15px; The first value defines the top margin.
The second value defines the left and right margins.
The third value defines the bottom margin.
Four values margin: 5px 10px 15px 20px; The first value defines the top margin.
The second value defines the right margin.
The third value defines the bottom margin.
The fourth value defines the left margin.

You can define the width for CSS margin in either length units (e.g., pixels) or percentages (in relation with the container block). The default value is 0 0 0 0, which represents no CSS margin.

Tip: you can also use the keyword auto, in which case the browser will select the margin for the HTML element.

Manipulating each CSS margin individually

CSS margin is actually a shorthand for four subproperties:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left

If you don't want to use the shorthand or just want to be extra sure about the CSS margin order, you can also use the subproperties on their own.

To apply margin to the top of an element, use margin-top:

Example
p {
   margin-top: 10px;
}

To apply margin to the right of an element, use margin-right:

Example
p {
   margin-right: 5px;
}

To apply margin to the bottom of an element, use margin-bottom:

Example
p {
   margin-bottom: 10px;
}

To apply margin to the left of an element, use margin-left:

Example
p {
   display: block;
   margin-left: 5px;
}

Browser support

Chrome
1+
Edge
12+
Firefox
1+
IE
3+
Opera
3.5+
Safari
1+

Mobile browser support

Chrome
18+
Firefox
4+
Opera
10.1+
Safari
1+
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index