The position property
position sets how an element is placed: in the normal flow, shifted, relative to a parent, relative to the window, or "sticking" while you scroll. top, right, bottom, left and z-index work together with it.
Syntax
position: static | relative | absolute | fixed | sticky;
top: 0; right: 0; bottom: 0; left: 0; - Initial value
static- Inherited
- no
- Applies to
- all elements except table-column-group and table-column
Values
| Value | What it does |
|---|---|
static | normal flow; top, right, bottom, left and z-index have no effect |
relative | stays in the flow (its space is kept) and is shifted by top/left; becomes the reference point for absolute descendants |
absolute | taken out of the flow and takes no space; placed relative to the nearest ancestor whose position is not static |
fixed | taken out of the flow and fixed to the window: it does not scroll with the page |
sticky | behaves like relative until scrolling reaches the top/bottom threshold, then sticks within its parent |
Example
.scroll {
height: 120px;
overflow: auto;
border: 1px solid #94a3b8;
}
.bar {
position: sticky;
top: 0;
background: #1e293b;
color: #fff;
padding: 6px 10px;
}
.card {
position: relative;
margin-top: 24px;
padding: 16px;
border: 1px solid #94a3b8;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background: #dc2626;
color: #fff;
padding: 2px 6px;
border-radius: 4px;
} <div class="scroll">
<div class="bar">position: sticky — scroll this box</div>
<p>Line 1</p><p>Line 2</p><p>Line 3</p><p>Line 4</p><p>Line 5</p>
</div>
<div class="card">A card with position: relative <span class="badge">new</span></div> Result
Good to know
- sticky does nothing without top, bottom, left or right, and it sticks inside an ancestor with overflow: hidden, auto or scroll if there is one between the element and the scrolling area.
- For absolute, give the parent position: relative — otherwise the element is placed relative to the nearest positioned ancestor or the whole page.
- z-index only affects positioned elements (position other than static) and flex and grid items.
- If an ancestor has a transform, a position: fixed element is fixed to that ancestor, not to the window.
Sources and specification
- MDN: the position property — MDN Web Docs
- CSS Positioned Layout Module Level 3 (W3C) — W3C
See also
Sticky header
A header that stays visible while the page scrolls: copy-ready HTML and CSS with position: sticky and top: 0, plus why sticky needs a scrolling parent.
Tooltip (CSS-only)
A hover tooltip built with CSS ::after and a transition.
Simple modal (no JS)
A modal dialog that opens and closes with pure CSS via the :target pseudo-class.
The display property
The CSS display property sets how an element takes part in layout: as a block, inline with text, as a flex or grid container, or not at all. Values, examples.
The margin property
The CSS margin property sets an element's outer spacing: the 1–4 value forms, margin: auto for centering, negative margins and how vertical margins collapse.