Static is the default positioning for all HTML elements. Elements with static positioning follow the normal document flow and cannot be moved using top, right, bottom, or left properties.
Relative positioning moves an element relative to its original position in the document flow. The element still takes up space in the original location, but appears moved.
Absolute positioning removes an element from the normal document flow and positions it relative to its nearest positioned ancestor (or the viewport if none exists).
Container with position: relative
This container creates a positioning context for the absolutely positioned element.
Fixed positioning removes an element from the document flow and positions it relative to the viewport. The element stays in the same position even when scrolling.
Look at the top-right corner of your screen! The "Fixed Position" box stays there even when you scroll.
Fixed elements are commonly used for:
Sticky positioning is a hybrid of relative and fixed positioning. The element is positioned relative until it crosses a specified threshold, then it becomes fixed.
Scroll down to see the sticky behavior in action. This sticky box will stick to the top of the viewport when you scroll past it.
Sticky positioning is perfect for:
Z-index controls the stacking order of positioned elements. Higher z-index values appear on top of lower values.
Note: Z-index only works on positioned elements (relative, absolute, fixed, sticky).
This card uses absolute positioning for the "NEW" badge.
Notice how the badge stays in the top-right corner.
Notice how the navigation bar stays at the top
The floating button stays in the bottom-right corner
You've seen all CSS positioning types in action!