All About Position In CSS    ๐Ÿ–ฅ

All About Position In CSS ๐Ÿ–ฅ

ยท

1 min read

The Position property:

It is used to manipulate the location of the element in the document.

Following are the possible values:

1) Static : The default position top/bottom/left/right /z-index has no effect.

     Syntax: position:static;

static_code.PNG

Output:

static_op.PNG

As we can see in the above image "I am box" div is in its default place we cannot use properties such as top/bottom/left/right /z-index in this div as the position is static(i.e default position).

2) Relative : The top/bottom/left/right /z-index will now work , otherwise the element is in the flow of the document like static.

       Syntax: position:relative;

relative.PNG

Output:

relative_op.PNG

As we can see in the above image "I am box div" is now able to shift 30px to the left from its default position.

3) Absolute : The element is removed from the flow & is relatively positioned to its first non-static ancestor , top/bottom/left/right /z-index will work.

      Syntax: position:absolute;

absolute.PNG

Output: absolute_op.PNG

4) Fixed : Just like absolute except the element is positioned relative to the browser window.

           Syntax: position:fixed;

fixed.PNG

Output:

fixed_op.PNG

5) Sticky : The element is positioned based on user's scroll position.

           Syntax: position:sticky;

sticky.PNG

Output:

sticky_op.PNG

As we can see in the above image header is stuck to its position and the text is behind it.

ย