Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The margin-right property

Reference for the CSS margin-right property: syntax, values, auto margins in block and flex layouts, negative values, examples and browser support.

The margin-right property sets the space to the right of an element's border edge. It separates the element from a following sibling on the same line or from the right edge of its container.

CSS
.icon {
  margin-right: 0.5rem;
}

Try it

Space on the right

Syntax

CSS
margin-right: <length> | <percentage> | auto
  • Initial value: 0
  • Applies to: all elements except internal table display types
  • Inherited: no
  • Percentages: relative to the width of the containing block
  • Animatable: yes
  • Computed value: the percentage as specified or the absolute length; auto stays auto

Values

Value Description
<length> A fixed size such as 1rem. Negative values pull following content closer or make it overlap.
<percentage> A share of the containing block's width.
auto Takes up free space on that side. With margin-left: auto as well, a block with a width is centered. On its own it pushes the block to the left. In a flex row, margin-right: auto pushes the following items to the far end.

Usage notes

  • Over-constrained blocks. If width, left margin, right margin and the containing block width do not add up, the browser ignores margin-right in left-to-right text. That is why margin-right: 20px on a full-width block seems to do nothing.
  • Whitespace between inline-blocks. The gap between inline-block elements comes from whitespace in the HTML, not from margins. A margin is added on top of it.
  • Spacing in lists of items. For rows of items, gap on a flex or grid container avoids a stray margin after the last item.
  • Right-to-left text. Use margin-inline-end so the spacing flips automatically.

Browser support

Supported in every browser.