CSS margin-right Property

The CSS margin-right property sets the outer margin on the right side of an element, controlling the space between its right border and surrounding content.

It accepts lengths, percentages, negative values, and the auto keyword. Unlike logical margin properties, margin-right always targets the physical right side, regardless of writing mode or text direction.

margin-right Syntax

The margin-right property accepts a CSS length, percentage, or auto. CSS-wide keywords are also supported.

Basic Syntax

selector {
  margin-right: 20px;
}

Length Value

div {
  margin-right: 2rem;
}

Percentage Value

div {
  margin-right: 10%;
}

Auto Value

div {
  margin-right: auto;
}

Negative Value

div {
  margin-right: -15px;
}

CSS-Wide Keywords

div {
  margin-right: initial;
}
div {
  margin-right: inherit;
}
div {
  margin-right: unset;
}
div {
  margin-right: revert;
}
div {
  margin-right: revert-layer;
}

The initial value of margin-right is 0.

Property Values

Value Description
<length> Sets the right margin using a length such as px, em, rem, or vw.
<percentage> Sets the right margin as a percentage of the containing block's width.
auto Allows the browser to calculate the right margin according to the formatting context.
initial Resets the property to its initial value of 0.
inherit Inherits the computed right margin from the parent element.
unset Resets the property to 0 because margins are not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 0
Inherited No
Applies to All elements except elements with table display types other than table-caption, table, and inline-table
Computed value Absolute length, percentage, or auto
Animation type Length, percentage, or calculated combination
Negative values Allowed for lengths and percentages
Physical side Right
Shorthand property margin

How It Works

The margin-right property creates space outside an element's right border. This space affects the element's layout but does not extend its background or padding.

HTML Example

<div class="content-row">
  <span class="content-box">First Item</span>
  <span class="content-box">Second Item</span>
</div>

CSS Example

.content-row {
  display: flex;
}
.content-box {
  margin-right: 20px;
  padding: 12px;
  border: 2px solid #2563eb;
  background-color: #dbeafe;
}

Each item receives a 20-pixel right margin. In this horizontal flex layout, the margin creates space after each item.

Remove the Last Item's Margin

.content-box:last-child {
  margin-right: 0;
}

This removes unnecessary spacing after the final item.

Margin vs. Padding

.content-box {
  margin-right: 20px;
  padding-right: 15px;
}

The margin adds space outside the right border, while the padding adds space between the content and right border.

Length Values

The margin-right property supports absolute and relative CSS length units.

Pixels

.box {
  margin-right: 24px;
}

Root Font Size

.box {
  margin-right: 2rem;
}

Element Font Size

.box {
  margin-right: 1.5em;
}

Viewport Units

.box {
  margin-right: 3vw;
}

Calculated Value

.box {
  margin-right: calc(1rem + 10px);
}

Relative units can help keep spacing proportional as font sizes or viewport dimensions change.

Percentage Values

Percentage right margins are calculated relative to the width of the containing block, not the element's own width.

Percentage Example

.box {
  margin-right: 10%;
}

Fixed Container Width

.container {
  width: 600px;
}
.container .box {
  margin-right: 10%;
}

The right margin equals 60 pixels when the containing block's width is 600 pixels.

Important Consideration

Percentage margins change as the containing block resizes. Combining large percentage margins with fixed element widths can cause horizontal overflow.

Auto Margins

The auto keyword allows the browser to calculate the right margin based on available space and the formatting context.

Basic Example

.box {
  margin-right: auto;
}

Align a Block to the Left

.box {
  width: 300px;
  max-width: 100%;
  margin-left: 0;
  margin-right: auto;
}

In a normal left-to-right block layout, the automatic right margin absorbs available horizontal space, leaving the box aligned to the left side.

Center a Block

.centered-box {
  width: 300px;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
}

When sufficient horizontal space exists, equal automatic left and right margins center the block.

Flexbox Auto Margin

.flex-container {
  display: flex;
}
.flex-item:first-child {
  margin-right: auto;
}

In a left-to-right flex row with available free space, the first item's automatic right margin pushes the following items toward the right side.

Important Consideration

Automatic margins do not always create visible spacing. Their behavior depends on the formatting context, available space, and element dimensions.

Negative Margins

Negative right margins reduce the space contributed on an element's right side and can cause adjacent elements to move closer together or overlap.

Negative Length

.box {
  margin-right: -20px;
}

Negative Percentage

.box {
  margin-right: -5%;
}

Overlapping Items

.item {
  display: inline-block;
  margin-right: -10px;
  padding: 12px;
  border: 1px solid #2563eb;
}

The negative right margin reduces the space after each item and may cause the following item to overlap it.

Important Consideration

Negative margins are valid CSS but can create unintended overlap or overflow. Test these layouts at different viewport sizes.

margin Shorthand

The margin shorthand sets all four physical margins, including margin-right.

Individual Properties

.box {
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 30px;
  margin-left: 40px;
}

Equivalent Shorthand

.box {
  margin: 10px 20px 30px 40px;
}

With four values, the order is top, right, bottom, and left.

One Value

.box {
  margin: 20px;
}

All four margins are 20 pixels.

Two Values

.box {
  margin: 10px 20px;
}

The top and bottom margins are 10 pixels, while the right and left margins are 20 pixels.

Three Values

.box {
  margin: 10px 20px 30px;
}

The right and left margins are both 20 pixels.

Shorthand Override

.box {
  margin-right: 40px;
  margin: 10px;
}

The final right margin is 10 pixels because the later shorthand resets all four margins.

Override Only the Right Margin

.box {
  margin: 20px;
  margin-right: 40px;
}

The right margin becomes 40 pixels, while the other three margins remain 20 pixels.

Physical vs. Logical Margins

The margin-right property always targets the physical right side. Logical properties such as margin-inline-start and margin-inline-end adapt to writing modes and text direction.

Physical Right Margin

.box {
  margin-right: 20px;
}

Logical Inline-End Margin

.box {
  margin-inline-end: 20px;
}

These declarations are equivalent in a left-to-right horizontal writing mode.

Comparison

Feature margin-right margin-inline-end
Physical side Always right Depends on writing mode and direction
Horizontal LTR Right Right
Horizontal RTL Right Left
Vertical writing modes Right Top or bottom
Direction-aware No Yes

Conflicting Declarations

.box {
  margin-right: 10px;
  margin-inline-end: 20px;
}

In a left-to-right horizontal writing mode, both declarations target the right margin. With equal specificity and importance, the later declaration wins.

Important Consideration

Use physical margins when spacing must remain on a fixed side. Use logical margins when components must adapt to different writing directions.

Flexbox and Grid

The margin-right property works with Flexbox and CSS Grid, where it can create spacing between items or control alignment.

Flexbox Example

.flex-container {
  display: flex;
}
.flex-item:first-child {
  margin-right: auto;
}

In a left-to-right flex row, the first item's automatic right margin can absorb free space and push following items toward the right.

Grid Example

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}
.grid-item {
  margin-right: 20px;
}

Each grid item receives a 20-pixel right margin within its grid area.

Using gap Instead

.grid-container {
  display: grid;
  gap: 20px;
}

The gap property is usually preferable for consistent spacing between flex or grid items because it does not add margins around the outside edges.

Important Consideration

Use margin-right when an individual item requires additional spacing on its right side. Use gap for consistent spacing between items.

Responsive Spacing

Right margins can be adjusted for different viewport sizes using relative units, media queries, and CSS functions.

Media Query Example

.content-box {
  margin-right: 40px;
}
@media (max-width: 768px) {
  .content-box {
    margin-right: 15px;
  }
}

Relative Spacing

.content-box {
  margin-right: 1.5rem;
}

Fluid Spacing

.content-box {
  margin-right: clamp(1rem, 4vw, 3rem);
}

Important Consideration

Large fixed margins combined with fixed widths can cause horizontal overflow on narrow screens. Use responsive values when appropriate.

margin-right Example

This example demonstrates how different right margin values change the spacing after an element in a horizontal layout.

HTML Example

<div class="margin-right-demo">
  <div class="demo-section">
    <h3>No Right Margin</h3>
    <div class="margin-area">
      <div class="demo-box no-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>20px Right Margin</h3>
    <div class="margin-area">
      <div class="demo-box medium-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>40px Right Margin</h3>
    <div class="margin-area">
      <div class="demo-box large-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
</div>

CSS Example

.margin-right-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
}
.margin-right-demo .demo-section {
  padding: 15px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.margin-right-demo h3 {
  margin: 0 0 12px;
  font-size: 1rem;
}
.margin-right-demo .margin-area {
  display: flex;
  direction: ltr;
  padding: 12px;
  background-color: #e2e8f0;
}
.margin-right-demo .demo-box {
  flex: 0 0 auto;
  padding: 12px;
  border: 2px solid #2563eb;
  background-color: #bfdbfe;
  color: #1e3a8a;
  text-align: center;
  font-size: 0.875rem;
}
.margin-right-demo .no-margin {
  margin-right: 0;
}
.margin-right-demo .medium-margin {
  margin-right: 20px;
}
.margin-right-demo .large-margin {
  margin-right: 40px;
}
.margin-right-demo .following-box {
  margin: 0;
  border-color: #64748b;
  background-color: #f1f5f9;
  color: #334155;
}
Browser Result

No Right Margin

First
Second

20px Right Margin

First
Second

40px Right Margin

First
Second

As the right margin increases, the space after the first item increases. The margin remains on the physical right side regardless of text direction.

Change the Margin to 30px

.margin-right-demo .medium-margin {
  margin-right: 30px;
}

Use a Relative Unit

.margin-right-demo .large-margin {
  margin-right: 2rem;
}

Remove the Margin

.margin-right-demo .large-margin {
  margin-right: 0;
}

Use a Negative Margin

.margin-right-demo .large-margin {
  margin-right: -10px;
}

The negative margin reduces the space after the first item and can cause the second item to overlap it.

Common Mistakes

Confusing Margin with Padding

margin-right creates space outside the border, while padding-right creates space inside it.

Expecting margin-right to Follow Text Direction

The property always targets the physical right side. Use logical margins for direction-aware spacing.

Expecting Percentage Values to Use the Element's Width

Percentage right margins are based on the containing block's width.

Expecting Auto to Always Create Space

Automatic margins depend on the formatting context and available space.

Using Negative Margins Without Testing

Negative margins can cause unintended overlap or overflow.

Overwriting the Value with a Shorthand

.box {
  margin-right: 40px;
  margin: 10px;
}

The final right margin is 10 pixels.

Mixing Physical and Logical Margins

In a left-to-right horizontal layout, margin-right and margin-inline-end target the same side. The cascade determines which declaration wins.

Using Margins Instead of gap for Every Layout

Use gap when consistent spacing between flex or grid items is needed.

Important Considerations

Keep the following points in mind when using margin-right:

  • The property controls the outer margin on the physical right side.
  • The initial value is 0.
  • The property is not inherited.
  • It does not change sides when text direction changes.
  • It does not change sides when the writing mode changes.
  • CSS length values are supported.
  • Percentage values are based on the containing block's width.
  • The auto keyword is supported.
  • Negative lengths and percentages are allowed.
  • Negative margins can cause overlap or overflow.
  • Margins are outside the element's border.
  • Margins do not extend the element's background.
  • Margins do not directly increase the element's declared width.
  • The property can be set using the margin shorthand.
  • Later shorthand declarations can override earlier right margins.
  • Physical and logical margin declarations can affect the same side.
  • The CSS cascade determines which conflicting declaration wins.
  • Automatic right margins can absorb available space in certain layouts.
  • Automatic left and right margins can center constrained blocks.
  • The property works with Flexbox and CSS Grid.
  • Relative units can provide scalable spacing.
  • Media queries can adjust margins for smaller screens.
  • Large margins combined with fixed widths can cause overflow.
  • Use logical margins when spacing should follow writing direction.

Browser Support

The CSS margin-right property is widely supported across modern and older browsers, including Chrome, Edge, Firefox, Safari, and Opera.

It is a long-established CSS property and does not require vendor prefixes.

For current compatibility information, visit Can I Use? and search for margin-right.

Specifications

The margin-right property is defined in the CSS Box Model Module Level 3 specification.

CSS Box Model Module Level 3: Physical Margin Properties

The specification defines physical margins, their accepted values, and how margins affect the CSS box model.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: margin-right