I am having a hard time understanding how vertical-align:middle works in CSS. All this while I knew it in my head that if there are inline elements (say a span followed by an image and then followed by a span) all sitting side by side:
<span class="align">THIS IS A SPAN </span><img src="https://thinktwisted.com/wp-content/uploads/2020/07/imageResizer.png"><span>THIS IS A SPAN </span>
I would:
img {
vertical-align:middle
}
and the spans would be vertically align with the image like below:
But what I am failing to understand is how this actually works. Mozilla docs says:
middle: Aligns the middle of the element with the baseline plus half the x-height of the parent
In my case the img tag doesn't have any parent and its parent is the body. In that case where is the baseline to which the middle of the image is aligned? Also, even though I am targetting the img tag the image itself doesn't seem to be affected only the span is moving up to the middle of the image . How is that happening?
Additionally if I target the span itself nothing changes:
span {
vertical-align: middle;
}
What is happening when I do that?
How is vertical-align evaluated and which element is actually affected?