CSS vertical alignment and baseline position
Asked Answered
M

1

10

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):

  1. In the 1st div, everything seems fine. All the 3 spans are aligned to the baseline of the line box.
  2. In the 2nd div, after I set the vertical-align property to top for 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.
  3. The 3rd div is even more wired to me. I set the vertical-align property to bottom for 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.

Mistaken answered 20/12, 2013 at 5:58 Comment(2)
#4357815Decasyllable
Note that the fiddle's third example is different in IE (11) from Chrome (31) and Firefox (25 & 26).Followthrough
W
11

vertical-align is mostly used for inline element for example img tag, which is commonly set to vertical-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-align works with a value of middle.

So, first lets see what are the valid values for vertical-align property.

enter image description here Credits : Mozilla Developer Network

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-height to the span which varies, but the fact is line-height is actually not applied as the way you think...

Line height is actually not getting applied

Make it inline-block and it will be applied

You can read this answer for more information, that why using line-height on span is useless.


Moving on to your second doubt, you are having line-height on first span, second span but not the third span so what's happening here? As span is inline with the text and anyways line-height won't play the role there as I previously explained, they are happily aligned vertically with the text, whereas when you use vertical-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 text


Coming to the last example of yours, in here, first span element is aligned to the very bottom as 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-height is what it makes that element align vertically center and last, moves a bit to the top, which is infact aligned to the top.

Lets make them inline-block and 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-height property and inline-block property as well, also don't forget to refer the answer I shared.

Wellknit answered 20/12, 2013 at 6:42 Comment(4)
Hi, @Mr. Alien, I still think the line-height property will be applied to inline elements. According to the specification, when calculating the height of line box, 1.The height of each inline-level box in the line box is calculated. For replaced elements, inline-block elements, and inline-table elements, this is the height of their margin box; for inline boxes, this is their 'line-height'Mistaken
What I am assuming is that setting the vertical-align property in the inline boxes to top/down will actually change the baseline position of the line box. And due to the baseline is moved up or down, the other boxes will be affected somehow. But I am not sure whether I am heading to the right direction...Mistaken
@YuanLi baseline doesn't move, the boxes touch the baseline or the top, depends on what you setWellknit
I did read your answer and links as well as the one given by Dilish, and I still insist, at least for the time being :) Might be I need sometime to digestMistaken

© 2022 - 2024 — McMap. All rights reserved.