CSS: CSS Positioning

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Dana🏷 Category: CSS Tutorial

CSS positioning is a powerful layout tool that lets you precisely control element placement on the page.

Position Property Quick Reference

Value Behavior Reference Point Common Use
static Normal flow (default) — Default behavior
relative Offset from normal position Self's original position Fine-tuning, positioning context
absolute Removed from flow Nearest positioned ancestor Badges, overlays
fixed Fixed in viewport Viewport Sticky headers, back-to-top
sticky Hybrid Scroll container + viewport Sticky headers

Properties in this section: top bottom left right z-index transform filter opacity

1. position: relative

Offsets element from its normal position, without removing it from document flow:

▶ Example

▶ Try it Yourself

Common uses:


2. position: absolute

Removes element from flow, positioned relative to nearest positioned ancestor:

▶ Example

▶ Try it Yourself

Key point: If no positioned ancestor exists, absolute positions relative to the viewport.


3. position: fixed

Fixed in viewport, doesn't scroll with page:

▶ Example

▶ Try it Yourself

Common uses:


4. position: sticky

Hybrid of relative and fixed — scrolls normally then "sticks" at a threshold:

▶ Example

▶ Try it Yourself

Usage notes:


5. z-index (Stacking Order)

When elements overlap, z-index controls which appears on top:

▶ Example

▶ Try it Yourself

Note: z-index only works on positioned elements (non-static).

(1) Stacking Context

z-index is compared within the same "stacking context." Each positioned element creates a stacking context — children's z-index only compares within the parent's context.


Properties in this section: top bottom left right z-index transform filter opacity

❓ FAQ

Q What's the difference between absolute and relative, and when do I use each?
A relative stays in the document flow — the original spot is kept after offsetting; absolute leaves the flow, positioning against the nearest non-static ancestor, and other elements fill its old spot. Nudge an icon with relative; build popovers and badges with absolute — and the absolute element's parent almost always needs a relative as reference.
Q My absolute child keeps positioning against the page's top-left — how do I fix it?
A The parent lacks position: relative, so absolute walked up to the body as reference. Fix: add position: relative to the parent (no offset values needed). To debug: select the element in DevTools and check the Computed panel for what its containing block actually is.
Q How do I make a sticky header that pins on scroll — and why doesn't sticky work?
A One line: position: sticky; top: 0;. The most common failure: an ancestor has overflow: hidden/auto/scroll, locking the sticky element inside that scroll container. Inspect ancestor overflow layer by layer, or move the sticky element outside the scrolling container. And don't omit top — without it nothing sticks.
Q What's the difference between fixed and absolute?
A absolute positions against the nearest non-static ancestor and scrolls with the page; fixed positions against the viewport and never moves while scrolling. Back-to-top buttons and floating chat widgets use fixed; card badges use absolute. Watch out: fixed elements inside ancestors with transform or filter "degrade" to positioning against that ancestor — a common cause of broken fixed behavior.
Q z-index is set high but still blocked?
A z-index only works within the same stacking context. If a parent has z-index, children's z-index only compares within that parent's context — it won't cross parent boundaries. Check if a parent has lower z-index, or if parent has properties like opacity or transform that create new stacking contexts.
Q How do I center an absolutely positioned element horizontally and vertically?
A The most robust: top: 50%; left: 50%; transform: translate(-50%, -50%); — no need to know the element's own size, works for any content. Horizontal-only centering can use a fixed width with left: 0; right: 0; margin: auto, but vertical is easiest with transform. Modals and loading overlays all use this pattern.
Q What are the values of position, and what's each one for?
A Five common values: static (default, not positionable), relative (nudging + reference point), absolute (out-of-flow, popovers/badges), fixed (viewport-relative, floating buttons), sticky (scroll snapping, sticky table headers). Mnemonic: relative stays in flow, absolute leaves flow to find its ancestor, fixed finds the viewport, sticky finds the scroll container. z-index on static does nothing.
Q fixed positioning doesn't work well on mobile?
A Fixed behaves inconsistently in mobile Safari, especially with keyboard popups or page scrolling. Use position: sticky as an alternative for some scenarios. On iOS, fixed elements drift when the soft keyboard opens — for bottom input bars prefer sticky, or reposition after the keyboard closes.
Q An element is covered by another layer — how do I quickly find whose z-index to change?
A DevTools Layers/3D view shows the stacking structure directly. Or walk the covering element's ancestor chain in the Elements panel — every node that creates a stacking context (z-index, opacity<1, transform, etc.) is a boundary. Most battles are between two parent chains; rather than inflating z-index endlessly, adjust the hierarchy of the parent contexts.

📖 Summary

📝 Exercises

  1. Create a "product card" with a "HOT" badge in the top-right corner using position: absolute.
  2. Create a fixed bottom navigation bar or copyright notice using position: fixed.
  3. Create a "sticky heading" — section titles that stick to the top when scrolling until the next title pushes it away.
  4. Implement absolute centering of a child element (using absolute + top/left + transform).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏