Using the position property should be done with a specific purpose in mind. Block and inline elements handle most positioning tasks. However, there are cases where the position:
property is necessary, especially with the relative and absolute positions being the most commonly used. Let me help you grasp these concepts.
The position: absolute;
property allows you to set a specific position, as shown in this example:
div {
position: absolute;
top: 50px;
left: 50px;
}
In this code snippet, the selected div element is positioned 50 pixels from the top and left borders. It's important to define the reference point for these borders.
To establish this reference point, we use position: relative;
. Here's an example:
.parent {
position: relative;
}
.child{
position: absolute;
top: 50px;
left: 50px;
}
In this scenario, the parent element serves as the reference for its child element. Therefore, the child with position: absolute;
will be placed 50 pixels away from the top and left borders of its .parent
. I hope this clarifies things for you.
For further reading on positioning, check out this article on W3schools: W3 Positioning