DEV Community

Ravi kumar
Ravi kumar

Posted on

3 1

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).

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top 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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay