r/csshelp • u/void5253 • May 13 '23
Help understanding why this is centering the div?
/r/css/comments/13gd0u2/help_understanding_why_this_is_centering_the_div/
3
Upvotes
r/csshelp • u/void5253 • May 13 '23
1
u/be_my_plaything May 13 '23
insetis the distance it should be from the edges of the parent.So, basically, a shorter way of saying...
...If you just had
inset:0;and didn't declare a size it would fill the parent, as you're basically telling it to touch every edge. (similarly if inset had a value, eg: 100px, that would tell it to stop 100px before each edge giving a margin all round it.Except in this case it tries to touch every edge but can't as you have declared width and height so it is too small to do so. It starts by default in the top left corner (touching those edges) and wants to reach the bottom right but can't because it is too small, leaving the rest of the area as free space.
Finally
margin:autotells it to apply that free space evenly between each side. So rather than having the element at the start (top left) and all the free space after it (to the right and bottom) it now splits the remaining space evenly before and after itself therefore centering it.Finally
position:fixed;simply means it is attached to that position, (eg won't scroll with screen) the same result would be achieved withposition:absoluteto start with, but that would scroll with screen. But is lost with standard positioning such asrelativesince that only works in relation to the previous element so it has no vertical height to center within (Unless it is within a parent with a declared height) since default height value isauto, it would still be horizontally centered since default width is 100% so it still has a value to work from.