Prev Page Next Page
Top

CSS position Property

➢The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).
➢There are five different position values:
➢static
➢relative
➢fixed
➢absolute
➢sticky

position: static;

➢HTML elements are positioned static by default.
➢An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page.

EXAMPLE:

div { position: static; border: 3px solid #73AD21;}

Run


position: relative;

➢An element with position: relative; s positioned relative to its normal position.

EXAMPLE:

div {position: relative; left: 30px; border: 3px solid #73AD21;}

Run


position: 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.

EXAMPLE:

div { position: fixed; bottom: 0; right: 0; width: 300px; border: 3px solid #73AD21;}

Run


position: absolute;

➢An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

EXAMPLE:

div { position: absolute; top: 80px; right: 0; width: 200px; height: 100px; border: 3px solid #73AD21;}

Run


position: sticky;

➢An element with position: sticky; is positioned based on the user's scroll position.

EXAMPLE:

div { position: -webkit-sticky; /* Safari */ position: sticky; top: 0; background-color: green; border: 2px solid #4CAF50;}

Run


Prev Page Next Page





FEEDBACK

Rating


Message