CSS Tutorial

Positioning

Static, Relative, Absolute, Fixed, and Sticky

Controlling the Flow

By default, HTML elements follow the "Normal Flow" (appearing one after the other). The position property allows you to take an element out of that flow and place it exactly where you want.

1. relative

The element stays in its normal spot, but you can move it slightly using top, bottom, left, or right without affecting the elements around it.
css
.box {
  position: relative;
  top: 10px;
}

2. absolute

The element is removed from the normal flow. It is positioned relative to its nearest positioned ancestor (the closest parent that has a position other than static).
Wait! If you want to place a badge inside a card, you give the card position: relative; and the badge position: absolute;.

3. fixed

The element is positioned relative to the viewport (the screen). It stays in the exact same spot even when you scroll. This is perfect for navigation bars.

4. sticky

Wait! This is a modern favorite. The element behaves like relative until the user scrolls past it, at which point it "sticks" to the top of the screen like fixed.
css
header {
  position: sticky;
  top: 0;
}

Z-Index (Layers)

When elements overlap, the z-index property determines which one is on top. A higher number means the element is "closer" to the user.

Summary

TypeRelative to...Description
staticNormal FlowThe default
relativeItselfMoves without affecting others
absoluteParentTargeted placement
fixedScreenStays while scrolling
stickyParent/Screen"Relative" then "Fixed"
Best ForOverlays, Navbars, Sidebars, and specialized UI components
Logic"Coordinate-based placement"
SafetyAbsolute positioning can break responsive layouts if used too much
Modernsticky is now supported in all major browsers
StandardAlways check z-index if an element is "disappearing"
IdentityThe "Third Dimension" of web design (Depth)