Nearby lessons
16 of 19π CSS Position Property
π What is CSS Position Property?
The position property in CSS specifies how an element is positioned in a document.
It plays a crucial role in controlling layout, layering, and behavior of elements.
- static β Default position (normal document flow)
- relative β Offset from original position
- absolute β Positioned relative to nearest positioned ancestor
- fixed β Fixed to viewport, unaffected by scroll
- sticky β Switches between relative and fixed on scroll
π position: static
This is the default position. Elements appear in normal document flow.
Code Example
PREVIEW READY
π position: relative
Moves element relative to its normal position without affecting siblings.
Code Example
PREVIEW READY
π position: absolute
Removes element from flow and positions it relative to nearest positioned ancestor.
Code Example
PREVIEW READY
π position: fixed
Element is fixed to viewport and stays in place during scroll.
Code Example
PREVIEW READY
π position: sticky
Element toggles between relative and fixed based on scroll position.
Code Example
PREVIEW READY
π‘ Positioning Best Practices
- β Always define a positioned ancestor when using absolute.
- β Use fixed positioning for sticky buttons or headers.
- β Combine z-index carefully with position to manage layers.
- π« Avoid overusing absolute or fixed β it can break layouts.
- π± Test sticky and fixed behavior on mobile devices.
π§ Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!CSS - Overflow