To define when to use position:absolute
and when position:relative
:
When we want the element NOT to depend on the natural flow of the other elements we use absolute
and it is positioned with coordinates top
and left
. always start in the upper left corner of your container.
For elements with a relativo
position, the origin of their coordinates is not the upper left corner of the container element, but the position they would naturally occupy, not conditioned or positioned with top
or left
.
EXAMPLES:
When you use position:relative
and position it with margin
the result is that it will affect the elements of natural order, note that with the% margin .logo1
not only moves the same, but also moves to the next element .logo2
:
#normal{
border: 5px solid black;
}
#normal .logo0{
width:100px;
border: 2px solid yellow
}
#normal .logo1{
width:100px;
position: relative;
border: 2px solid red;
margin-left: 30px; //posicion con margen
}
#normal .logo2 {
width:100px;
border: 2px solid blue;
}
<div id="normal">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo0">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo1">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo2">
</div>
When you use position:relative
and position it with top
and / or left
the result is that only the same element .logo1
will be displaced from its natural position, without affecting the other elements.
#normal {
border: 5px solid black;
}
#normal .logo0 {
width: 100px;
border: 2px solid yellow
}
#normal .logo1 {
width: 100px;
position: relative;
border: 2px solid red;
left: 30px; /*posicionamiento con left y right*/
top: 10px;
}
#normal .logo2 {
width: 100px;
border: 2px solid blue;
}
<div id="normal">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo0">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo1">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo2">
</div>
When you use position:absolute
and position it with top
and / or left
the result is that only the same element .logo1
will move from the upper left corner of your container #normal
, without having account for other elements of natural order.
#normal {
border: 5px solid black;
}
#normal .logo0 {
width: 100px;
border: 2px solid yellow
}
#normal .logo1 {
width: 100px;
position: absolute;
border: 2px solid red;
left: 70px;
top: 50px;
}
#normal .logo2 {
width: 100px;
border: 2px solid blue;
}
<div id="normal">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo0">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo1">
<img src="https://image.freepik.com/iconos-gratis/murcielago-con-las-alas-abiertas-logo-variante_318-46799.jpg" class="logo2">
</div>