I have stumbled upon something that made me doubt my understanding of percentage values in css
I was under the impression that percentage values wouldn't work when the length they took as a reference didn't have a defined value (for instance, setting the height of an element to any % value doesn't work unless the parent element has a specified height on which the % can be computed upon)
However, I found out by testing that this is not the case for the top & bottom properties of absolutely positioned elements, here's an example, it consists of 2 divs of content-dependent height, the parent (green) is has position:relative and the child (red) is absolutely positioned and has a top:50% position value
.parent_element{
background:#55cc55;
position:relative;
}
.child_element{
background:#dd5555;
position:absolute;
top:50%;
}
<div class="parent_element">
<div class="child_element">
this is<br>a<br>test
</div>
<!-- lorem ipsum text-->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet rutrum est. Mauris sed sollicitudin erat, a condimentum diam. Duis nec dui nec nibh tempus condimentum. Suspendisse potenti. Quisque ligula augue, bibendum sed eros sed, molestie tempus sapien. Nullam auctor rutrum arcu, eget vehicula turpis mattis at. Cras elit erat, pulvinar ut placerat sed, pulvinar eu arcu. Curabitur pulvinar pharetra nisl non imperdiet. Duis vehicula euismod turpis, sit amet euismod nisi tincidunt nec. Ut congue tristique diam, ac egestas dui egestas quis.
Quisque vitae sapien quis nisi facilisis suscipit a a velit. Vestibulum ut vestibulum velit, sed gravida est. Integer placerat nisl enim, vel suscipit tellus cursus eu. In aliquet malesuada nisi, a aliquet nisi. Etiam volutpat, lacus a condimentum laoreet, purus augue cursus tortor, sed sollicitudin nisi risus non est. Etiam eleifend sollicitudin tortor sit amet semper. Duis tempus sed nisl in condimentum. Fusce sit amet ornare nibh. Vivamus sit amet massa velit. Donec non quam nec risus cursus tincidunt nec aliquet leo. Cras et eros eget justo ultricies laoreet. Suspendisse maximus felis at augue viverra, id iaculis urna commodo. Nulla erat lacus, ultrices quis maximus non, consectetur eget elit. In hac habitasse platea dictumst.
Nullam porttitor in nibh at hendrerit. Phasellus sit amet tristique purus, eu vulputate augue. Nunc ultrices magna libero, ut fringilla nibh hendrerit quis. Nullam imperdiet ornare dolor, viverra aliquam tortor cursus sed. Suspendisse finibus venenatis magna, eu porta elit bibendum in. Sed vitae hendrerit lacus. Sed porttitor tempus magna, in convallis augue pulvinar eu. Aenean et mi fermentum nunc elementum dapibus. Maecenas vulputate sodales magna, vitae commodo lorem dictum eu. Aliquam placerat risus ipsum, posuere lacinia dolor dapibus sit amet. Suspendisse sem ex, venenatis non semper aliquet, semper sollicitudin augue. Aenean arcu dui, vulputate ac sapien in, mollis maximus purus.
</div>
As you can see, the child element positioned left a space of 50% of the parent div's height from said div's top edge, even though the parent didn't have a specified height, so my question is, why does this work? I expected nothing to happen since I didn't specify a height for the parent, yet it works Same when I try bottom instead of top, and it still works no matter how much the parent's content changes it's height
Can someone enlighten me as to when I can expect percentage values to work even though their reference length hasn't been explicitly defined please (I made sure that the parent's div was not being take as reference instead of the height, which is what happens when you use % to specify top and bottom margins of an element)