Position & z-index in CSS

From the CSS cheat sheet ยท Positioning ยท verified Jul 2026

Position & z-index

Static, relative, absolute, fixed, and sticky positioning

css
/* Relative (offset from normal position) */
.badge { position: relative; top: -5px; left: 10px; }

/* Absolute (relative to nearest positioned ancestor) */
.tooltip { position: absolute; top: 100%; left: 0; }

/* Fixed (relative to viewport) */
.navbar { position: fixed; top: 0; width: 100%; z-index: 50; }

/* Sticky (scrolls then sticks) */
.sidebar { position: sticky; top: 20px; }
๐Ÿ’ก Absolute positioning needs a positioned ancestor (relative parent) โ€” otherwise it uses the viewport
โšก position: sticky is perfect for sidebars, table headers, and section headings
๐Ÿ“Œ z-index only works on positioned elements โ€” static elements ignore it
๐ŸŸข inset: 0 is shorthand for top: 0; right: 0; bottom: 0; left: 0
positionz-indexstickyabsolute

More CSS tasks

Back to the full CSS cheat sheet