Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

CSS Position: Relative, Absolute, Fixed and Sticky Explained

A product badge appears in the corner of the page, a fixed button scrolls with a panel, or a sticky heading never sticks. These bugs often come from confusing three separate mechanisms: normal flow, the containing block that supplies coordinates, and stacking contexts. CSS positioning complements Grid and Flexbox rather than replacing them.

Published Written by Reading time 8 min read
On this page Contents: quick navigation between sections

Static and relative: keeping space in normal flow

<div class="flow-demo">
  <div class="item">A</div>
  <div class="item item--shifted">B</div>
  <div class="item">C</div>
</div>
.flow-demo {
  display: grid;
  gap: 12px;
}

.item {
  min-block-size: 40px;
  background: #dbeafe;
}

.item--shifted {
  position: relative;
  inset-inline-start: 16px;
  inset-block-start: 8px;
}

The default position: static leaves a box in normal flow; physical inset properties do not move it. Relative positioning preserves the box's original layout space while allowing a visual offset. [1][2]

In the example, B shifts 16px toward inline-end and 8px downward in horizontal LTR writing; C stays where it would have been without the shift. Relative offsets can cause overlaps, so use gaps, margins or padding for ordinary spacing. A relative box without offsets is useful as a local positioning reference for descendants.

Absolute positioning: containing blocks and badges

<article class="product-card">
  <span class="product-badge">New</span>
  <h2>Headphones</h2>
  <p>Wireless audio</p>
</article>
.product-card {
  position: relative;
  box-sizing: border-box;
  max-inline-size: 320px;
  padding: 48px 16px 16px;
  border: 2px solid #94a3b8;
  border-radius: 12px;
}

.product-badge {
  position: absolute;
  inset-block-start: 12px;
  inset-inline-end: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  background: #dbeafe;
}

An absolutely positioned box is out of normal flow, so siblings reserve no space for it. An ordinary positioned block ancestor provides its padding-box edges as the containing block. This need not be the direct parent; properties such as transform can establish a containing block too. Without a suitable ancestor, the initial containing block is used. [1][3][4]

The product card reserves room above its title for a badge of known size. For long or variable labels, prefer normal flow and Grid because an absolute badge cannot grow the card. Logical inset-inline-end and inset-block-start follow writing direction; in horizontal RTL the badge moves to the left. [6]

Stretching a positioned box with inset

<figure class="preview">
  <img src="/images/product.webp" alt="Wireless headphones" />
  <figcaption class="preview-caption">Studio edition</figcaption>
</figure>
.preview {
  position: relative;
  margin: 0;
  max-inline-size: 480px;
  overflow: hidden;
  border-radius: 12px;
}

.preview img {
  display: block;
  inline-size: 100%;
  block-size: auto;
}

.preview-caption {
  position: absolute;
  inset-inline: 0;
  inset-block-end: 0;
  padding: 12px;
  background: #172033;
  color: #fff;
}

Opposite insets on one axis can stretch a typical absolutely positioned box with an automatic size across the available space. inset: 0 sets all four physical insets to zero, whereas inset-inline: 0 targets the inline axis only. Explicit sizes can prevent stretching. [1][6]

The caption spans the image width; the image remains in flow and determines the figure height. Replace the example image path with your own asset. This overlay suits short captions; longer text may obscure the image, so place it below instead. Add actual intrinsic image dimensions in HTML to reserve space before the image loads.

Fixed positioning: when the viewport stops being the reference

<div class="workspace">
  <p>Workspace</p>
  <a class="help-link" href="/en/help">Help</a>
</div>
.workspace {
  margin: 40px;
  inline-size: 300px;
  block-size: 200px;
  background: #f1f5f9;
}

.help-link {
  position: fixed;
  inset-inline-end: 16px;
  inset-block-end: 16px;
  padding: 8px 12px;
  background: #dbeafe;
}

.workspace--transformed {
  transform: translateZ(0);
}

In a browser, fixed positioning normally uses the viewport and takes the element out of flow. Scrolling the document then does not change its distance from the viewport edges. An ancestor with a non-none transform, or certain other properties such as contain: paint, can establish a fixed-position containing block instead. [1][3][4]

Add workspace--transformed to .workspace: the help link now uses the panel as its reference. Even a transform with no visible displacement changes this behavior. Place global overlays outside such ancestors; for an interactive modal consider a native dialog opened with showModal() rather than recreating focus management manually.

Sticky positioning: a scrolling panel heading

<section class="activity" aria-labelledby="activity-title" tabindex="0">
  <h2 id="activity-title" class="activity-heading">Activity</h2>
  <ul class="activity-list">
    <li>Account created</li>
    <li>Profile updated</li>
    <li>Invitation accepted</li>
    <li>Project created</li>
    <li>Report exported</li>
    <li>Settings saved</li>
  </ul>
</section>
.activity {
  max-block-size: 180px;
  overflow: auto;
  border: 1px solid #94a3b8;
}

.activity-heading {
  position: sticky;
  inset-block-start: 0;
  z-index: 1;
  margin: 0;
  padding: 12px;
  background: #fff;
  color: #172033;
}

.activity-list {
  margin-block: 0;
  padding-inline: 32px 12px;
}

.activity-list li {
  padding-block: 12px;
}

Sticky boxes remain in flow. When an inset threshold such as inset-block-start: 0 is reached, the browser adjusts the box against the nearest scrollport while constraining it within its containing block. An axis with only automatic insets has no sticky offset on that axis. [1][2]

The activity panel has a limited height and overflow: auto, so its list scrolls locally. The heading sticks to the panel's top edge, not the window. Its background prevents the list from showing through. A named section with tabindex="0" can be focused and scrolled with a keyboard; retain the focus indicator.

Why position: sticky does not work

<div class="article-frame">
  <div class="article-layout">
    <aside class="article-sidebar">
      <nav aria-label="On this page">
        <a href="#details">Details</a>
      </nav>
    </aside>
    <article id="details" class="article-content">
      <h2>Details</h2>
      <p>Long article content</p>
    </article>
  </div>
</div>
.article-frame {
  overflow: clip;
}

.article-layout {
  display: grid;
  gap: 24px;
}

.article-content {
  min-block-size: 120vh;
}

@media (min-width: 48rem) {
  .article-layout {
    grid-template-columns: 180px minmax(0, 1fr);
  }

  .article-sidebar {
    position: sticky;
    inset-block-start: 16px;
    align-self: start;
  }
}

Inspect ancestor overflow first: hidden, auto or scroll can create the scroll reference for sticky even when the ancestor is not actually scrolling. The two overflow axes interact: overflow-x: hidden with default overflow-y: visible computes vertical overflow to auto. overflow: clip does not create a scroll container and also disallows programmatic scrolling; use it only when clipping is intended. [2][5]

The example sidebar sticks during document scrolling on wide screens. align-self: start prevents Grid from stretching it to the row height. A box as tall as its containing block has no room to move; very tall menus may need normal flow or an intentionally scrollable panel. Switching clip to hidden demonstrates the ancestor problem. On narrow screens the menu stays static.

Z-index and stacking contexts

<div class="layer-demo">
  <div class="card-layer">
    <span class="card-label">Card label</span>
  </div>
  <div class="page-layer">Page notice</div>
</div>
.layer-demo {
  position: relative;
  isolation: isolate;
  block-size: 180px;
}

.card-layer {
  position: absolute;
  inset: 0;
  z-index: 1;
}

.card-label {
  position: absolute;
  inset-block-start: 32px;
  inset-inline-start: 32px;
  z-index: 9999;
  padding: 12px;
  background: #dbeafe;
}

.page-layer {
  position: absolute;
  inset-block-start: 40px;
  inset-inline-start: 48px;
  z-index: 2;
  padding: 12px;
  background: #fde68a;
}

z-index is not a global ranking. Relative and absolute boxes with a non-auto z-index create stacking contexts; fixed and sticky boxes do so even with auto. Other triggers include transforms, opacity below 1 and isolation: isolate. [7]

The label with z-index: 9999 stays below the notice with z-index: 2 because the label's parent is at level 1. Changing the parent to 3 moves its entire context above the notice. Inspect the context hierarchy rather than increasing numbers indefinitely; component systems benefit from a small set of named layer levels.

<header class="site-header">
  <a href="#main">Skip to content</a>
  <a href="#contact">Contact</a>
</header>
<main id="main" tabindex="-1">
  <h1>Documentation</h1>
  <section class="documentation">
    <h2>Overview</h2>
    <p>Documentation content</p>
  </section>
  <section id="contact">
    <h2>Contact</h2>
    <a href="mailto:help@example.com">Email support</a>
  </section>
</main>
:root {
  --header-height: 56px;
}

html {
  scroll-padding-block-start: calc(var(--header-height) + 12px);
}

.site-header {
  position: sticky;
  inset-block-start: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  box-sizing: border-box;
  block-size: var(--header-height);
  padding-inline: 16px;
  background: #fff;
  color: #172033;
}

.documentation {
  min-block-size: 120vh;
}

@media (max-width: 40rem), (max-height: 30rem) {
  html {
    scroll-padding-block-start: 0;
  }

  .site-header {
    position: static;
    flex-wrap: wrap;
    block-size: auto;
    padding-block: 12px;
  }
}

A sticky header can cover an anchor destination or keyboard-focused control. WCAG 2.2 criterion 2.4.11 requires that an item receiving keyboard focus is not entirely hidden by author-created content. scroll-padding-block-start on the actual scrolling container helps reserve space when scrolling to a target. [8][9]

The example shares a CSS variable between header height and scroll padding. On narrow or short viewports the header returns to normal flow to preserve reading space. For real headers with variable heights, adapt to the actual height or avoid sticky positioning. Test Tab, Shift+Tab, anchors and zoom; scroll padding alone does not guarantee WCAG conformance.

Positioning becomes predictable when flow, containing blocks and stacking contexts are treated separately. Use Grid and Flexbox for layout, absolute for local overlays, fixed for global elements independent of scrolling, and sticky for headings associated with a scroll area. Debug ancestor relationships and actual dimensions instead of adding arbitrary offsets and ever-larger z-index values.

CSS CSS Position Relative Positioning Absolute Positioning Fixed Positioning Sticky Positioning Containing Block z-index Stacking Context Accessibility

Frequently asked questions

What is the difference between relative and absolute?

Relative preserves layout space; absolute removes the box from flow and uses a containing block. [1]

Does absolute positioning require a relative parent?

No. A suitable containing block may be established by a more distant ancestor or a transform. A relative parent is a common choice. [3]

Why does sticky fail with overflow: hidden?

The hidden-overflow ancestor can become sticky's scroll reference even when the page scrolls elsewhere. [2][5]

Is fixed always relative to the viewport?

No. A non-none transform on an ancestor can establish a different containing block, for example. [3][4]

Why does z-index: 9999 not work?

A descendant cannot independently escape its stacking context. Inspect the layers of its ancestors as well. [7]

Sources and references

  1. W3C, CSS Positioned Layout Module Level 3↩1↩2↩3↩4↩5↩6↩7↩8
  2. MDN, position↩1↩2↩3↩4↩5
  3. MDN, Layout and the containing block↩1↩2↩3↩4↩5
  4. MDN, transform↩1↩2↩3↩4
  5. MDN, overflow↩1↩2
  6. MDN, Logical properties for floating and positioning↩1↩2
  7. MDN, Stacking context↩1↩2↩3
  8. W3C WAI, Understanding Focus Not Obscured (Minimum)↩
  9. MDN, scroll-padding↩

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning