close
CSS Portal

CSS margin-right Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!
Image

Description

The margin-right property controls the space outside an element’s right edge in the CSS box model, pushing neighboring content away horizontally and affecting how much empty area appears between that element and whatever follows it on the right. It is one side of the element’s outer spacing and works together with the other sides when you think about overall spacing — for example, the shorthand margin can set all sides at once, while the opposing side can be adjusted with margin-left. Because it sits outside the element’s border and padding, changing it changes the apparent separation between boxes without altering the element’s internal layout.

How margin-right affects layout depends on the element’s display and formatting context. On block-level boxes its right margin contributes to the total horizontal space the element consumes alongside its content width and any borders or padding, so it interacts with properties such as display and width when determining whether elements fit on the same line or wrap. In contexts with floated or positioned elements, the right margin can influence how nearby content flows or is offset; it therefore relates to how float and position change the normal document flow.

There are a few important behavioral details to keep in mind when using margin-right. Horizontal margins do not participate in the vertical margin collapsing rules, so right-side spacing behaves independently of the top/bottom collapse phenomenon you might see with other margins; for the vertical counterparts see properties such as margin-top. For internationalization and logical layout flows, the modern logical counterpart to this physical side property is margin-inline-end, which maps to the appropriate side in left-to-right or right-to-left contexts. Finally, because margins affect the distance between elements rather than their inner sizing, they are commonly used to create external spacing and alignment without altering an element’s internal box or its intrinsic dimensions.

Definition

Initial value
0
Applies to
All elements
Inherited
No
Computed value
As specified, but with relative lengths converted into absolute pixel values.
Animatable
Yes
JavaScript syntax
object.style.marginRight

Interactive Demo

Margin Right Example

Syntax

margin-right: <length> | <percentage> | auto

Values

  • <length>Specifies a fixed length, using any standard CSS length units. Negative Values are allowed.
  • <percentage>A percentage always relative to the width of the containing block.
  • inherit

Example

<div class="example">
<div class="box primary">Box with margin-right</div>
<div class="box">Adjacent box</div>
</div>
.example {
    display: flex;
    align-items: center;
    padding: 20px;
    background: #f7f7f7;
}

.box {
    padding: 16px 24px;
    background: #4a90e2;
    color: #fff;
    border-radius: 6px;
}

.box.primary {
    margin-right: 24px; /* space between this box and the next */
}

.box + .box {
    background: #50e3c2;
    color: #03312e;
}

Browser Support

The following information will show you the current browser support for the CSS margin-right property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!
Image