CSS position Property
The CSS position property determines how an element is positioned in a document and how its location can be adjusted using inset properties.
It supports normal document flow, relative offsets, absolute positioning, viewport-relative positioning, and sticky positioning during scrolling. Understanding these positioning methods is essential for creating overlays, navigation bars, labels, tooltips, and responsive layouts.
position Syntax
The position property accepts one positioning keyword.
Basic Syntax
selector {
position: relative;
}
Positioning Values
.element {
position: static;
}
.element {
position: relative;
}
.element {
position: absolute;
}
.element {
position: fixed;
}
.element {
position: sticky;
}
Positioning With Offsets
.element {
position: absolute;
top: 20px;
right: 20px;
}
The inset properties top, right, bottom, and left determine offsets or constraints according to the positioning method.
CSS-Wide Keywords
.element {
position: initial;
}
.element {
position: inherit;
}
.element {
position: unset;
}
.element {
position: revert;
}
.element {
position: revert-layer;
}
Property Values
| Value | Description |
|---|---|
static |
Uses normal document flow. The inset properties do not offset the element. |
relative |
Positions the element in normal flow, then offsets it relative to its original position. |
absolute |
Removes the element from normal flow and positions it relative to its containing block. |
fixed |
Removes the element from normal flow and positions it relative to a fixed-position containing block, usually the viewport. |
sticky |
Keeps the element in normal flow while allowing it to stick at specified offsets within its scrolling constraints. |
initial |
Resets the property to static. |
inherit |
Uses the computed position value of the parent element. |
unset |
Resets the property to static 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 | static |
| Inherited | No |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable to position itself |
Valid Values
.header {
position: sticky;
}
.badge {
position: absolute;
}
.panel {
position: relative;
}
.toolbar {
position: fixed;
}
Invalid Values
.element {
position: center;
}
.element {
position: top;
}
.element {
position: 20px;
}
.element {
position: relative absolute;
}
The property requires one supported positioning keyword. Coordinates and offsets belong in separate inset declarations.
How It Works
CSS positioning determines how an element participates in document layout and which reference area is used to calculate its location.
Normal Document Flow
Elements normally appear in the order and layout established by HTML and CSS. Static, relative, and sticky positioning preserve an element's normal-flow space.
Out-of-Flow Positioning
Absolutely and fixed-positioned elements are removed from normal flow. Other elements generally behave as though those positioned elements do not occupy their original layout space.
Positioned Elements
An element is considered positioned when its position value is anything other than static.
Positioning Reference Areas
Relative positioning uses the element's original location. Absolute and fixed positioning use a containing block. Sticky positioning combines normal-flow layout with scrolling constraints.
Offsets
The inset properties can move or constrain positioned elements. Their effects depend on the chosen positioning method.
Positioning Does Not Replace Layout
Use Flexbox or Grid for the overall arrangement of page content. Use positioning when an element needs special placement, layering, or scroll-related behavior.
Static Positioning
The static value is the default positioning method for elements.
.element {
position: static;
}
A statically positioned element follows normal document flow.
Inset Properties Have No Effect
.element {
position: static;
top: 20px;
left: 30px;
}
The top and left declarations do not move the element because it is statically positioned.
When to Use static
Use static to restore normal positioning behavior or when no special positioning is required.
Important Consideration
Normal document flow remains the preferred foundation for most page content.
Relative Positioning
The relative value offsets an element from its original position without removing its original space from normal flow.
.element {
position: relative;
top: 10px;
left: 20px;
}
The element moves visually 10 pixels downward and 20 pixels to the right. Its original layout space remains reserved.
Original Space Is Preserved
Surrounding elements do not move into the space the relatively positioned element originally occupied.
Creating an Absolute Positioning Reference
.card {
position: relative;
}
.card-badge {
position: absolute;
top: 10px;
right: 10px;
}
A relatively positioned parent commonly establishes the containing block for an absolutely positioned child.
Relative Without Offsets
.card {
position: relative;
}
The parent remains in its original location but can still serve as a positioning reference for descendants.
Important Consideration
Relative positioning is useful for small visual adjustments, but it does not reorganize the surrounding layout.
Absolute Positioning
The absolute value removes an element from normal flow and positions it relative to its containing block.
Basic Example
.parent {
position: relative;
}
.child {
position: absolute;
top: 10px;
right: 10px;
}
The child is positioned near the top-right corner of the parent's padding box.
HTML Structure
<div class="parent">
<p>Card content</p>
<span class="child">New</span>
</div>
Positioning Ancestors
An absolutely positioned element typically uses the nearest ancestor that establishes an absolute-positioning containing block. A non-static positioned ancestor is a common example, but certain properties such as transform can also establish containing blocks.
Without a Positioned Ancestor
If no ancestor establishes a suitable containing block, the element generally uses the initial containing block.
Centering an Absolute Element
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
The percentage offsets locate the child's positioning reference at the center of the containing block. The transform then moves the child by half its own dimensions.
Stretching With Insets
.child {
position: absolute;
inset: 10px;
}
An auto-sized element can stretch to fit the available area while maintaining the specified offsets, subject to sizing constraints.
Important Consideration
Because absolute positioning removes an element from normal flow, the parent may not automatically expand to accommodate that child's dimensions.
Fixed Positioning
The fixed value removes an element from normal flow and positions it relative to a fixed-position containing block, usually the viewport.
Fixed Corner Button
.back-to-top {
position: fixed;
right: 20px;
bottom: 20px;
}
The button normally remains at the same viewport location as the document scrolls.
Fixed Header
.site-header {
position: fixed;
top: 0;
left: 0;
right: 0;
}
The header remains positioned at the top of its fixed-position containing block.
Preventing Content Overlap
.site-header {
position: fixed;
top: 0;
height: 70px;
}
main {
padding-top: 70px;
}
Additional spacing may be necessary because a fixed element does not reserve normal-flow space.
Containing Block Exceptions
Properties such as transform, filter, or perspective can cause an ancestor to establish the containing block for fixed-positioned descendants.
Printing
Fixed-positioned elements can behave differently in paged media and may be repeated on printed pages.
Important Consideration
Fixed elements can obscure content on small screens. Ensure sufficient space and avoid blocking important controls.
Sticky Positioning
The sticky value combines normal-flow positioning with offset constraints that can keep an element visible while scrolling.
Sticky Navigation
.navigation {
position: sticky;
top: 0;
}
The navigation remains in normal flow until scrolling reaches its specified top constraint.
Sticky Sidebar
.sidebar {
position: sticky;
top: 20px;
}
The sidebar can remain near the top of its scrollport while scrolling, subject to the boundaries of its containing block.
An Inset Is Usually Required
.element {
position: sticky;
top: 0;
}
For sticky positioning to operate along an axis, at least one inset property on that axis must be something other than auto.
Scrolling Ancestors
Sticky positioning uses the nearest ancestor with a scrolling mechanism for its sticky constraints, which may not be the element that actually scrolls.
Container Boundaries
A sticky element remains constrained by its containing block. It does not remain fixed indefinitely as the entire document scrolls.
Overflow Considerations
Ancestor overflow settings can change which scrollport controls sticky positioning. If a sticky element does not behave as expected, inspect overflow on its ancestors.
Important Consideration
Sticky positioning works best when the relevant scrolling area provides enough space for the element to move before reaching its constraints.
Inset Properties
The inset properties define offsets or constraints for positioned elements.
Physical Inset Properties
.element {
position: absolute;
top: 10px;
right: 20px;
bottom: 30px;
left: 40px;
}
The inset Shorthand
.element {
position: absolute;
inset: 10px 20px 30px 40px;
}
The four values correspond to top, right, bottom, and left.
Two-Value Shorthand
.element {
position: absolute;
inset: 10px 20px;
}
The first value applies to top and bottom. The second applies to right and left.
Logical Inset Properties
.element {
position: absolute;
inset-block-start: 10px;
inset-inline-end: 20px;
}
Logical inset properties follow the element's writing mode and direction rather than fixed physical sides.
Percentages
.element {
position: absolute;
top: 25%;
left: 50%;
}
Percentage inset values are resolved against the relevant dimensions of the containing block.
Important Consideration
Inset properties do not offset statically positioned elements. Their behavior depends on the element's positioning method.
Containing Blocks
A containing block is the reference area used to calculate an element's position and certain dimensions.
Relative Positioning
Relative offsets are calculated from the element's normal-flow position.
Absolute Positioning
An absolutely positioned element uses the nearest ancestor that establishes an absolute-positioning containing block, or the initial containing block when no such ancestor exists.
Fixed Positioning
A fixed-positioned element normally uses the viewport as its reference, although certain ancestor properties can establish a different containing block.
Sticky Positioning
A sticky element retains its normal-flow position and uses scrolling constraints while remaining within its containing block.
Practical Parent-Child Example
.card {
position: relative;
padding: 20px;
}
.card-label {
position: absolute;
top: 8px;
right: 8px;
}
The card establishes the containing block used by the absolutely positioned label.
Other Containing Block Creators
Properties including transform, filter, perspective, and certain containment settings can establish containing blocks for positioned descendants.
Important Consideration
When an element appears in an unexpected location, inspect its ancestor elements and determine which one establishes its containing block.
Stacking and z-index
Positioned elements can overlap. The z-index property helps control their stacking order.
Basic Stacking Example
.panel {
position: relative;
z-index: 1;
}
.popup {
position: absolute;
z-index: 10;
}
Within compatible stacking contexts, the popup can appear above the panel.
Positioned Elements and Stacking Contexts
Positioned elements with a non-auto z-index establish stacking contexts. Fixed and sticky positioning also establish stacking contexts.
Nested Stacking Contexts
A descendant cannot escape its ancestor's stacking context simply by using a very large z-index.
Overlapping Content
Positioning and stacking can cause elements to cover links, buttons, or text. Test overlapping areas carefully.
Important Consideration
Use a consistent stacking strategy instead of assigning increasingly large z-index values to solve every overlap.
Positioning vs. Layout
Positioning is not a replacement for Flexbox or Grid. Each method solves a different layout problem.
Use Flexbox
Flexbox is useful for arranging items in one dimension, such as navigation links, toolbars, and button groups.
.toolbar {
display: flex;
align-items: center;
gap: 12px;
}
Use Grid
Grid is useful for arranging content in rows and columns.
.layout {
display: grid;
grid-template-columns: 250px 1fr;
gap: 20px;
}
Use Absolute Positioning
Absolute positioning is useful for badges, overlays, and controls attached to a specific container.
.card {
position: relative;
}
.card-badge {
position: absolute;
top: 10px;
right: 10px;
}
Use Fixed Positioning
Fixed positioning is useful for controls that need to remain at a consistent viewport location.
Use Sticky Positioning
Sticky positioning is useful for navigation, headings, and sidebars that should remain visible within scrolling constraints.
Important Consideration
Build the main document layout with normal flow, Flexbox, or Grid, then add special positioning only where necessary.
position Example
The following example demonstrates relative positioning on a card and absolute positioning on a badge.
HTML Example
<div class="position-demo">
<div class="demo-card">
<span class="demo-badge">NEW</span>
<h3>CSS Positioning</h3>
<p>The badge is positioned relative to this card.</p>
</div>
</div>
CSS Example
.position-demo .demo-card {
position: relative;
min-height: 150px;
padding: 30px 20px 20px;
border: 2px solid #94a3b8;
background-color: #f1f5f9;
}
.position-demo .demo-badge {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
background-color: #2563eb;
color: #ffffff;
font-size: 12px;
font-weight: 700;
}
.position-demo h3 {
margin: 0 0 10px;
}
.position-demo p {
margin: 0;
}
CSS Positioning
The badge is positioned relative to this card.
The card uses position: relative to establish a positioning reference. The badge uses position: absolute with top and right offsets to appear near the card's upper-right corner.
Moving the Badge
.position-demo .demo-badge {
position: absolute;
bottom: 10px;
left: 10px;
}
Replace the original top and right declarations with these values to position the badge near the lower-left corner.
Removing Absolute Positioning
.position-demo .demo-badge {
position: static;
}
The badge returns to normal document flow, and the inset offsets no longer affect it.
Accessibility Considerations
CSS positioning changes visual placement without automatically changing HTML source order or accessibility semantics.
Maintain Logical Reading Order
Keep content in a meaningful source order so keyboard and screen reader navigation remain predictable.
Avoid Covering Interactive Elements
Absolutely and fixed-positioned elements can overlap links, buttons, and form controls. Ensure important controls remain accessible.
Support Zoom and Text Resizing
Test positioned content at increased zoom levels and with larger text. Fixed dimensions and offsets can cause clipping or overlap.
Use Sticky Elements Carefully
Sticky headers and navigation should not consume excessive viewport space or obscure focused elements.
Preserve Keyboard Focus Visibility
Ensure fixed or sticky elements do not hide the currently focused control. Properties such as scroll-margin-top and scroll-padding-top can help with scroll positioning.
Avoid Visual and Source Order Conflicts
Do not use absolute positioning to create a visual sequence that differs substantially from the logical reading order.
Important Consideration
Positioned content should remain readable, operable, and understandable across different viewport sizes and input methods.
Common Mistakes
Using Offsets With static
.element {
position: static;
top: 20px;
}
The top declaration does not offset a statically positioned element.
Forgetting the Absolute Positioning Reference
.badge {
position: absolute;
top: 10px;
right: 10px;
}
Without the intended containing block, the badge may appear somewhere unexpected.
Removing Elements From Flow Unintentionally
Absolute and fixed positioning do not reserve normal-flow space, which can cause overlapping content.
Using relative for Major Layout Changes
Relative offsets move an element visually while preserving its original space. Use Grid or Flexbox for major layout arrangements.
Forgetting Sticky Offsets
.header {
position: sticky;
}
Without a non-auto inset on the relevant axis, sticky positioning behaves like relative positioning along that axis.
Ignoring Ancestor Overflow
Overflow settings on ancestors can change sticky positioning behavior and the relevant scrolling container.
Assuming fixed Always Means Viewport
Some ancestor properties establish containing blocks for fixed-positioned descendants.
Using Excessive z-index Values
Large z-index values do not allow descendants to escape their stacking contexts.
Confusing position With display
The position property controls positioning behavior. The display property controls the element's display and layout participation.
Confusing position With transform
The transform property visually transforms an element and can affect containing blocks and stacking contexts. It does not replace the positioning model.
Important Considerations
Keep the following points in mind when using position:
- The initial value is
static. - The property is not inherited.
- It applies to all elements.
- Its animation type is discrete.
staticfollows normal document flow.- Inset properties do not offset statically positioned elements.
relativepreserves the element's original layout space.- Relative offsets move an element visually from its normal position.
absoluteremoves the element from normal flow.- Absolute positioning uses an established containing block.
- A relatively positioned parent commonly provides a reference for an absolutely positioned child.
fixedremoves the element from normal flow.- Fixed positioning normally uses the viewport but can use an ancestor-established containing block.
stickycombines normal-flow positioning with scrolling constraints.- Sticky positioning requires a non-auto inset on the axis where sticking is desired.
- Sticky elements remain constrained by their containing blocks.
- Ancestor overflow settings can affect sticky positioning.
- Use
top,right,bottom, andleftto define physical offsets. - Use
insetas a shorthand for physical inset properties. - Use logical inset properties for writing-mode-aware positioning.
- Positioned elements can overlap surrounding content.
z-indexcan control stacking within applicable stacking contexts.- Fixed and sticky positioning establish stacking contexts.
- Ancestor stacking contexts limit how descendants can overlap other content.
- Use Grid or Flexbox for primary page layout.
- Use positioning for overlays, badges, sticky navigation, and similar specialized placement.
- Positioning does not automatically change HTML source order.
- Test positioned elements at different viewport sizes and zoom levels.
- Ensure positioned content does not obscure important interactive controls.
Browser Support
The CSS position property is Widely available in modern browsers, including Chrome, Edge, Firefox, and Safari.
The static, relative, absolute, and fixed values have long-standing browser support. The sticky value is also widely supported, although its behavior depends on scrolling containers, inset values, and layout constraints.
For current compatibility information, visit Can I Use? and search for position: sticky.
Specifications
The position property is defined in CSS Positioned Layout Module Level 3.
CSS Positioned Layout Module Level 3
The specification defines the positioning schemes, containing block behavior, and relationship between positioned elements and inset properties.
For additional documentation and examples, consult MDN Web Docs.
