Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS property `position`

`position` selects a box’s positioning scheme: `static` leaves it unpositioned, `relative` offsets it after normal layout, `absolute` removes it from flow, `sticky` constrains its offset during scrolling, and `fixed` usually attaches it to the viewport.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

static | relative | absolute | sticky | fixed

position selects a box’s positioning scheme: static leaves it unpositioned, relative offsets it after normal layout, absolute removes it from flow, sticky constrains its offset during scrolling, and fixed usually attaches it to the viewport.

The example measures that static ignores an inset, relative shifts the rendering without moving the following sibling, absolute occupies no flow space in its containing block, and fixed keeps viewport coordinates after scrolling.

Grammar: static | relative | absolute | sticky | fixed.

Every value other than static makes a positioned box and establishes an absolute-positioning containing block for descendants. absolute and fixed blockify the box and can overlap content; sticky needs an inset and a suitable scrolling area to produce a visible sticking effect.

Examples

<!doctype html><meta charset="utf-8"><style>html,body{margin:0}body{height:1000px}.flow{width:40px;height:20px}.cb{position:absolute;left:300px;top:0;width:200px;height:60px}.cb>*{width:20px;height:20px}.fixed{position:fixed;left:8px;top:6px;width:10px;height:10px}</style><div id="staticBox" class="flow" style="position:static;top:25px"></div><div id="relativeBox" class="flow" style="position:relative;top:15px"></div><div id="afterBox" class="flow"></div><div id="cb" class="cb"><i id="absoluteBox" style="position:absolute;left:70px;top:10px"></i><i id="flowBox"></i></div><i id="fixedBox" class="fixed"></i><pre id="result"></pre><script>const point=e=>{const r=e.getBoundingClientRect();return[Math.round(r.left),Math.round(r.top)]},s=point(staticBox),r=point(relativeBox),after=point(afterBox),abs=point(absoluteBox),flow=point(flowBox),before=point(fixedBox),values=[staticBox,relativeBox,absoluteBox,fixedBox].map(e=>getComputedStyle(e).position);scrollTo(0,200);requestAnimationFrame(()=>{const fixedAfter=point(fixedBox);if(s.join(',')!=='0,0'||r.join(',')!=='0,35'||after.join(',')!=='0,40'||abs.join(',')!=='370,10'||flow.join(',')!=='300,0'||before.join(',')!=='8,6'||fixedAfter.join(',')!=='8,6'||values.join('/')!=='static/relative/absolute/fixed')throw new Error('position geometry mismatch '+[s,r,after,abs,flow,before,fixedAfter,values]);result.textContent='flow='+s.join(',')+'/'+r.join(',')+'/'+after.join(',')+'; abs='+abs.join(',')+' flow-sibling='+flow.join(',')+'; fixed='+before.join(',')+'->'+fixedAfter.join(',')+'; computed='+values.join('/')});</script>

Result

flow=0,0/0,35/0,40; abs=370,10 flow-sibling=300,0; fixed=8,6->8,6; computed=static/relative/absolute/fixed

The example measures that `static` ignores an inset, `relative` shifts the rendering without moving the following sibling, `absolute` occupies no flow space in its containing block, and `fixed` keeps viewport coordinates after scrolling.

Sources