So this question says that the font-size
defines the height of a box so that all letters (with ascenders and descenders) can fit.
But why has a span
with 40px font-size
and line-height
an actual size of 45px? If I understand the linked question correctly then "X" should be smaller than 40px but the overall height should be exactly 40px. I thought that maybe it is making some extra room above/below the ascenders/descenders but the image shows that the ascenders/descenders take all the space so there can't be much extra room:
When I wrap a div
(green) around the span
then the div
has a height of 40px. Why does the div
use the font-size
of its child for its height but the child itself doesn't?:
Now when I set the span
's line-height
to 15px (less than the font-size
) then the div
's height changes to 26px. How is this value calculated? Has this something to do with the baseline?:
When I set the span
's line-height
to 65px (more than the font-size
) then the div
's height is the height of the line-height
. I would have expected the div
's height to be something like (65px - 45px) + 45px.:
So how do font-size
and line-height
affect the actual heights of elements? I read some questions that referenced the spec but I couldn't make much out of it. Are there any easy to understand rules?
Fiddler
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…