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
The perfect line height depends on the design and size of the font itself. There is no magic number that works for all text. A line height of 1.5 times the font size is a good place to start from, and then you can adjust accordingly. Using an 8 point grid system works well when using 1.5 line height.
If the value of the line-height property is smaller than the value of the font-size , text may overflow the element, and text on one line may overlap text on another line above or below it. It is often convenient to set the value of the line-height in the font shorthand property.
Height is the vertical measurement of the container, for example, height of a div. Line-height is a CSS property to specify the line height i.e. the distance from the top of the first line of text to the top of the second. It is the space between the lines of two paragraphs.
A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.
First, I recommend reading my answer in Inline elements and line-height. To summarize, there are various heights related to inline boxes:
line-height
line-height
, but not here.The other height in your case is the height of the parent div. This is determined by §10.6.3. In this case, since the box establishes an inline formatting context with one line,
The element's height is the distance from its top content edge to [...] the bottom edge of the last line box
So the height of the parent block is given by the height of the line box.
What happens here is that the height of the line box is not the line-height
of your inline box. And that's because the line-height
of the parent block is also taken into account:
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.
The minimum height consists of a minimum height above the baseline and a minimum depth below it, exactly as if each line box starts with a zero-width inline box with the element's font and line height properties.
We call that imaginary box a "strut."
If you set parent's line-height
to 0
, and child's vertical-align
to e.g top
, then the height of the parent will exactly be the line-height
of the child.
.outer { margin-top: 50px; background-color: green; width: 150px; font-family: "Times New Roman"; line-height: 0; } .letter-span-1 { background-color: red; line-height: 40px; font-size: 40px; vertical-align: top; } .letter-span-2 { background-color: red; line-height: 15px; font-size: 40px; vertical-align: top; } .letter-span-3 { background-color: red; line-height: 65px; font-size: 40px; vertical-align: top; }
<span class="letter-span-1">XxÀg</span> <div class="outer"> <span class="letter-span-1">XxÀg</span> </div> The parent block is 40px tall. <div class="outer"> <span class="letter-span-2">XxAg</span> </div> The parent block is 15px tall. <div class="outer"> <span class="letter-span-3">XxÀg</span> </div> The parent block is 65px tall.
If you don't set a line-height
to the parent, it will be normal
.
Tells user agents to set the used value to a "reasonable" value based on the font of the element[...]. We recommend a used value for 'normal' between
1.0
to1.2
.
That means that there will be a minimum height for the parent which will be its font-size
(which you don't specify, and the default is implementation-dependent) multiplied by that factor (implementation-dependent).
You should also consider the vertical-align
of the span. By default it's baseline
, and that may create a gap below. The image in web-tiki's answer is especially useful:
That's because vertical-align
determines how the span will be aligned with the strut, and with baseline
the alignment can depend on font-size
and end up increasing the height of the line box. The line box height is the distance between the top of the uppermost and the bottom of the lowermost boxes in the line.
If you don't want the height of the parent div to be increased by that, you need some other vertical-align
, like top
, middle
, or bottom
. Then the font-size
of the span shouldn't affect the height of the div.
To summarize, the height of the div depends on
line-height
font-size
line-height
font-size
font-size
, depending on span's vertical-align
height
, min-height
, max-height
, etc.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With