CSS position is sometimes considered an advanced topic because it can do things that are somewhat unexpected. Well, donât let âthe expertsâ intimidate you from pursuing excellence in your CSS competence! Itâs a very accessible topic once youâre equipped with some of the underlying ideas.
Render Flow
An important concept to understanding relative/absolute positioning is render flow.
The general idea is that HTML elements all take up some space. Your browserâs rendering engine always renders everything in a grid-like fashion, starting at the top-left corner and moving successively towards the bottom-right until itâs done placing all of your HTML content.
If youâve ever had a slow internet connection, and watched as large stuff on the webpage would push everything rightward and downward, that is essentially ârender flowâ in action.
You can change this default behavior using CSS position.
CSS Position
CSS position is sometimes considered an advanced skill because itâs not as intuitive as font-size or margin, etc., since it changes the natural ârender flowâ of the browser.
Today weâre just going to look at position: absolute and position: relative since theyâre perhaps the most versatile ones that will get you a lot of mileage once you feel confident with them.
Relative Positioning
When you make an HTML element position: relative, itâll remain âin the flowâ of the layout but you can move it around!
Along with position: relative youâll usually want to define the top, right, bottom, or left offset.
You can think of ârelativeâ position as being: ârelative to where it was initially positioned.â In this case, the green square is now 25px from the left, and 25px from the top of where it was initially going to be.
Whatâs also worth noting is that its width and height is preserved in the square grid. That means itâs still considered âin the flowâ of the layout⌠it just got kinda nudged.
Absolute Positioning
Absolute positioning is a very powerful CSS rule for moving HTML elements around. Sometimes yielding unexpected results:
The orange square is actually the 13th of these 25 squares (the one in the middle of the grid), but it looks like itâs the last square! Weird. Using position: absolute takes elements âout of flowâ so its grid space gets collapsed.
Yea but whyâs it all the way up there?!
Originating coordinates
The orange square gets placed at the 0x, 0y coordinates (eg.: the top-left corner). Just how browser rendering always begins at the top-left corner, position: absolute elements use that as their rendering origin too. You can use top/right/bottom/left properties to offset it from there.
But, you can also give it different originating coordinatesâŚ
In the example above, the parent element (div.grid) has the position: relative rule which causes the orange square to take that as its rendering origin.
While this may seem unintuitive behavior, itâs actually intentional! Allowing for this gives you a lot of control over where/how you arrange HTML elementsâŚ
Practical Guide to CSS Positioning
CSS position is sometimes considered an advanced topic because it can do things that are somewhat unexpected. Well, donât let âthe expertsâ intimidate you from pursuing excellence in your CSS competence! Itâs a very accessible topic once youâre equipped with some of the underlying ideas.
Render Flow
An important concept to understanding relative/absolute positioning is render flow.
The general idea is that HTML elements all take up some space. Your browserâs rendering engine always renders everything in a grid-like fashion, starting at the top-left corner and moving successively towards the bottom-right until itâs done placing all of your HTML content.
If youâve ever had a slow internet connection, and watched as large stuff on the webpage would push everything rightward and downward, that is essentially ârender flowâ in action.
You can change this default behavior using CSS position.
CSS Position
CSS position is sometimes considered an advanced skill because itâs not as intuitive as
font-size
ormargin
, etc., since it changes the natural ârender flowâ of the browser.These are the possible values for CSS position:
Today weâre just going to look at
position: absolute
andposition: relative
since theyâre perhaps the most versatile ones that will get you a lot of mileage once you feel confident with them.Relative Positioning
When you make an HTML element
position: relative
, itâll remain âin the flowâ of the layout but you can move it around!Along with
position: relative
youâll usually want to define thetop
,right
,bottom
, orleft
offset.You can think of ârelativeâ position as being: ârelative to where it was initially positioned.â In this case, the green square is now
25px
from theleft
, and25px
from thetop
of where it was initially going to be.Whatâs also worth noting is that its width and height is preserved in the square grid. That means itâs still considered âin the flowâ of the layout⌠it just got kinda nudged.
Absolute Positioning
Absolute positioning is a very powerful CSS rule for moving HTML elements around. Sometimes yielding unexpected results:
The orange square is actually the 13th of these 25 squares (the one in the middle of the grid), but it looks like itâs the last square! Weird. Using position: absolute takes elements âout of flowâ so its grid space gets collapsed.
Yea but whyâs it all the way up there?!
Originating coordinates
The orange square gets placed at the 0x, 0y coordinates (eg.: the top-left corner). Just how browser rendering always begins at the top-left corner,
position: absolute
elements use that as their rendering origin too. You can usetop
/right
/bottom
/left
properties to offset it from there.But, you can also give it different originating coordinatesâŚ
In the example above, the parent element (
div.grid
) has theposition: relative
rule which causes the orange square to take that as its rendering origin.While this may seem unintuitive behavior, itâs actually intentional! Allowing for this gives you a lot of control over where/how you arrange HTML elementsâŚ
Conclusion
Hope you learned something here today. Please leave a comment if you have a question or have something to add and I will 100% respond to you.
Keep on coding đ
Well then I hope you do well. đ