position controls how an element is placed. There are five values:
staticis the default. The element sits in normal flow, andtop,leftandz-indexhave no effect.relativestays in flow, but you can shift it visually from its normal spot. Its original space stays reserved. It also becomes the containing block for absolutely positioned descendants.absoluteis removed from flow and placed relative to its containing block.fixedis removed from flow and placed relative to the viewport, the visible browser window. The exception: if an ancestor has atransform,filteror similar property, that ancestor becomes its containing block.stickyacts likerelativeuntil scrolling reaches the threshold you set withtopor a similar property. Then it sticks inside its nearest scrolling ancestor.
A sticky element only sticks while it stays inside its containing block, usually its parent. When the parent scrolls out of view, the sticky element leaves with it.
The containing block is the box that an element is positioned and sized against. Think of it as the frame that top: 0; left: 0 measure from. For an absolute element, it is the padding box of the nearest ancestor whose position is not static. The padding box is the area inside the border, padding included. If there is no such ancestor, it is the initial containing block, a box the size of the viewport at the top of the page.
relativestays in flow, shifts visually, and becomes the anchor for absolute children.absoluteleaves the flow and is placed against the nearest positioned ancestor.fixedis placed against the viewport, unless an ancestor hastransformorfilter.stickyacts likerelativeuntil a threshold. It needstoporbottomand a scroll container.- A sticky element never leaves its containing block, usually its parent.
Read more
- position (MDN) (https://developer.mozilla.org/en-US/docs/Web/CSS/position)developer.mozilla.org
- Containing block (MDN) (https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block)developer.mozilla.org