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
Loading Editor...
Live Preview

πŸ“ position: relative

Moves element relative to its normal position without affecting siblings.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

πŸ“ position: absolute

Removes element from flow and positions it relative to nearest positioned ancestor.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

πŸ“ position: fixed

Element is fixed to viewport and stays in place during scroll.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

πŸ“ position: sticky

Element toggles between relative and fixed based on scroll position.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

πŸ’‘ 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 Questions

Progress: 0 / 5
Keep Going!CSS - Overflow