CSS Positioning with Examples

ยท

2 min read

The Position property of CSS is used for positioning/ locating the element inside the document. These properties are:

  1. Static
  2. Relative
  3. Absolute
  4. Fixed
  5. Sticky

1. Static Positioning

This is present by default in every HTML element. It is unaffected if properties like a top, left, right, and the bottom is applied. It is always positioned with the normal flow of the HTML page.

Example:

Static.JPG

2. Relative Positioning

This property is used to set the element relative to its normal position. It gets affected when properties like a top, left, right, and bottom is applied.

image.png

3. Absolute Positioning

This property is used to position elements relative to the first parent with a non-static position. If no such element is found, the contained block is HTML.

image.png

4. Fixed Positioning

Fixed position elements are similar to absolutely positioned elements. They are also removed from the normal flow of the document. But unlike absolutely positioned element, they are always positioned relative to the element.

One thing to note is that fixed elements are not affected by scrolling. They always stay in the same position on the screen.

image.png

5. Sticky Positioning

The element is positioned based on the user's scroll position. Position: sticky is a mix of position: relative and position: fixed. It acts like a relatively positioned element until a certain scroll point and then it acts like a fixed element.

image.png

ย