This is related to how line-height
works and how the height of each element is calculated.
Let's start with a trivial example to highlight the effect of line-height
span {
border:1px solid red;
padding:5px;
line-height:50px;
}
<span>some text</span> <span style="display:inline-block">some text</span>
Note how in the second case the height is getting bigger but not in the first case.
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.ref
The above apply to the inline-block
element and it's clear that line-height will increase the height. To be more accurate, the height of the inline-block is the sum of its line boxes height and in your case we have only one defined with the line-height
value.
On a non-replaced inline element, 'line-height' specifies the height that is used in the calculation of the line box height.
The above apply to the inline
element where the line-height won't increase the height but will only increase the height of the line box where it belong
span {
border:1px solid red;
padding:5px;
}
<div style="border:1px solid blue;">
<span>some text</span>
</div>
<br>
<div style="border:1px solid blue;">
<span style="line-height:100px;">some text</span>
</div>
To identify the height of an inline element, it's a bit tricky because it depends on the font properties and is covered in another part of the specification:
The 'height' property does not apply. The height of the content area should be based on the font, but this specification does not specify how. A UA may, e.g., use the em-box or the maximum ascender and descender of the font. (The latter would ensure that glyphs with parts above or below the em-box still fall within the content area, but leads to differently sized boxes for different fonts; the former would ensure authors can control background styling relative to the 'line-height', but leads to glyphs painting outside their content area.) ref
Considering this we can identify the height of each one.
For the first line:
- the inline-block is inherting
line-height:1.5
and font-size:1rem
so we will get a line-box height equal to 1.5x1rem = 1.5x16px = 24px
. we add the padding/border to get the 32px
- the inline element is getting a content height for the font equal to
21px
1 and adding padding/border we get 29px
For the second line the .badge
will apply font-size: 75%;line-height: 1;
so
the inline-block will get 1x1remx0.75 = 0.75x16px = 12px
and with border/padding we get 20px
the inline element will logically have 75% of the previous content area so 0.75x21px = 15.75px
and with border/padding we get 23.75px ~ 24px
1In order to identify how the content area is calculated you need to see how the font is designed and find some complex metrics.
below some a related questions that can help you:
What determines the space between the highest and lowest letter and the top and bottom border of the element the letter's in?
How can I calculate the size of font when using different font-types?
Another related question around height calculation: How to determine height of content-box of a block and inline element