DEV Community

Cover image for CSS Positioning in under 4 minutes
ARAVIND S
ARAVIND S

Posted on

CSS Positioning in under 4 minutes

If you have just started learning CSS or have yet to work much in CSS, you will find difficulty in positioning the element. To make your life easier, there are CSS positioning properties like Absolute, Relative, Static, Fixed, Sticky, and Inherit. In this post, we will learn the six CSS positioning properties and where to use them with some examples. If you need a video version of this post, refer to my YouTube video.

Youtube Video Link - https://youtu.be/7V0bbUKE3KQ?si=VaODQNs_rJJFlHli

1. Static

By default, every HTML element has a static position. This means that elements stick to the normal page flow, and their placement is determined by the document's structure. The left, right, top, bottom, and z-index properties do not affect elements with a static position. Essentially, these elements behave as if the positional properties don't exist.

2. Relative

An element with a relative position retains its original place in the document flow, just like a static element. However, unlike static positioning, the left, right, top, bottom, and z-index properties will have an effect. These properties "nudge" the element from its original position in the specified direction.

3. Absolute

An element with an absolute position is removed from the normal document flow. Other elements will behave as if the absolutely positioned element is not there. This allows for precise placement using the left, right, top, bottom, and z-index properties. The element is positioned relative to its nearest positioned ancestor (i.e., an ancestor element with a position other than static), or the initial containing block if no such ancestor exists.

4. Fixed

An element with a fixed position is also removed from the document flow, similar to an absolutely positioned element. However, unlike absolute positioning, fixed positioning is relative to the viewport, not to any particular parent element. This means the element remains in the same place even when the page is scrolled, making it useful for creating persistent navigation bars or sticky headers.

5. Sticky

The sticky positioning value combines aspects of relative and fixed positioning. An element with sticky positioning is treated like a relatively positioned element until the scroll location of the viewport reaches a specified threshold. At this point, it becomes fixed and stays in that position relative to the viewport until it is scrolled out of view.

6. Inherit

The inherit value for the position property allows an element to inherit its position value from its parent. This can be useful when you want an element to follow the same positioning rules as its parent, especially in complex layouts where consistency is key.

Conclusion
Understanding CSS positioning is essential for crafting flexible and dynamic web layouts. By knowing how static, relative, absolute, fixed, sticky, and inherit work, you can better control the placement and behavior of elements on your web pages. Experiment with these positioning values to achieve the desired layout and functionality for your designs.

Top comments (0)