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.
Fundamentals
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.
Foundation
📝 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;}
Cheat Sheet
⚡ Quick Reference
Question
Answer
Initial value
0
Applies to
All elements except table display types that use separate border model
Inherited
No
Animatable
Yes, as a length
Logical equivalent
margin-inline-end (in horizontal LTR writing)
Reference
💎 Property Values
Value
Example
Meaning
Length
margin-right: 20px;
Fixed margin in px, em, rem, etc.
Percentage
margin-right: 10%;
Margin as a percentage of the containing element width
auto
margin-right: auto;
Browser calculates the margin; useful in flex and grid layouts
inherit
margin-right: inherit;
Inherits the margin-right value from the parent element
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.
Preview
👀 Live Preview
Boxes with right margin creating gaps in a horizontal row:
Box A
Box B
Box C
Hands-On
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><pclass="text">This paragraph has a margin of 20px on the right.</p><p>This paragraph does not have a custom margin-right value.</p>
For simple English LTR pages, margin-right is familiar and widely used. Choose margin-inline-end when direction may change.
Context
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.
A11y
♿ 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.
Compatibility
🖥 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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Legacy & Chromium
Full support
OperaAll versions · Modern & legacy
Full support
margin-right property99% supported
Bottom line: Safe to use everywhere. For direction-aware spacing, see margin-inline-end.
Wrap Up
🎉 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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about margin-right
Use these points when spacing elements on the right side.
5
Core concepts
→01
Right Space
Outside border.
Purpose
002
Default 0
No spacing.
Default
rem03
Scalable
Use rem.
Units
in04
Row Gap
Separate items.
Use case
pad05
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.