Position property¶
position
property, allows you to determine the position of an HTML item relative to others. It can take the following values:
static
- the default value that causes this element to be set "naturally".relative
- allows you to change the position using thetop
,bottom
,left
,right
properties to your natural position. absolute - allows you to determine the position with the properties of thetop
,bottom
,left
,right
with respect to a parent element which is in the relative* position (for which thepostion
property has valuerelative
).fixed
- places the item at a fixed position relative to the browser window using the propertiestop
,bottom
,left
,right
. This value is often used to "glue" the menu at the top or side of the page.
In the following examples we will consider positioning on the basis of the following HTML code:
<div class="father">
<div class="child-red">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut quasi recusandae omnis, beatae libero ipsam ab facere dolorum illo ratione accusantium alias quam, adipisci repellat animi numquam rem nobis eum. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut quasi recusandae omnis, beatae libero ipsam ab facere dolorum illo ratione accusantium alias quam, adipisci repellat animi numquam rem nobis eum.</div>
<div class="child-blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut quasi recusandae omnis, beatae libero ipsam ab facere dolorum illo ratione accusantium alias quam, adipisci repellat animi numquam rem nobis eum. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut quasi recusandae omnis, beatae libero ipsam ab facere dolorum illo ratione accusantium alias quam, adipisci repellat animi numquam rem nobis eum.</div>
</div>
Static position¶
In the default position, i.e. static
, the elements will be arranged as follows:
.father {
background-color: orange;
height: 1500px;
}
.child-red {
background-color: red;
height: 50px;
}
.child-blue {
background-color: blue;
height: 50px;
}
Relative position¶
In the relative position, the element is positioned relative to its natural position, without releasing space for other elements, e.g:
.father {
background-color: orange;
height: 1500px;
}
.child-red {
background-color: red;
height: 50px;
position: relative;
left: 30px; /* 30px offset from the left */
top: 30px; /* 30px offset from the top */
}
.child-blue {
background-color: blue;
height: 50px;
}
Absolute position¶
In the absolute position, the element is positioned relative to the parent element in the relative
position, while freeing space for other elements.
.father {
background-color: orange;
height: 1500px;
position: relative;
}
.child-red {
background-color: red;
height: 50px;
position: absolute;
left: 30px; /* 30px offset from the left */
top: 30px; /* 30px offset from the top */
}
.child-blue {
background-color: blue;
height: 50px;
}
Fixed position¶
The element using the value fixed
is placed in a fixed position relative to the browser window. When the page is scrolled (if possible), it remains in the fixed position.
.father {
background-color: orange;
height: 1500px;
}
.child-red {
background-color: red;
height: 50px;
position: fixed;
left: 130px; /* 130px from the left relative to the browser window */
top: 130px; /* 130px from the top relative to the browser window */
}
.child-blue {
background-color: blue;
height: 50px;
}