The real reason is that the way they work is different, and this can be understood by looking at the W3C Specs for line height and inheritance . The following is the specification for row-by-row about dimensionless value ( <number>
) and percentage value.
<number> - The property value used is a number times the font size of the element. Negative values ββare illegal. The calculated value matches the specified value .
<percent> - the calculated value of the property is the percentage times the size of the calculated element size . Negative values ββare illegal
As BoltClock points out in his comment (correctly, as usual), inheritance always works the same way, and always the computed value gets inherited . The confusion with the wording was that I was referring to an older version of the specification, while the new one is very clear and uses the right words.
If a single value (number) is specified, the specified value for line-height
is a number, which is also a calculated value. Thus, h1
(child) inherits a number equal to 1, but still you need to resolve this number to the actual line-height
that you can use. Thus, the value used is calculated based on specifications by multiplying the inherited factor by the font size h1
(which is 2em = 32px) and sets the line-height
to 32px
.
For a percentage value, the calculated value for line-height
at body
is 100% of the body font size (which is 16px
) and therefore equal to 16px
. Now, since this 16px
is a computed value, the child of h1
inherits this value and uses it as is, because there is no need for additional permissions.
This is why there is a difference between the two fragments, since in one row the height for h1
is 16px and in the other 32px.
If we set line-height: 100%
directly to h1
, then we will see that the outputs correspond, because now h1
calculates its own line height as 100% of 2em (or 32px), which is equal to 1 * its font size.
h1 { line-height: 100%; }
<h1> Hello <br> world </h1>