Skip to content

CSS margin-right Property

The margin-right property is used to define how much the right margin of the element will be set.

INFO

Negative values are allowed.

Initial Value0
Applies toAll elements. It also applies to ::first-letter.
InheritedNo.
AnimatableYes. Right margin of the element is animatable.
VersionCSS2
DOM Syntaxobject.style.marginRight = "50px";

Syntax

Syntax of CSS margin-right Property

css
margin-right: length | auto | initial | inherit;

Example of the margin-right property:

Example of CSS margin-right Property with px value

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .right {
        margin-right: 400px;
      }
    </style>
  </head>
  <body>
    <h2>Margin-right property example</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <p class="right">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </body>
</html>

Result

CSS margin-right Property

Example of the margin-right property specified in "%":

Example of CSS margin-right Property with % valuel

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .right {
        margin-right: 50%;
      }
    </style>
  </head>
  <body>
    <h2>Margin-right property example</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <p class="right">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </body>
</html>

Example of the margin-right property with the "auto" value:

Example of the margin-right property with the "auto" value:

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .right {
        margin-right: auto;
      }
    </style>
  </head>
  <body>
    <h2>Margin-right property example</h2>
    <p>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    </p>
    <p class="right">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    </p>
  </body>
</html>

Values

ValueDescriptionPlay it
autoSets the right margin. It is the default value of this property.Play it »
lengthDefines a right margin in px, pt, cm, etc. Default value is 0.Play it »
%Sets the right margin in % of containing element.Play it »
initialMakes the property use its default value.Play it »
inheritInherits the property from its parents element.

Practice

What does the 'margin-right' CSS property do?

Dual-run preview — compare with live Symfony routes.