Positioning in CSS

Positioning in CSS

Positioning in CSS: (pre-requisite for this topic is the DOM)

The top, right, bottom, and left properties are used to position the element.

  • When values to the top,right,bottom and left properties is positive we move away.
  • When values to the top,right,bottom and left properties is negative we move towards.


There are 5 different position values in CSS

1. Static

2. Relative

3. Fixed

4. Absolute

5. Sticky


1. Static: HTML elements are positioned static by default. \

  • Note :They are not affected by top bottom, left and right properties

2. Relative: An element with a relative position is positioned relative to its normal position.

  • Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
  • Elements are not moved out of the DOM

3. Fixed: An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. A fixed element does not leave a gap in the page where it would normally have been located.

4. Absolute: An element with position: absolute is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors (will check if any of the ancestors have relative positioning, and takes the nearest parent with position relative into account), it uses the document body, and moves along with page scrolling.

  • Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

5. Sticky: An element with position: sticky; is positioned based on the user's scroll position. Depending on the scroll position, a sticky element toggles between relative and fixed.

  • Note: It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position: fixed).

To view or add a comment, sign in

More articles by Jeevan Henry Dsouza

  • Mystery Behind Node js

    What is Node JS ? Computer only understands 1 & 0 or in other words Machine Code. Every programming language has its…

  • Stacking Contexts In CSS

    Stacking Context : It is a group of elements positioned on the Z-axis. Stacking Contexts can be nested within other…

  • Flexbox

    FlexBox: as the name suggests it is a flexible box. It makes positioning much easier than normal methods.

  • Git Hub Markdown Cheat Sheet

    <!-- What is markdown --> <!-- Markdown is a way to style text on the web. We control the display of the…

  • Selectors in CSS

    Selectors in CSS The purpose of HTML is to structure the data into different sections on the webpage. Example word…

Others also viewed

Explore content categories