28
loading...
This website collects cookies to deliver better user experience
position
property that specifies how an element should appear in the document. This property is helpful when you want to position elements in the DOM outside of normal flow.position
property with the top
, right
, bottom
, left
, and z-index
properties that determine the final position of an element.Check out the demos I explained in this post here
position
property specifies how an element is placed in the DOM.top
, right
, bottom
, and left
properties that specify where the element is placed.The position
property can take five values:
Before we jump in, let's understand in short what top
, right
, bottom
, left
, and z-index
are.
top
, right
, bottom
, and left
, properties specify how far away from the top/ right/ bottom/ left and element should be positioned.
z-index
is used to specify the stack level, and layer elements.
z-index
is 0, and will not work unless you apply position on the element (except static).z-index
value overlaps elements with smaller z-index
value.position: static;
is the default value of this property, and the elements appear in their normal flow in the document.top
, right
, bottom
, left
, and z-index
have no effect on an element positioned static
.div
with class static
has a static position.
The code for the above example:
div.static {
position: static; /* Need not specify as this is default */
}
position: relative;
is applied to an element, it appears in the normal flow of the document.But unlike static position, it accepts accepts values for top
, right
, bottom
, left
, and z-index
.
top
, right
, bottom
, left
, and stacked in layers depending on the value of z-index
.In the example shown below, the blue colored div
element with class relative
has a relative position, with top and left offset values.
The code for the above example:
div.relative {
position: relative;
top: 5em;
left: 25%;
}
position: absolute;
is applied to an element, it is taken out of the the normal flow of the document.top
, right
, bottom
, left
values are specified.z-index
can also be specified to stack and layer elements.In the example below, the blue colored div
with class absolute
is given a relative position, and no values of top
, right
, bottom
, and left
are specified.
See what happens when this blue box is given z-index: -1;
div.absolute
is the section with class section
and background-color: lightskyblue;
has z-index: 0;
. of
section.section is greater than that of
div.absolute, the
div` is stacked below it's parent.position: absolute;
is placed relative to it's closest positioned ancestor (other than static).If no such ancestor is found, it is simply placed relative to the document.
The code for the above example:
div.absolute-two {
position: absolute;
top: 0;
bottom: 0;
left: 2em;
}
Check out what happens, if we give the parent (or an ancestor) of an element with absolute position: position: relative;
.
div
with a class absolute-three
, and it's lightskyblue colored parent section
with class section
.div.absolute-three
is positioned relatively within the parent and not the document.The code for the above example is:
section.section {
position: relative;
}
div.absolute-three{
position: absolute;
bottom: 0;
right: 25%;
height: 100px;
}
position: fixed;
is applied to an element, it is taken out of the the normal flow of the document similar to Absolute Position.
position: fixed;
element is not given space in the layout.top
, right
, bottom
, and left
values.transform
, perspective
, or filter
property applied with a value other than none.
position: sticky;
is applied to an element, it is positioned in the the normal flow of the document similar to Relative Position.top
, right
, bottom
, and left
properties are crossed.sticky
. section
with class section
, and has a lightskyblue background
.div.sticky
is 5px
away from the top, it becomes a fixed element. Before that it remains as relatively positioned element.section.section
scrolls away from the layout, so does the div with sticky position of. It no longer remains fixed, and now becomes relative again.
Hence the sticky (in particular fixed) behavior of an element remains only within it's closest positioned parent.
The code for the above example is:
section.section{
position: relative;
}
div.sticky {
position: sticky;
top: 5px;
}
position
property is very important, and comes in handy in various scenarios.Check out the demos I explained in this post here
relative
and absolute
positions are often used, and mostly together when you have pseudo elements.
absolute
position within relative
position is common, and is used to add styles such as shadows to elements.sticky
position can be used for headers and navbar as shown in the example for Sticky above.
These are not the only available use cases of position
property. I highly recommend you to practice and get your hands dirty to understand these concepts in depth.