I am new to CSS and recently reading the specification and having some problems in understanding the vertical-align property.
<div style="border: 1px solid black;">
<span style="border: 1px solid red; padding-left: 1px; line-height: 30px;"></span>
<span style="border: 1px solid red; padding-left: 1px;"></span>
<span style="border: 1px solid red; padding-left: 1px; line-height: 40px;"></span>
</div>
<div style="border: 1px solid black;">
<span style="border: 1px solid red; padding-left: 1px; line-height: 30px;"></span>
<span style="border: 1px solid red; padding-left: 1px;"></span>
<span style="border: 1px solid red; padding-left: 1px; line-height: 40px; vertical-align: top"></span>
</div>
<div style="border: 1px solid black;">
<span style="border: 1px solid red; padding-left: 1px; line-height: 30px; vertical-align: bottom"></span>
<span style="border: 1px solid red; padding-left: 1px;"></span>
<span style="border: 1px solid red; padding-left: 1px; line-height: 40px; vertical-align: top"></span>
</div>
Above code creates 3 div, each of them contains 3 empty inline boxes (spans):
- In the 1st div, everything seems fine. All the 3 spans are aligned to the baseline of the line box.
- In the 2nd div, after I set the
vertical-alignproperty totopfor the 3rd span, the first two spans are moved up. And I get lost from here, I don't understand why they will be moved up, according to what rule. - The 3rd div is even more wired to me. I set the
vertical-alignproperty tobottomfor the 1st span, and it causes the 2nd span to move slightly lower than the 3rd span (this will be noticed when zoom in enough).
The thing I can find in the specification says below, but what exactly are the multiple solutions? Could anyone shed more light on this?
In case they are aligned 'top' or 'bottom', they must be aligned so as to minimize the line box height. If such boxes are tall enough, there are multiple solutions and CSS 2.1 does not define the position of the line box's baseline.
I've also created a fiddle. Please run it in Firefox or Chrome if you are interested.
vertical-alignis mostly used forinlineelement for exampleimgtag, which is commonly set tovertical-align: middle;inorder to align correctly within the text.Demo (Without the use of
vertical-align)Demo 2 (Using
vertical-align)Ok, so that was a general idea of how
vertical-alignworks with a value ofmiddle.So, first lets see what are the valid values for
vertical-alignproperty.Now, lets solve your doubt step by step..
In the first example, everything's fine according to you but the answer is no, you are applying
line-heightto thespanwhich varies, but the fact isline-heightis actually not applied as the way you think...Line height is actually not getting applied
Make it
inline-blockand it will be appliedYou can read this answer for more information, that why using
line-heightonspanis useless.Moving on to your second doubt, you are having
line-heighton firstspan, secondspanbut not the thirdspanso what's happening here? Asspanisinlinewith the text and anywaysline-heightwon't play the role there as I previously explained, they are happily aligned vertically with the text, whereas when you usevertical-align: top;, it doesn't move the other two boxes above that, instead it aligns to the top of the text.See how the
vertical-align: top;aligns at the top of the textComing to the last example of yours, in here, first
spanelement is aligned to the verybottomas expected, well its correct, moving on to second, you said it's slightly in the lower than the third, because it is not aligned at all,line-heightis what it makes that elementalignverticallycenterand last, moves a bit to thetop, which is infact aligned to thetop.Lets make them
inline-blockand see how they actually behave..So I hope you got the difference between all the three examples, but its necessary for you to understand the
line-heightproperty andinline-blockproperty as well, also don't forget to refer the answer I shared.