Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

HTML and CSS Positioning & Z-Index


HTML & CSS: Positioning & Z-Index

Positioning lets you break out of normal flow when you need overlays, tooltips, sticky headers, or UI chrome.

Learn how the position property, offsets, and z-index interact with stacking contexts.


Positioning modes

  • static - default; the element stays in normal flow.
  • relative - stays in flow but offsets visually using top/right/bottom/left.
  • absolute - removed from flow and positioned relative to the nearest ancestor with positioning.
  • fixed - pinned to the viewport; ignores scrolling.
  • sticky - behaves like relative until a scroll threshold where it sticks like fixed.

Note: Normal flow means the browser's default placement.

Absolute and fixed positioning skip that flow, so other elements act like they are not there.

If you want to read more about CSS Positioning or get an in-depth understanding, go to CSS Positioning in the CSS tutorial.


Offset properties

Combine position with offsets to place elements:

  • top / bottom control vertical displacement.
  • left / right control horizontal displacement.
  • Percentages resolve against the containing block (e.g., positioned ancestor).

Note: Offsets move the element away from its reference point.

For example, top: 10px pushes it 10 pixels down.



Sticky Header with Scroll Indicator

Sticky headers are common in UI chrome and navigation.

Sticky utility

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
  <header class="hero">
    <h1>Design Systems Handbook</h1>
    <progress value="25" max="100"></progress>
  </header>
  <main>
    <p>Scroll to see the sticky header stay in view while content flows underneath.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque habitant morbi tristique senectus et netus.</p>
  </main>
</body>
</html>
body {
  margin: 0;
  font-family: "Inter", sans-serif;
}
header.hero {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: linear-gradient(90deg, #2563eb, #7c3aed);
  color: #fff;
  padding: 20px 24px;
}
header.hero progress {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 4px;
  appearance: none;
}
header.hero progress::-webkit-progress-value {
  background: rgba(255,255,255,0.9);
}
main {
  padding: 48px 24px 96px;
  max-width: 720px;
  margin: 0 auto;
  line-height: 1.8;
}
Try it Yourself »

Managing z-index

  • Only positioned elements (plus flex/grid children) respect z-index.
  • Higher z-index wins within the same stacking context.
  • New stacking contexts form when you use position with z-index, or properties like opacity < 1, transform, or filter.

Note: Think of z-index as depth.

Bigger numbers appear on top, but only inside the same stacking context.

If you want to read more about CSS z-index or get an in-depth understanding, go to CSS z-index in the CSS tutorial.


Pattern: tooltip component

Hover tooltip

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
  <button class="button">
    Save draft
    <span class="tooltip">Shortcut: Ctrl + S</span>
  </button>
</body>
</html>
.button {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: 999px;
  background: #1d4ed8;
  color: #fff;
  cursor: pointer;
}
.button span.tooltip {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 12px);
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 10;
}
.button:hover span.tooltip {
  opacity: 1;
  transform: translate(-50%, -4px);
}
Try it Yourself »

Guidelines

  • Prefer relative + absolute positioning within components for badges or icons.
  • Keep fixed elements (like sticky navbars) minimal to avoid screen real estate issues.
  • Use position: sticky; for table headers or in-page navs that follow scrolling sections.
  • Audit stacking contexts when overlays misbehave-DevTools highlights them.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.