What’s the difference between absolute and relative positioning in CSS?
absolute and relative position properties are used a lot in CSS and seem to be doing the same work, which is positioning an element in the page. But in reality, they’re different.
In this tutorial we’ll see how each one of these properties work.
Let’s just take a simple html page that contains 4 <div></div> tags one inside another. These <div></div> tags have a width, height and a background-color.
the display flex property is just to center the boxes vertically one inside another. So at the end we’ll have:
Now let’s apply the position absolute to the small blue box, which means box 4.
If you refresh the browser you will not see anything changed. Why? Because the absolute position needs top, bottom, left and right values.
let’s position this blue box at the top left corner of the green box. For that we’ll set the top and left to 0px and see what will happen.
So in the browser we’ll have:
Wow, what’s this! This is not what we want at all ! Why did the blue box move to the top left corner of the page instead of the top left corner of the green box ?
Well, it is because when applying the absolute position to an element, It positions itself to the closest parent that has already its position set.
Here in this example, none of the 3 remaining boxes (light blue, red, green) have their position set. The default position that they have is unset.
So the small blue box did not find any parent element to position itself to, so what it did it positioned itself to the body.
Now, if there was a parent element with a specific position already set, let’s say the position of the green box (which means box3) is set to absolute:
We would have had the results wanted:
Let’s say now that we want to position the blue box to the red box, so we will add the absolute position to the red box (box 2) and remove it from the green box. Because if we keep the absolute position in the green box as well, the small blue box will position itself to the the closest parent it finds that has the absolute position.
In the browser we’ll have:
Now let’s change the position type of the blue box and set it this time to relative instead of absolute. Also let’s give it a top value of 10px and a left value of 20px.
So in the browser we’ll have:
As you have probably noticed, the blue box this time positioned itself to its origin, and not to a certain parent element as with absolute position. And this is the difference between absolute and relative positioning.
You can add absolute or relative position to any parent box you want, this will not affect the positioning of the small blue box.
Because relative position cares only about the origin of the element, and not about a certain parent element position like absolute position does.
Now let’s take 3 boxes, or 3 <div></div> tags that will be aligned one next to another. Each box has a background-color:
To continue reading check my tutorial:
https://purpose-code.com/absolute-and-relative-positions-whats-the-difference/