CSS right Property
The CSS right property specifies the offset between a positioned element's right margin edge and the corresponding edge of its containing block or positioning reference.
It works with the position property to control horizontal placement. Its behavior depends on whether the element uses relative, absolute, fixed, or sticky positioning. The property accepts lengths, percentages, auto, and supported anchor-positioning functions.
right Syntax
The right property accepts a length, percentage, auto, or a supported anchor-positioning value.
Basic Syntax
selector {
right: 20px;
}
Available Values
.element {
right: auto;
}
.element {
right: 20px;
}
.element {
right: 2rem;
}
.element {
right: 10%;
}
.element {
right: -15px;
}
.element {
right: calc(10px + 2%);
}
.element {
right: anchor(left);
}
.element {
right: anchor-size(width);
}
CSS-Wide Keywords
.element {
right: initial;
}
.element {
right: inherit;
}
.element {
right: unset;
}
.element {
right: revert;
}
.element {
right: revert-layer;
}
The right property has no positioning effect on elements using position: static.
Property Values
| Value | Description |
|---|---|
auto |
Allows the browser to determine the right inset according to the positioning and layout rules. This is the initial value. |
<length> |
Specifies a fixed offset using units such as px, em, or rem. |
<percentage> |
Specifies an offset relative to the width of the containing block. |
calc() |
Calculates an offset using compatible lengths, percentages, and arithmetic expressions. |
anchor() |
Uses the position of an anchor element to calculate the inset when CSS Anchor Positioning is supported. |
anchor-size() |
Uses an anchor element's dimensions to calculate the inset when CSS Anchor Positioning is supported. |
initial |
Resets the property to auto. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to auto because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Positioned elements |
| Computed value | Absolute length or percentage as specified, or auto |
| Animation type | Length, percentage, or calculated value |
| Percentages | Relative to the containing block's width |
Valid Values
.panel {
right: 0;
}
.panel {
right: 24px;
}
.panel {
right: 5%;
}
.panel {
right: -10px;
}
.panel {
right: calc(100% - 250px);
}
Invalid Values
.panel {
right: center;
}
.panel {
right: none;
}
.panel {
right: 20;
}
.panel {
right: 10px 20px;
}
.panel {
right: right;
}
Nonzero numeric offsets require a unit. The property does not accept alignment keywords such as center or multiple space-separated offset values.
How It Works
The effect of right depends on the element's positioning mode.
| Position Value | Behavior of right |
|---|---|
static |
Has no effect on the element's position. |
relative |
Moves the element leftward for a positive offset, relative to its normal position. |
absolute |
Positions the element relative to the right edge of its containing block. |
fixed |
Positions the element relative to its fixed-position containing block, commonly the viewport. |
sticky |
Defines a right inset constraint used during sticky positioning. |
Basic Positioned Element
.container {
position: relative;
}
.box {
position: absolute;
right: 20px;
}
The box is positioned with its right margin edge 20 pixels from the containing block's right edge, assuming no conflicting sizing constraints.
Important Distinction
For absolutely positioned elements, right defines an inset from the containing block. For relatively positioned elements, it offsets the element from its normal position.
Relative Positioning
With position: relative, the right property shifts an element horizontally without removing it from the normal document flow.
Positive Right Offset
.box {
position: relative;
right: 30px;
}
The element moves 30 pixels to the left of its normal position.
Negative Right Offset
.box {
position: relative;
right: -30px;
}
The element moves 30 pixels to the right of its normal position.
HTML Example
<div class="relative-box">Relatively Positioned Box</div>
CSS Example
.relative-box {
position: relative;
right: 25px;
width: 200px;
padding: 15px;
background-color: #dbeafe;
border: 2px solid #2563eb;
}
The element moves left while its original space remains reserved in the document layout.
Important Consideration
Relative positioning changes the element's visual location but does not cause surrounding elements to move into its original space.
Absolute Positioning
With position: absolute, the right property places an element relative to its containing block, which is commonly established by a positioned ancestor.
Positioning Against a Container
.container {
position: relative;
height: 180px;
}
.box {
position: absolute;
top: 20px;
right: 20px;
}
The box appears near the top-right corner of its containing block.
Aligning With the Right Edge
.box {
position: absolute;
right: 0;
}
A zero inset places the element's right margin edge at the corresponding containing block edge.
Using a Percentage
.box {
position: absolute;
right: 10%;
}
The offset is calculated as 10% of the containing block's width.
Positioning Beyond the Container
.box {
position: absolute;
right: -20px;
}
A negative right inset can place the element beyond the containing block's right edge.
Important Consideration
Absolutely positioned elements are removed from normal document flow. Their placement does not reserve space for surrounding content.
Fixed Positioning
With position: fixed, the right property positions an element relative to its fixed-position containing block, which is commonly the viewport.
Fixed Right Offset
.floating-button {
position: fixed;
right: 20px;
bottom: 20px;
}
The button normally remains near the bottom-right corner of the viewport while the document scrolls.
Using Viewport-Relative Units
.floating-panel {
position: fixed;
right: 5vw;
top: 20px;
}
The right offset is based on 5% of the viewport width.
Fixed Positioning Within Transformed Ancestors
Some ancestor properties, including certain transforms, can establish a containing block for fixed-positioned descendants. In these cases, right may be measured relative to that ancestor instead of the viewport.
Important Consideration
Fixed elements can cover page content, particularly on narrow screens. Leave enough space for important controls and ensure the interface remains usable at increased zoom levels.
Sticky Positioning
With position: sticky, the right property defines a horizontal inset constraint that can affect the element's position while scrolling.
Basic Example
.sticky-column {
position: sticky;
right: 0;
}
This can be useful for keeping a column aligned with the right edge of a horizontal scrolling area.
Horizontal Scrolling Container
.scroll-container {
width: 100%;
overflow-x: auto;
}
.sticky-column {
position: sticky;
right: 0;
background-color: #ffffff;
}
The sticky element can remain constrained by the right edge of its scrolling area when horizontal scrolling would otherwise move it beyond that boundary.
Sticky Positioning Requirements
Sticky behavior depends on the scroll container, the element's normal position, available scrolling space, and the specified inset values.
Important Consideration
Unlike fixed positioning, sticky positioning remains constrained by the element's containing block and scrollport. A right inset does not automatically make an element stick to the right edge in every layout.
Lengths, Percentages, and Negative Values
The right property supports different CSS units and calculations, making it useful for fixed and responsive layouts.
Pixel Values
.box {
position: absolute;
right: 24px;
}
Pixels provide a specific offset from the right edge.
Relative Length Units
.box {
position: absolute;
right: 2rem;
}
The offset scales with the root element's font size.
Percentage Values
.box {
position: absolute;
right: 10%;
}
The offset is calculated relative to the containing block's width.
Negative Values
.box {
position: absolute;
right: -15px;
}
Negative values move the right inset beyond the reference edge, subject to the applicable positioning rules.
Calculated Values
.box {
position: absolute;
right: calc(5% + 10px);
}
The calc() function combines percentage and length values.
Responsive Offset
.box {
position: absolute;
right: clamp(10px, 3vw, 40px);
}
The clamp() function limits the offset to a minimum of 10 pixels and a maximum of 40 pixels.
Using left and right Together
When both left and right are specified, their combined effect depends on the element's positioning mode, width, margins, and writing direction.
Stretching an Absolutely Positioned Element
.panel {
position: absolute;
left: 20px;
right: 20px;
width: auto;
}
With automatic width and suitable margins, the element can stretch between the two specified insets.
Conflicting Insets With a Fixed Width
.panel {
position: absolute;
left: 20px;
right: 20px;
width: 200px;
margin-left: 0;
margin-right: 0;
}
If these constraints cannot all be satisfied, the browser resolves the over-constrained layout according to the applicable writing direction and positioning rules.
Relative Positioning With Both Insets
.box {
position: relative;
left: 20px;
right: 10px;
}
For relative positioning, conflicting horizontal offsets are resolved according to the containing block's direction. In a left-to-right context, left takes precedence; in a right-to-left context, right takes precedence.
Important Consideration
When positioning with both horizontal insets, account for width, margins, and writing direction rather than assuming both values will always determine the final edges.
Logical Positioning
The right property uses a physical direction. Its meaning does not change with the document's writing mode or text direction.
For layouts that should adapt to writing direction, consider the logical inset properties inset-inline-start and inset-inline-end.
Physical Right Positioning
.box {
position: absolute;
right: 20px;
}
The element is positioned relative to the physical right edge.
Logical Inline-End Positioning
.box {
position: absolute;
inset-inline-end: 20px;
}
In a left-to-right horizontal writing mode, inset-inline-end corresponds to the right edge. In a right-to-left horizontal writing mode, it corresponds to the left edge.
Logical Inline-Start Positioning
.box {
position: absolute;
inset-inline-start: 20px;
}
The inset follows the start of the inline axis rather than a fixed physical side.
Important Consideration
Use physical positioning when the element must remain tied to a specific side. Use logical positioning when placement should follow the document's writing direction.
right Example
The following example demonstrates how the right property positions a box inside a relatively positioned container.
HTML Example
<div class="right-demo">
<div class="right-box">right: 20px;</div>
</div>
CSS Example
.right-demo {
position: relative;
height: 180px;
padding: 15px;
border: 2px dashed #94a3b8;
background-color: #f1f5f9;
}
.right-demo .right-box {
position: absolute;
top: 25px;
right: 20px;
width: 150px;
padding: 15px;
background-color: #2563eb;
color: #ffffff;
text-align: center;
box-sizing: border-box;
}
The blue box is positioned 20 pixels from the right edge and 25 pixels from the top edge of its containing block.
Changing the Right Offset
.right-demo .right-box {
right: 50px;
}
Increasing the right inset moves the box farther toward the left.
Aligning With the Right Edge
.right-demo .right-box {
right: 0;
}
The box aligns with the containing block's right padding edge.
Using a Negative Offset
.right-demo .right-box {
right: -10px;
}
The box extends beyond the containing block's right edge.
Accessibility Considerations
Positioned elements should remain readable, reachable, and understandable regardless of their visual location.
Preserve Logical Document Order
CSS positioning changes visual placement without changing the HTML source order or keyboard navigation sequence.
Avoid Covering Important Content
Absolutely and fixed-positioned elements can overlap text, buttons, and form controls. Test layouts at different viewport sizes.
Support Zoom and Reflow
Ensure positioned elements remain usable when users increase browser zoom or text size.
Maintain Visible Focus
Keyboard focus indicators should not be hidden behind positioned panels or floating controls.
Use Responsive Offsets
Consider percentages, relative units, or clamp() when fixed offsets could cause overflow on smaller screens.
Consider Writing Direction
Use logical inset properties when positioning should adapt to right-to-left or vertical writing modes.
Important Consideration
The right property controls visual positioning only. It does not change semantic relationships, reading order, or focus behavior.
Common Mistakes
Using right Without Positioning
.box {
right: 20px;
}
With the default position: static, the property has no positioning effect.
Forgetting the Containing Block
.box {
position: absolute;
right: 20px;
}
Without an intended positioning ancestor, the element may be positioned relative to a different containing block than expected.
Confusing Positive and Negative Offsets
For relatively positioned elements, a positive right value moves the element left, while a negative value moves it right.
Using Unitless Nonzero Values
.box {
right: 20;
}
Nonzero lengths require a unit, such as px, rem, or vw.
Assuming Percentages Use the Viewport
Percentage offsets are calculated relative to the containing block's width, which is not necessarily the viewport width.
Ignoring Conflicting Horizontal Insets
Using left, right, and a fixed width together can create over-constrained positioning.
Using Fixed Positioning Without Responsive Testing
Fixed elements can overlap content or become difficult to use on narrow screens.
Confusing Physical and Logical Directions
The right property always refers to the physical right side. Use logical inset properties when placement should adapt to writing direction.
Important Considerations
Keep the following points in mind when using right:
- It defines the right inset of a positioned element.
- Its initial value is
auto. - The property is not inherited.
- It has no positioning effect on elements using
position: static. - Its behavior depends on the element's positioning mode.
- With relative positioning, positive values move the element left.
- With relative positioning, negative values move the element right.
- Relative positioning preserves the element's original layout space.
- Absolute positioning uses the element's containing block.
- Absolutely positioned elements are removed from normal flow.
- Fixed positioning commonly uses the viewport as its containing block.
- Some ancestor properties can establish a fixed-position containing block.
- Sticky positioning uses inset constraints during scrolling.
- Percentage values are relative to the containing block's width.
- Negative offsets are permitted.
- Nonzero length values require units.
- The
calc()function can combine compatible offset values. - The
clamp()function can constrain responsive offsets. - Both
leftandrightcan be specified on the same element. - Width and margin constraints affect how opposing insets are resolved.
- The
rightproperty uses a physical direction. - Logical inset properties adapt to writing mode and direction.
- CSS Anchor Positioning can provide anchor-based inset values.
- Positioned elements may overlap surrounding content.
- Visual positioning does not change HTML source order.
- Test positioned layouts at different viewport sizes and zoom levels.
Browser Support
The CSS right property is Baseline Widely available and is supported in all major browsers.
Standard length, percentage, and auto values have long-standing support. Newer anchor-positioning functions such as anchor() and anchor-size() have more limited availability.
For current compatibility information, visit Can I Use? and search for right.
When using newer positioning features, check support for the specific function or related CSS feature rather than relying only on support for the right property.
Specifications
The right property is defined in CSS Positioned Layout Module Level 3.
CSS Positioned Layout Module Level 3
The specification defines the inset properties and explains their behavior with different positioning modes, containing blocks, and layout constraints.
For additional documentation and examples, consult MDN Web Docs.
