DEV Community

Tamilselvan K
Tamilselvan K

Posted on • Edited on

Day-10 Mastering CSS Positioning: My Learning Journey

When building web layouts, understanding CSS positioning is essential. It helps you control where and how elements appear on the page. In this blog, we'll explore the five main types of positioning in CSS: static, relative, absolute, fixed, and sticky.


πŸ”Ή 1. static Position (Default)

This is the default position for all HTML elements. With static, elements follow the normal document flow.

div {
  position: static;
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Έ Note: You can't use top, left, right, or bottom with static.


πŸ”Ή 2. relative Position

relative positions the element relative to its normal position. You can shift it using top, left, right, or bottom.

div {
  position: relative;
  top: 20px;
  left: 10px;
}
Enter fullscreen mode Exit fullscreen mode

βœ… The element still takes up space in the layout, even if it moves visually.


πŸ”Ή 3. absolute Position

absolute removes the element from the normal flow and positions it relative to the nearest positioned ancestor (relative, absolute, fixed, or sticky).

div {
  position: absolute;
  top: 0;
  left: 0;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ If there's no positioned ancestor, it will be placed relative to the <html> or <body>.


πŸ”Ή 4. fixed Position

fixed positions the element relative to the viewport, so it stays in place even when the page scrolls.

div {
  position: fixed;
  bottom: 0;
  right: 0;
}
Enter fullscreen mode Exit fullscreen mode

✨ Often used for sticky navbars, chat widgets, or back-to-top buttons.


πŸ”Ή 5. sticky Position

A combination of relative and fixed. The element behaves like relative until it reaches a certain scroll position, then it sticks like fixed.

div {
  position: sticky;
  top: 0;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“ Perfect for sticky headers or table columns.


πŸ“˜ Example Summary

Position Keeps Layout Space? Scrolls with Page? Relative To
static βœ… Yes βœ… Yes Normal document flow
relative βœ… Yes βœ… Yes Its original position
absolute ❌ No ❌ No Nearest positioned parent
fixed ❌ No ❌ No Viewport
sticky βœ… Yes (initially) ⚠️ Sometimes Scroll container / viewport

🧠 Final Tips

  • Use relative to adjust an element slightly without affecting the flow.
  • Use absolute when you want full control inside a container.
  • Use fixed for UI elements that need to stay visible.
  • Use sticky to enhance user experience during scroll.

Top comments (3)

Collapse
 
hosseinyazdi profile image
Hossein Yazdi

Thanks, bro. To add on, here are some great, fun websites to practice CSS positioning:

Collapse
 
tamilselvan1812 profile image
Tamilselvan K

thanks bro for your information and i already finished all levels in flexbox froggy and grid garden ...i will try flexbox defenseπŸ’–

Collapse
 
hosseinyazdi profile image
Hossein Yazdi

Woohoo, that’s awesome, bro! Great jobβ€”well done!