Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS positioning and stacking

Positioning determines a box's location and participation in normal flow. Stacking determines which overlapping box appears in front. Understanding containing blocks and stacking contexts helps you use position and z-index correctly.

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

Syntax

position: static | relative | absolute | fixed | sticky;
/* Physical inset syntax subset; anchor-positioning functions anchor() and anchor-size() are not shown. */
top: auto | <length-percentage>;
right: auto | <length-percentage>;
bottom: auto | <length-percentage>;
left: auto | <length-percentage>;
z-index: auto | <integer>;

position selects a positioning scheme. static keeps normal flow, and top, right, bottom, and left do not apply. relative shifts a box from its normal location while preserving its reserved space. absolute and fixed remove the box from normal flow: siblings do not reserve space for it.

For absolute, the reference is its containing block, often established by the nearest ancestor whose position is not static. This need not be its DOM parent. For fixed, the reference is usually the viewport; however, an ancestor with a transform other than none can establish that containing block instead.

sticky preserves its flow space and adjusts its position relative to the nearest scrollport, within its containing block's limits. To obtain sticking on an axis, set at least one inset on that axis to something other than auto, such as top: 0.

z-index controls stacking, not layout coordinates. Within one stacking context, a higher stack level appears in front of a lower one. A child context is atomic within its parent context: an enormous descendant z-index cannot escape above a neighboring context. Positioned elements with an integer z-index establish contexts; fixed and sticky do so even with auto. Thus z-index: auto and z-index: 0 are not always equivalent.

In the example, .stage establishes the containing block, but position: relative alone does not create a stacking context. Blue #front covers yellow #back because 2 exceeds 1, despite the opposite document order. The sources are working drafts; the example still requires local verification.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Positioning and stacking</title>
<style>
  body { margin: 0; }
  .stage {
    position: relative;
    width: 160px;
    height: 120px;
    background: #eee;
  }
  .box {
    position: absolute;
    width: 80px;
    height: 80px;
  }
  #front {
    top: 10px;
    left: 10px;
    z-index: 2;
    background: #0055cc;
  }
  #back {
    top: 30px;
    left: 30px;
    z-index: 1;
    background: #ffcc00;
  }
</style>
<div class="stage">
  <div class="box" id="front"></div>
  <div class="box" id="back"></div>
</div>

Result

A 160 × 120 CSS-pixel gray stage contains two 80 × 80 CSS-pixel squares. The blue square starts at (10, 10), the yellow square at (30, 30). Blue covers yellow in their overlap, although yellow occurs later in the document.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources