right

Moves a positioned element away from the right edge of its reference point.

Initial value
auto
Inherited
No
Animatable
yes, as a length or percentage
Applies to
positioned elements (position other than static)
JavaScript
element.style.right = "24px"
On this page

Example

#
CSS
.badge {
  position: absolute;
  right: 8px;
}
Preview

Definition and usage

#

The right property only works on elements whose position is something other than static. It says how far from the right edge of the reference point the element should sit.

The reference point depends on the kind of positioning. With relative, the element moves relative to the spot it would normally occupy. With absolute, it’s the nearest positioned ancestor that counts; with fixed, the browser window; and with sticky, the edge of the scrolling container.

Percentage values are calculated from the size of the reference point. If you set both right and left on an element with no fixed size, it stretches between both edges.

Syntax

#
Syntax
right: auto | <length> | <percentage>;

Values

#
  • auto

    No offset from this side. The element stays wherever other properties place it.
  • 24px

    <length>

    A fixed distance from the right edge. Every length unit works, including negative values.
  • 50%

    <percentage>

    The distance, calculated as a percentage of the reference point’s size.

Values in practice

#

auto

No offset from this side. The element stays wherever other properties place it.
.demo { right: auto; }

24px

A fixed distance from the right edge. Every length unit works, including negative values.
.demo { right: 24px; }

50%

The distance, calculated as a percentage of the reference point’s size.
.demo { right: 50%; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • If the offset doesn’t seem to work, check whether the element has position set.
  • To set all four sides at once, use the inset shorthand.

Related entries

#

Spotted a mistake or missing an example? Write to us.