CSS Properties

Position Properties

These properties are used to position an element. Without setting up position property we can not use any of these properties like - left, right, top, bottom.
  • static
  • fixed
  • relative
  • absolute
Static

When we set static position for an element then it is not positioned in any special way, it is always positioned according to the normal flow of the page
left, right, top and bottom property won't work with static position.

Example:

h1{
position : static;
border : 3px solid red;
}


Fixed

When we set 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.

Example:

img{
position : fixed;
right : 0;
top : 50%;
}


Relative

When we set relative position for an element,elements positioned to its normal flow.

Example:

h1{
position : relative;
left : 50px;
}


Absolute

An element with position absolute is positioned relative to the nearest positioned ancestor. If there is no positioned ancestor then it follow the normal position according to browser.

Example:

div{
position : relative;
}
div{
position : absolute;
top : 5px;
right : 0;
width : 200px;
}
Cristle Academy

Compiler