CSS Reference ยท Positioning and Stacking
The right property
Reference for the CSS right property: how it offsets positioned elements from the right edge, percentages, conflicts with left, and logical alternatives.
The right property sets the horizontal offset of a positioned element from the right edge of its reference box. For an absolute element the reference is the padding edge of the nearest positioned ancestor; for fixed, the viewport. For a relative element, a positive value shifts the box to the left, because it pushes the element away from the right edge.
It has no effect on position: static elements.
.close-button { position: absolute; top: 0.5rem; right: 0.5rem; }
Try it
Syntax
right: auto | <length> | <percentage> | <anchor()> | <anchor-size()>;
- Initial value:
auto - Applies to: positioned elements
- Inherited: no
- Percentages: refer to the width of the containing block
- Animatable: yes, as a length or percentage
- Computed value: for positioned elements, an absolute length or a percentage as specified;
autowhere it cannot be resolved; forstaticelements,auto
Values
| Value | Description |
|---|---|
auto |
No offset from the right side. |
<length> |
A fixed distance from the right edge; negative values push the element beyond it. |
<percentage> |
A percentage of the containing block's width. |
anchor(...) |
A position taken from an anchor element (see anchor positioning). |
Usage notes
- Common uses: close buttons, badges, floating action buttons and anything pinned to the right corner.
- Scrollbars. For
fixedelements,right: 0sits against the viewport edge, usually inside any visible classic scrollbar. - Conflict with
left. Whenleft,widthandrightare all set on an absolutely positioned box,rightis ignored in left-to-right text. - Writing direction.
rightdoes not flip in right-to-left languages. Useinset-inline-endwhen the badge should follow the reading direction. - Relative positioning is counter-intuitive.
right: 20pxmoves the box left. Prefertransform: translateX()for visual nudges.
Browser support
Supported in every browser for many years.
Related
left,top,bottominset,inset-inlineposition- Lesson: CSS Position