Encodera

Thanks for reading!

Donate to the site — we work hard for you.

HTML & CSS reference

position

How an element is positioned: in the normal flow or by offsets.

Syntax

position: value;

Values

ParameterTypeDescription
statickeywordIn the normal flow; offsets do not work (the default).
relativekeywordShifted from its normal place, which stays reserved; also a reference point for absolute children.
absolutekeywordTaken out of the flow and placed relative to the nearest positioned ancestor.
fixedkeywordPlaced relative to the window and does not scroll with the page.
stickykeywordScrolls with the page until it reaches the given offset, then sticks.

Examples

A badge in the corner of a card — absolute inside relative:

<style>
  .card { position: relative; width: 200px; padding: 16px; background: whitesmoke; }
  .badge { position: absolute; top: 8px; right: 8px; background: crimson; color: white; padding: 2px 6px; }
</style>
 
<div class="card">Product card <span class="badge">New</span></div>

A sticky header:

<div style="height: 120px; overflow: auto; border: 1px solid gray;">
  <h4 style="position: sticky; top: 0; margin: 0; background: gold;">Sticky header</h4>
  <p>Scroll this box.</p><p>The header stays at the top.</p><p>More text.</p><p>And more.</p>
</div>

Notes

An absolutely positioned element looks for the nearest ancestor with any position other than static; if there is none, it is placed relative to the page. sticky needs an offset like top: 0 and does not work if an ancestor has overflow: hidden.

See also

  • top, right, bottom, left — The offsets of a positioned element from the edges of its reference box: top, right, bottom and left.
  • z-index — Which positioned element is drawn on top when they overlap.
  • margin-inline — Logical properties: sizes and spacing by the direction of the text instead of left, right, top and bottom.
  • anchor-name — Names an element as an anchor, so that an absolutely positioned element anywhere on the page can be placed next to it.

Related recipes

In the tutorial

  • Positioning — The position property — static, relative, absolute, fixed and sticky, the top, right, bottom, left and inset offsets.

Thanks for reading!

Donate to the site — we work hard for you.