Skip to article content
FrontendXLesson 20 / 30

Positioning and stacking contexts

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

advanced · about 40 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Choose between static, relative, absolute, fixed and sticky positioning.
  • Explain why a high z-index sometimes has no effect.

Overview

#

The position property removes an element from normal flow or offsets it. relative offsets the element from its normal position while keeping its space. absolute takes it out of flow and positions it against the nearest ancestor that is positioned. fixed positions against the viewport. sticky toggles between relative and fixed as the user scrolls past a threshold.

snippet.css
.card { position: relative; }
.badge {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
}

An absolutely positioned element needs a positioned ancestor, or it falls back to the initial containing block, which is why a badge sometimes lands in the wrong corner. Set position: relative on the intended parent.

z-index applies to positioned elements and also to flex and grid items, which take part in stacking even when position: static. It is scoped to a stacking context. A stacking context is created by positioned elements with a z-index other than auto, by opacity less than 1, by transform, filter, will-change and several other properties. Once a stacking context exists, its children cannot escape it: a child with z-index: 9999 still sits below a sibling of the parent that has a higher stacking order. This is the usual cause of a modal appearing behind a header.

snippet.css
.modal-root { position: fixed; inset: 0; z-index: 1000; }

Sticky positioning is common for headers and table columns. It needs a scroll container and a threshold such as top: 0. A sticky element sticks only within its parent, so a sticky header inside a short wrapper stops sticking as soon as the wrapper scrolls away. Prefer inset shorthand for offsets and reserve z-index for a small, documented set of layers.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This badge is meant to sit in the top-right of the card but it is positioned against the page. Fix it.

    starter.css
    .card { padding: 1rem; }
    .badge { position: absolute; top: 0.5rem; right: 0.5rem; }

    Reveal the reference answer
    reference.css
    .card { position: relative; padding: 1rem; }
    .badge { position: absolute; top: 0.5rem; right: 0.5rem; }

    Authored expected output: The badge is positioned against the card because the card is now a positioned ancestor.

    Absolute positioning resolves against the nearest positioned ancestor; relative on the card establishes it.

  2. Exercise 2Explain

    A child has z-index: 9999 but still renders below a sibling section. Explain the most likely cause.

    Reveal the reference answer
    reference.text
    The child is inside a stacking context created by an ancestor (for example transform, opacity below 1, or a positioned ancestor with z-index). Its z-index is scoped to that context, so it cannot rise above a sibling of the context that has a higher stacking order.

    Authored expected output: An explanation naming stacking context and scoped z-index.

    z-index competes only within its own stacking context, not across the whole page.

Common misconceptions

#
  • z-index works on any element, including a static block or inline element.
  • A large z-index always wins, even across stacking contexts.

Follow-up practice

#
  • html-css-21-logical-properties

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.