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

ValueWhat it does
staticnormal flow; top, right, bottom, left and z-index have no effect
relativestays in the flow (its space is kept) and is shifted by top/left; becomes the reference point for absolute descendants
absolutetaken out of the flow and takes no space; placed relative to the nearest ancestor whose position is not static
fixedtaken out of the flow and fixed to the window: it does not scroll with the page
stickybehaves 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

▶ Open in HTML Editor

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

See also