Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The margin property

Reference for the CSS margin shorthand: one to four values, auto centering, negative margins, margin collapsing, syntax, examples and browser support.

The margin property sets the space outside an element's border on all four sides. Margins are always transparent, they push neighboring boxes away, and they are the usual tool for spacing blocks apart. It is a shorthand for margin-top, margin-right, margin-bottom and margin-left.

The number of values decides which sides they apply to, always clockwise from the top.

CSS
margin: 1rem;              /* all four sides */
margin: 1rem 2rem;         /* top and bottom | left and right */
margin: 1rem 2rem 3rem;    /* top | left and right | bottom */
margin: 1rem 2rem 3rem 4rem; /* top | right | bottom | left */

Try it

One to four values

The gray strips are the parents. The 1px padding on each one stops the child's margin from collapsing through the parent, so you can see the margin as a gap inside it.

Margin collapsing

Syntax

CSS
margin: <margin-top>{1,4}

/* each value: */
<length> | <percentage> | auto
  • Initial value: 0 for every side
  • Applies to: all elements except those with an internal table display type (other than table-caption, table and inline-table); it has no effect on the vertical margins of non-replaced inline elements
  • Inherited: no
  • Percentages: relative to the width of the containing block, for all four sides
  • Animatable: yes, as a length or percentage
  • Computed value: each side as specified, with lengths made absolute; auto stays auto

Values

Value Description
<length> A fixed size such as 16px, 1rem or 2em. Negative values are allowed and pull the box toward or over its neighbors.
<percentage> A share of the containing block's width, even for top and bottom.
auto Lets the browser compute the margin. On a block with a set width, margin-inline: auto splits the free space and centers the box. In flex and grid layouts, auto margins absorb free space on that axis.

Usage notes

  • Margin collapsing. Adjacent vertical margins of block boxes in normal flow merge into one, and the result is the larger of the two (or the sum of a positive and a negative one). A parent with no padding, border or clearance can also collapse with its first or last child. Flex and grid items, floats, absolutely positioned boxes and elements that create a block formatting context (for example with overflow: hidden or display: flow-root) do not collapse with their neighbors.
  • Centering. margin-inline: auto (or margin: 0 auto) centers a block that has a width smaller than its container. It does nothing on an auto-width block, which already fills the line.
  • Percentages refer to width. margin-top: 10% is 10% of the parent's width, not its height.
  • Inline elements. Horizontal margins work, vertical margins do not affect the line box.
  • Spacing with gap. In flex and grid containers, prefer gap for spacing between items; it avoids the "extra margin on the last item" problem.
  • Negative margins can overlap content. Use them deliberately and check that text stays readable.
  • Logical alternatives. margin-inline and margin-block follow the writing mode, which helps with right-to-left languages.

Browser support

Supported in every browser; margin has been part of CSS since level 1.