Positions in CSS

Positions in CSS

Position and it's type with Example.

welgif.gif

Position

The position CSS property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements. The position property specifies the type of positioning method used for an element.

There are five different position values:

  • static
  • relative
  • fixed
  • absolute
  • sticky

Let's Learn about this position in details

Static

The element is positioned according to the normal flow of the document. The top, right, bottom, left, and z-index properties have no effect. This is the default value. An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page

Syntax:
position: static;

Example:

Relative

An element with position: relative; 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. The positional properties “nudge” the element from the original position in that direction.

Syntax:
position: relative;
left/right/bottom/top: 10px;

samllblog.gif It's a Small blog

Example:

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. The top, right, bottom, and left properties are used to position the element.

A fixed element does not leave a gap in the page where it would normally have been located.

Syntax:
position: fixed;
left/right/bottom/top: 10px;

Example:

See the Result at 0.25 , the box which is not moving is an example of Fixed

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, 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. the element is removed from the flow of the document and other elements will behave as if it’s not even there whilst all the other positional properties will work on it.

Syntax:
position: absolute;
left/right/bottom/top: 10px;

Example:

Sticky

An element with position: sticky; 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). the element is treated like a relative value until the scroll location of the viewport reaches a specified threshold, at which point the element takes a fixed position where it is told to stick.

Syntax:
position: sticky;
left/right/bottom/top: 10px;

Example:

The box which is sticking at top is an example of sticky position

Reference of the Blog:

dchoqoo-c9cb91ac-14cc-49da-9ac1-5786676e8893.gif

next-week-ill-see-you-next-week.gif