I expect that I could set the line-height of a block element to zero, and each line box inside that element would then be aligned based only on the line-heights of its content. However, when I try to use very small fonts on those inline elements, they seem to align with a lower baseline than necessary for the content in the line. My understanding of the CSS spec doesn't line up with what all browsers are rendering; what do I have wrong?
The code for a simple demo looks like this (and it's in a fiddle too):
body {
font-size:60px;
}
div {
height:3em;
width:8em;
border:1px solid black;
line-height:0; /* minimum line height for contained elements */
}
span {
line-height:normal; /* don't inherit from containing block */
background-color: #cff; /* so we can see positioning */
}
<div><span>Big text works</span></div>
<div><span style="font-size:.5em">Half text size works fine too</span></div>
<div><span style="font-size:.2em">Very small text doesn't align with the top of the containing box. Why does this happen?</span></div>
According to the CSS spec:
On a block container element whose content is composed of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element.
On a non-replaced inline element, 'line-height' specifies the height that is used in the calculation of the line box height.
I know that for a simple example like there are a bunch of ways to hack the position of the small text and get it to end up at the top of the containing block, but I'd really like to understand the actual reason that the text is lining up as it does.