TechnicalEasy HTML & CSS
Explain the CSS position values: static, relative, absolute, fixed and sticky.
Asked for Frontend Developer
Why they ask this: Positioning is needed for dropdowns, modals and sticky headers.
Answer
static(default): normal document flow.top/lefthave no effect.relative: stays in the flow, but can be nudged withtop/left. It also becomes the containing block for absolutely positioned children.absolute: removed from the flow and positioned relative to the nearest positioned ancestor (any value other thanstatic), or the initial containing block if there is none.fixed: removed from the flow and positioned relative to the viewport, so it stays put while scrolling. Note that atransformon an ancestor changes this.sticky: acts likerelativeuntil a scroll threshold (such astop: 0) is reached, then sticks within its parent.
css
.dropdown-wrapper { position: relative; }
.dropdown-menu { position: absolute; top: 100%; left: 0; }
.site-header { position: sticky; top: 0; }Can you explain this in your own words?