totn CSS

CSS: margin-right property

This CSS tutorial explains how to use the CSS property called margin-right with syntax and examples.

Description

The CSS margin-right property defines the margin on the right side of an element.

Syntax

The syntax for the margin-right CSS property is:

margin-right: value;

Parameters or Arguments

value

The margin to apply to the right side of the element. It can be one of the following:

Value Description
fixed Fixed value expressed in px, em, ...
div { margin-right: 10px; }
div { margin-right: 4em; }
percentage Percentage value
div { margin-right: 6%; }
auto Used for centering blocks
div { margin-right: auto; }
inherit Element will inherit the margin-right from its parent element
div { margin-right: inherit; }

Note

  • The value in the CSS margin-right property can be expressed as either a fixed value or as a percentage.
  • Negative values are allowed in the CSS margin-right property.
  • When the value is provided as a percentage, it is relative to the width of the containing block.
  • See also margin, margin-top, margin-bottom, and margin-left.

Browser Compatibility

The CSS margin-right property has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • IE Phone
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the margin-right property below, exploring examples of how to use this property in CSS with a fixed value as well as a percentage value.

Using Fixed Value

Let's look at a CSS margin-right example where we have provided the value as a fixed value.

div { margin-right: 4px; }

In this CSS margin-right example, we have provided a value of 4px which would apply to the right side of the element.

Let's look at another CSS margin-right example with a fixed value.

div { margin-right: 5em; }

In this CSS margin-right example, we have provided a value of 5em which would apply to the right side of the element.

Using Percentage

Let's look at a CSS margin-right example where we have provided the value as a percentage.

div { margin-right: 3%; }

In this CSS margin-right example, we have provided a value of 3% which would apply to the right side of the element.

Using Auto

Let's look at an example where we have provided the value as auto.

div { margin-right: auto; }

In this example, we have set the right margin to auto for the <div> tag. The auto value is generally used to center blocks.