DEV Community

Ravi kumar
Ravi kumar

Posted on

The CSS position property

The position property specifies the type of positioning method used for an element and by position property we can manipulate the location of an element.
These are type of position value :

  • static
  • relative
  • absolute
  • fixed
  • sticky

And also global values like inherit , initial, revert, unset can also be applied to position property

Let's talk about each position values one by one :)

static

position: static;
static in the default position and it's position according to the normal flow of the page. In static top/bottom/left/right/z-index property has no effect.

relative

position : relative;
It behaves like a static but when we apply on of these property top/bottom/left/right/z-index then element will flow according to these CSS properties.

absolute

position : absolute;
The element is removed from the normal document flow and is relatively positioned to its first non-static ancestor. Its final position is confirm by the values of top/bottom/left/right.

fixed

position : fixed;
The element is removed from the normal document flow like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the viewport, not any particular parent, and are unaffected by scrolling.

sticky

position : sticky;
The element is positioned based on the user's scroll position. A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

Latest comments (2)

Collapse
 
hrrsppzgl profile image
hrrsppzgl

Nice article clearly explaining the css position property!

I would probably change the following

fixed positioned elements are always relative to the document

to

fixed positioned elements are always relative to the viewport

Collapse
 
ravikumar1002 profile image
Ravi kumar

This make sense, will update.
thanks