CSS margin-right Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Box Model

What You’ll Learn

The margin-right property adds outer space on the right side of an element. It is one of the most common tools for horizontal spacing between row items in CSS.

01

Right Space

Outside border.

02

Box Model

Outer spacing.

03

Row Gap

Separate items.

04

Units

px, rem, %.

05

Physical

Always right.

06

Default 0

No spacing.

Introduction

The margin-right property in CSS is used to set the right margin of an element. This property allows you to create space to the right of an element, separating it from content on its right.

Margins are essential for controlling the layout and spacing of elements on a web page, providing a clean and organized appearance.

Definition and Usage

Apply margin-right when you need spacing on only the right side without changing top, left, or bottom margins. It is ideal for gaps between horizontal items, spacing around floated content, and creating separation in toolbars and tag lists.

💡
Beginner Tip

margin-right creates space outside the border on the right. Use padding-right when you need space inside the border.

📝 Syntax

The syntax for the margin-right property is straightforward. You can specify the value using different units, such as pixels (px), percentages (%), ems (em), and more.

syntax.css
element {
  margin-right: value;
}

Here, value can be a length, percentage, or one of the global values (inherit, initial, unset).

Basic Example

margin-right.css
p {
  margin-right: 20px;
}

⚡ Quick Reference

QuestionAnswer
Initial value0
Applies toAll elements except table display types that use separate border model
InheritedNo
AnimatableYes, as a length
Logical equivalentmargin-inline-end (in horizontal LTR writing)

💎 Property Values

ValueExampleMeaning
Lengthmargin-right: 20px;Fixed margin in px, em, rem, etc.
Percentagemargin-right: 10%;Margin as a percentage of the containing element width
automargin-right: auto;Browser calculates the margin; useful in flex and grid layouts
inheritmargin-right: inherit;Inherits the margin-right value from the parent element
initialmargin-right: initial;Sets the property to its default value (0)
unsetmargin-right: unset;Resets to inherit or initial depending on context
margin-right: 1rem; margin-right: 20px; margin-right: 5%;

🎯 Default Value

The default value of the margin-right property is 0, meaning no extra space is added to the right of the element. Override it when you need spacing on the right outer edge.

👀 Live Preview

Boxes with right margin creating gaps in a horizontal row:

Box A
Box B
Box C

Examples Gallery

Add right margin to a paragraph, space horizontal row items, separate cards in a row, and compare with the logical equivalent.

🔢 Right Spacing

Start with the reference example — add a right margin to a paragraph element.

Example 1 — Paragraph Right Margin

Set a right margin of 20 pixels on a paragraph element.

margin-right.html
<style>
  .text {
    margin-right: 20px;
  }
</style>

<p class="text">This paragraph has a margin of 20px on the right.</p>
<p>This paragraph does not have a custom margin-right value.</p>
Try It Yourself

How It Works

Only the right outer edge gets spacing. Content to the right of the element is pushed away, creating a visible gap.

Example 2 — Horizontal Row Spacing

Use right margin on inline-block tags to create gaps between items in a row.

margin-right-tags.css
.tag {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  background: #dbeafe;
  margin-right: 0.5rem;
}
Try It Yourself

How It Works

Right margin adds space after each tag, separating items without affecting padding inside each badge.

📈 Layout & Logical Properties

Space cards in a horizontal row and understand the logical property equivalent.

Example 3 — Card Row Spacing

Add right margin between cards displayed in a row using rem units.

margin-right-card.css
.card {
  display: inline-block;
  width: 120px;
  padding: 1rem;
  background: #f1f5f9;
  margin-right: 1.5rem;
}
Try It Yourself

How It Works

Right margin creates horizontal separation between cards. Remove margin on the last item with :last-child to avoid trailing space.

Example 4 — Physical vs Logical

In horizontal LTR writing, margin-right matches margin-inline-end:

margin-right-logical.css
/* Physical */
.note {
  margin-right: 1.5rem;
}

/* Logical equivalent in horizontal LTR */
.note {
  margin-inline-end: 1.5rem;
}
Try It Yourself

How It Works

For simple English LTR pages, margin-right is familiar and widely used. Choose margin-inline-end when direction may change.

margin-right in the Box Model

Margin sits outside the border. Right margin specifically adds transparent space on the right side of the element:

  • Content — text or child elements inside the box.
  • Padding — inner space between content and border.
  • Border — the visible edge.
  • Margin-right — outer space on the right side of the border.

♿ Accessibility

  • Keep spacing meaningful — Right margin separates row items without breaking semantics.
  • Do not rely on empty elements for spacing — Use margin instead of blank divs.
  • Test zoomed layouts — Large right margins can push content off-screen on small viewports.
  • Consider RTL layouts — Use logical properties when direction may change.
  • Combine with semantic HTML — Margins adjust spacing; structure still comes from proper elements.

🧠 How margin-right Works

1

You set right spacing

Apply margin-right with px, rem, %, or a keyword.

CSS rule
2

Browser reserves space

Transparent margin area is added on the right side, outside the border.

Box model
3

Neighbors move away

Content on the right is pushed away, creating horizontal separation.

Layout
=

Aligned spacing

Row items are separated with clear right-side gaps.

🖥 Browser Compatibility

The margin-right property is widely supported across all major browsers, including Chrome, Firefox, Safari, Edge, and Opera. This means you can use this property confidently, knowing it will render consistently across different platforms and devices.

Universal · All browsers

Core layout spacing everywhere

margin-right is one of the most fundamental CSS properties. You can rely on it in any project.

99% Global browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Legacy & Chromium
Full support
Opera All versions · Modern & legacy
Full support
margin-right property 99% supported

Bottom line: Safe to use everywhere. For direction-aware spacing, see margin-inline-end.

🎉 Conclusion

The margin-right property is a versatile and essential tool in CSS for managing the layout and spacing of elements on your web pages.

Whether you’re looking to align content, create space between elements, or design a unique layout, understanding how to use margin-right effectively can help you achieve your design goals. Experiment with different values and units to see how they impact your design.

💡 Best Practices

✅ Do

  • Use margin-right for row gaps and horizontal separation in LTR layouts
  • Prefer rem for scalable right spacing
  • Combine with other margin longhands for precise control
  • Consider margin-inline-end for multilingual pages
  • Remove right margin on the last item in a row with :last-child

❌ Don’t

  • Confuse margin-right with padding-right
  • Use empty divs only to create spacing
  • Use huge fixed margins that break mobile layouts
  • Rely on physical right margin in RTL layouts without testing
  • Apply right margin when flex or grid gap would be cleaner

Key Takeaways

Knowledge Unlocked

Five things to remember about margin-right

Use these points when spacing elements on the right side.

5
Core concepts
0 02

Default 0

No spacing.

Default
rem 03

Scalable

Use rem.

Units
in 04

Row Gap

Separate items.

Use case
pad 05

Not padding

Inside vs out.

Box model

❓ Frequently Asked Questions

The margin-right property sets outer spacing on the right side of an element, outside of its border. It pushes content away from the element on its right.
The default value is 0, meaning no extra space is added to the right of the element unless you set one explicitly.
margin-right adds space outside the border between elements. padding-right adds space inside the border between the border and the content.
margin-right always targets the physical right edge. margin-inline-end follows text direction and adapts in RTL layouts.
Yes. margin-right: auto can absorb leftover horizontal space in flex and grid layouts, though margin: 0 auto is more common for centering block elements.

Practice in the Live Editor

Open the HTML editor, try different margin-right values, and see how right spacing changes your layout.

HTML Editor →

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.

5 people found this page helpful