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/left have no effect.
  • relative: stays in the flow, but can be nudged with top/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 than static), 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 a transform on an ancestor changes this.
  • sticky: acts like relative until a scroll threshold (such as top: 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?

Back to question bank