Learn position property in CSS

Photo by RetroSupply on Unsplash

Learn position property in CSS

ยท

2 min read

Position property in CSS sometimes takes some time & practice to understand. Here we will see a practical way of learning CSS position properties.

Values of the position property ๐Ÿ”ฅ

๐Ÿ‘‰ static: Every element has a static position by default, so the element will stick to the normal page flow. So if there is a left/right/top/bottom/z-index set then there will be no effect on that element.

๐Ÿ‘‰ relative: an elementโ€™s original position remains in the flow of the document, just like the static value. But now left/right/top/bottom/z-index will work. The positional properties โ€œnudgeโ€ the element from the original position in that direction.

๐Ÿ‘‰ absolute: the element is removed from the flow of the document and other elements will behave as if itโ€™s not even there whilst all the other positional properties will work on it.

๐Ÿ‘‰ fixed: the element is removed from the flow of the document like absolutely positioned elements. In fact, they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.

๐Ÿ‘‰ sticky (experimental): the element is treated like a relative value until the scroll location of the viewport reaches a specified threshold, at which point the element takes a fixed position where it is told to stick.

๐Ÿ‘‰ inherit: the position value doesnโ€™t cascade, so this can be used to specifically force it to, and inherit the positioning value from its parent.

Reference

css-tricks.com/almanac/properties/p/position

ย