CSS Reference
position
Controls how an element is positioned in the document. Five values, one foundational property.
Last updated: October 2026
Definition: The position property controls how an element is placed in the document. It has five values: static (default), relative, absolute, fixed, and sticky. Combined with top, right, bottom, and left, it lets you place elements anywhere.
🎬 Live Examples
The three most-used positioning values in action.
Scroll this container. When the blue bar reaches the top, it sticks. It stays fixed until its parent scrolls out of view.
More content to enable scrolling...
Keep scrolling...
Almost there...
Done.
💡 Relative, absolute, and sticky are the three positioning values you'll use most.
Basic Syntax
.element {
position: relative;
top: 10px;
left: 20px;
}
Position alone does nothing. You need top, right, bottom, or left to actually move the element — except for static.
🎯 All position Values
| Value | Behavior | Common Use |
|---|---|---|
static | Default. Normal flow. | Most elements |
relative | Stays in flow, can offset | Parent for absolute children |
absolute | Removed from flow, positioned to nearest ancestor | Badges, dropdowns, overlays |
fixed | Removed from flow, positioned to viewport | Fixed navbars, chat widgets |
sticky | Stays in flow until scroll threshold | Sticky headers, table headers |
🎯 Offset Properties
| Property | Purpose |
|---|---|
top | Distance from the top edge of the positioned ancestor. |
right | Distance from the right edge. |
bottom | Distance from the bottom edge. |
left | Distance from the left edge. |
z-index | Stacking order. Higher = on top. |
relative — Offset From Normal
The element stays in the flow, but you can visually shift it.
.card {
position: relative;
top: 10px;
left: 20px;
}
When to use:
- To make an ancestor for absolute children (most common use).
- For subtle visual offsets without breaking the layout.
- To establish a stacking context for z-index.
absolute — Positioned to Ancestor
Removed from flow. Positions itself relative to the nearest positioned ancestor (any ancestor with position other than static).
.card {
position: relative; /* Establish the anchor */
}
.badge {
position: absolute;
top: 8px;
right: 8px;
background: red;
color: white;
padding: 4px 8px;
border-radius: 4px;
}
💡 The rule: If a child has position: absolute, always make its parent position: relative (unless you want it positioned to the page).
fixed — Positioned to Viewport
Stays in the same screen position when scrolling.
.back-to-top {
position: fixed;
bottom: 2rem;
right: 2rem;
background: #3b82f6;
color: white;
padding: 1rem;
border-radius: 50%;
cursor: pointer;
}
.fixed-navbar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
}
The element is removed from flow — add padding or margin to other content to prevent overlap. On mobile, fixed can cause issues with the on-screen keyboard. Test carefully.
sticky — Sticks Until Parent Ends
Behaves like relative until the scroll position reaches a threshold, then behaves like fixed — but stays within its parent.
.sticky-header {
position: sticky;
top: 0;
background: white;
z-index: 10;
}
Requirements:
- Must have a
top,right,bottom, orleftvalue. - Its parent must be taller than the sticky element.
- No ancestor can have
overflow: hiddenoroverflow: autobetween the sticky element and the scroll container.
z-index — Stacking Order
Controls which elements appear on top when they overlap.
.modal {
position: fixed;
z-index: 1000;
}
.tooltip {
position: absolute;
z-index: 100;
}
.navbar {
position: sticky;
top: 0;
z-index: 50;
}
Rules:
z-indexonly works on positioned elements (not static).- Higher numbers appear on top of lower numbers.
- A new stacking context is created by position + z-index, transforms, filters, or opacity less than 1.
Common z-index scale:
- Base content:
0 - Dropdowns:
10–100 - Sticky headers:
100–500 - Modals:
1000+ - Toasts:
9999
🎯 Real-World Patterns
| Pattern | Position |
|---|---|
| Navbar fixed to top | fixed; top: 0 |
| Back-to-top button | fixed; bottom: 2rem; right: 2rem |
| Notification badge on icon | absolute; top: 0; right: 0 |
| Dropdown menu below a button | absolute; top: 100%; left: 0 |
| Modal overlay covering the page | fixed; inset: 0; z-index: 1000 |
| Sticky table header | sticky; top: 0 |
| Tooltip on hover | absolute; z-index: 100 |
⚠️ Common Mistakes
- Using
position: absolutewithout a positioned parent. The element positions itself to the nearest positioned ancestor — often<body>, which breaks the layout. - Expecting
z-indexto work without position. z-index only works on positioned elements. Addposition: relativeif you need to control stacking. - Forgetting
top: 0on sticky elements. Sticky requires a threshold. Without it, the element never sticks. - Using
position: fixedinside a transformed parent. The containing block becomes the transformed parent, not the viewport. This breaks expected behavior. - Not accounting for fixed navbars. Add padding-top to
<body>equal to the navbar height, or your content will be hidden underneath. - Using too many
z-indexvalues. Define a scale (0, 10, 100, 1000) and stick to it. Random values cause "z-index wars." - Applying
overflow: hiddento a parent of a sticky element. This breaks stickiness. Ensure the scroll container has no overflow that clips the sticky child. - Using position for layout. Use Flexbox or Grid for layout. Position is for overlays, badges, and specific placements.
♿ Accessibility Notes
- Fixed navbars must not cover content. Add sufficient padding or margin below the fixed element.
- Sticky headers are fine, but test with mobile zoom. Users who zoom in might find the sticky header taking too much space.
- z-index stacking must not hide interactive elements. A high z-index overlay that blocks buttons is an accessibility failure.
- Modals need focus trapping. Positioning alone doesn't handle keyboard focus. Use
<dialog>or manage focus with JavaScript. - Test with keyboard navigation. Positioned elements can fall out of tab order or overlap other focusable elements.
- Avoid
position: fixedfor critical content. If it covers content when users zoom, the content becomes inaccessible.
❓ Frequently Asked Questions
What is the CSS position property?
The position property controls how an element is positioned in the document. It has five values: static (default), relative, absolute, fixed, and sticky. Combined with top, right, bottom, and left, it lets you place elements anywhere on the page.
What is the difference between relative and absolute?
position: relative keeps the element in the normal flow, but lets you offset it from its original position. position: absolute removes the element from the flow and positions it relative to the nearest positioned ancestor.
What is the difference between fixed and sticky?
position: fixed pins the element to the viewport — it stays in place when scrolling. position: sticky behaves like relative until the scroll reaches a threshold, then sticks. Sticky stays within its parent; fixed does not.
What is z-index in CSS?
z-index controls stacking order of positioned elements. Higher values appear on top of lower values. z-index only works on positioned elements (relative, absolute, fixed, or sticky).
How do I center a div with position absolute?
Set position: absolute, top: 50%, left: 50%, and transform: translate(-50%, -50%). The transform offsets by half the element's own width and height, achieving perfect centering.