I know that these CSS properties are used to position an element within the page.
However, when should you use each of them?
I know that these CSS properties are used to position an element within the page.
However, when should you use each of them?
The property
position
is used to position an element within the page. However, depending on which property we use, the element will take one reference or another to position itself with respect to it.The possible values that the property can take
position
are:static | relative | absolute | fixed | inherit | initial
. I will explain what each of them consists of:position: static
It is the value that an element takes by default to position itself. With this value, the element will respect the normal flow of the page, that is, it will be positioned in its proper place and will ignore the values for the
top
,left
,right
and propertiesbottom
.Notice that I have put an id to the second div and called it
movido
. I am trying to apply the propertyleft
without results.position:relative
Through this value we can position an element with respect to the normal flow of the page. It could be said that we are positioning an element taking as reference the normal flow (the default position) of said element.
We can use
top
,left
,right
andbottom
to position our element taking as reference the default position of the element. In this case, I'm going to apply the propertyleft: 100px
from the previous example and the divmovido
will be shifted 100 pixels to the right from its default position.position: absolute
This value will also accept the values
top
,left
,right
andbottom
. The element withposition: absolute
will not be within the normal flow of the page and will take as reference the browser window or the closest positioned element (which has any value ofposition
except ) if it is the parent of the element we want to position.static
Here is an example of each:
Example taking the browser window as reference
In this case I'm going to take the browser window as a reference and move the div
movido
40 pixels down and 50 pixels to the right with respect to it.You can see that the div
movido
, not being within the normal flow of the page, does not affect the normal flow of the rest of the elements and therefore the other two elements are positioned together (without respecting the space left by the divmovido
in the previous example , in which it is in the normal flow of the page).Example taking as reference the parent element with
position:relative
closestIn this case, I have used the same CSS for the div with id
movido
as in the previous example to show that in this case the divmovido
references the parent element withposition: relative
and not the browser window (40 pixels down and 50 pixels up). to the right of the parent element).position: fixed
The elements that are positioned with
position: fixed
are also outside the normal flow of the page. However, it should not be confused with elements that are positioned withposition: absolute
.Unlike the latter, elements with
position: fixed
take the browser window as a reference and do not respect having a parent container that is positioned. Also, when scrolling the page, the element that is positioned asposition: fixed
will remain in the same position relative to the browser window even if the scroll has moved the page down.As a picture is worth a thousand words and taking as a reference the last example of the section
position: absolute
:As you can see, the div
movido
is positioned withposition: fixed
respect to the browser window regardless of whether it is contained in an element withposition: relative
or whether the page is scrolled.position: inherent
Actually the property
position
with the valueinherit
acts in the same way as the rest of the properties that can obtain this value, it inherits the value for this property from the parent element.position: start
As with the previous value, it acts in the same way as the rest of the properties that can obtain this value, in this case making the property
position
take its default value, so usingposition: initial
, would be the same as indicatingposition: static
.position: sticky
This is a value that is relatively new for this property.
Using this value, the element acts as if it were positioned with the value
relative
until an offset threshold is reached (in the element itself or in the parent element), at which point the element becomes positioned as if it were positioned with the valuefixed
.For example, we are going to take as a reference a horizontal menu that is at the top of the page under the logo of the company to which we belong.
Something like that:
And we want that when scrolling and the menu no longer fits on the screen (that is, the value of the property
top
is less than 0), the menu remains fixed at the top of the screen.Something like that:
For this we will use the value
sticky
since it will do the job for us without the need to useJavascript
.Usage would be like this:
In this example you can scroll up and down to see that the div
menu
freezes when it reachestop: 0
.However - and this is the main reason why I did not indicate it at the beginning of the answer as a property value
position
- it is not yet fully supported by all browsers.You can see the browsers that support this property here .
BONUS: Typical examples.
Here are some typical examples of positioning (centering).
1.- Center div horizontally on the page (with
position: relative
)2.- Center div horizontally on the page (with
position: absolute
)3.- Center div horizontally on the page (with
position: static
, default value)4.- Center div horizontally inside another div
5.- Center div horizontally and vertically on the page
6.- Center div vertically and horizontally inside another div
I think this question-article needs a summary:
Static: Default value. The element respects its position relative to the parent element and its siblings, which determines the order in which it is painted on the screen.
Relative: Frees the element from its original position and combined with top, bottom, left and right, allows us to move it with respect to it. The position of the rest of the elements is not affected.
Absolute: Same as relative, but the top, bottom, left, and right attributes will position it relative to the first parent element with a position value other than static or initial, or if not present, the HTML element. The freed space becomes occupied by the rest of the sibling elements.
Fixed: Identical to absolute except that the element is always positioned relative to the :root, so it is not affected by any scroll, not even the document scroll.
Inherit: Inherits the value of the direct parent.
Initial: Returns the initial value, which in this case is static.