CSS margin-right Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The margin-right property sets the margin area on the right of an element. A positive value moves neighbors farther away on the right; a negative value pulls them closer.

It is part of the box model (outside the border), is not inherited, and always targets the physical right edge. Prefer margin-inline-end when spacing should follow text direction (LTR/RTL).

Baseline · Widely available.

Not deprecated. Core length, percentage, and auto values work across modern browsers.

CSS Syntax

syntax.css
margin-right: <length> | <percentage> | auto | inherit;

Example: margin-right: 1.5rem;. Values may be positive, zero, or negative.

Property Values

ValueDescriptionTry it
<length>Fixed size such as 16px or 1emTry it
<percentage>Relative to the containing block’s width (inline size)Try it
autoAbsorbs leftover horizontal space (often used to push or center)Try it
Negative lengthPulls the next content toward the elementTry it
0 / initialNo right margin (initial value is 0)Try it
inheritTakes the parent’s margin-rightTry it

How to Use margin-right

Use it to create space after a box on its physical right side.

  1. Select the element that needs space on the right.
  2. Set a length (1rem, 24px) or a percentage of the container width.
  3. Use auto when you want leftover space absorbed on the right (for example to keep a fixed-width block on the left).
  4. For RTL-friendly layouts, prefer margin-inline-end instead.
PieceRequired?Role
Any element (typical)YesApplies broadly; table internals use border-spacing instead
Length, %, or autoYesSets the right outer space
InheritedNoSet on each element that needs it
Defined width (for auto)OftenCentering / push-left with auto needs free horizontal space

Live Preview

Change margin-right on the yellow box and watch the gap before the next box.

Percentages are based on the container width. The right edge stays physical even in RTL.

Inline row
Target Next

margin-right: 24px

CSS Examples

Each snippet focuses on margin-right. Use Try It Yourself for a live editor.

Example 1 — Length Gap

Add a fixed gap on the right of a box.

CSS
<style>
  .card {
    margin-right: 24px;
  }
</style>

<span class="card">Card</span>
<span>Next</span>
Try It Yourself

How It Works

  1. Target the first box. The rule applies to .card.
  2. Set 24px. Outer space appears on the physical right.
  3. Next starts after that gap on the right.
  4. Not padding. The space is outside the border.

Example 2 — Percentage

Size the right margin from the container width.

CSS
<style>
  .box {
    margin-right: 10%;
  }
</style>

<span class="box">Box</span>
<span>Next</span>
Try It Yourself

How It Works

  1. Percent of width. The margin is a fraction of the containing block’s inline size.
  2. Wider parent → larger gap. The right margin scales with container width.
  3. Useful for responsive spacing without fixed pixels.
  4. Try resizing the editor pane to see the gap change.

Example 3 — Auto (Keep Left)

Let leftover space go to the right so the box stays on the left.

CSS
<style>
  .wrap {
    width: 220px;
  }
  .box {
    width: 80px;
    margin-right: auto;
  }
</style>

<div class="wrap">
  <div class="box">Left</div>
</div>
Try It Yourself

How It Works

  1. Give the box a width so free space exists in the parent.
  2. Set margin-right: auto. Unused space goes to the right.
  3. The box stays on the left inside the container.
  4. Both sides auto (margin-left and margin-right) centers the box instead.

Edge Cases & Pitfalls

If the right gap looks wrong or missing, check these first.

physical

Always the physical right

In RTL, margin-right still means right — not the end of the text. Use margin-inline-end for direction-aware spacing.

percent

% is based on width

Percentage margins resolve against the containing block’s inline size (width in horizontal writing modes).

vs

margin-right vs padding-right

Use padding-right for inner space (backgrounds extend into padding). Use margin for space between separate boxes.

collapse

Horizontal margins do not collapse

Unlike top/bottom, left and right margins between siblings do not merge into one gap.

auto

auto alone vs both sides

margin-right: auto with a fixed width keeps the box left-aligned. Pair with margin-left: auto to center.

Browser Support

margin-right is a browser CSS property for the box model. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

margin-right

Length, percentage, and auto values work in all modern browsers and older engines.

99% Browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Opera All modern versions
Full support
Samsung Internet Supported
Full support
Internet Explorer Supported
Full support
margin-right Widely supported

Bottom line: Safe for everyday right spacing. Remember RTL vs logical properties. It is not a runtime API.

Key Takeaways

  • Role: outer space on an element’s physical right edge.
  • Initial: 0. Not inherited.
  • Percentages use containing-block width.
  • auto can keep a box left or help center with margin-left: auto.

One line: margin-right adds outer space on the right — outside the border, before the next neighbor.

Frequently Asked Questions

It sets the outer space on the right of an element, outside its border. A positive value pushes neighbors farther away on the right; a negative value pulls them closer.
The initial value is 0. There is no right margin until you set one.
margin-right is outside the border, between elements. padding-right is inside the border, between the border and the content.
margin-right always targets the physical right edge. margin-inline-end follows writing mode and direction, so it maps to the left in RTL.
It absorbs leftover horizontal space on the right. Alone on a block it can push the box to the left; with margin-left: auto it centers a block that has a defined width.
It is Baseline Widely available. It is a browser CSS box-model property, not a Node.js runtime feature.

Next: margin-top

Set outer space above an element.

margin-top tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful