Reputation: 708
I have a series of elements with display:inline-block. They contain text of different font-sizes, and are displayed next to each other.
I am trying to understand where the vertical dimensions of the elements come from.
Here is the simple code :
<a class="icon"></a>
<a>H</a>
<a class="icon"></a>
<a>H</a>
<a class="icon"></a>
and the CSS :
a {
font-family: consolas;
display: inline-block;
padding: 10px;
font-size: 14px;
}
.icon:before {
content: "H";
font-size: 44px;
}
a:not(:empty) {
padding: 25px; // (44-14) / 2 + 10
}
Now, contrary to what I would expect, 25px is not the correct value for padding to compensate the font size difference, but 27px is.
I don't really understand why the text height for font-size: 44px is actually 51px and for 14px it is 17px.
Once these values are taken into account, it makes sense (51 - 17) / 2 + 10 = 27 is the right padding compensation.
Upvotes: 0
Views: 1528
Reputation: 201588
For an inline element, which is what a :before
pseudo-element is by default, the height is determined in a manner that depends on the font and on the browser, by the CSS 2.1 spec.
If you set display: inline-block
on the :before
pseudo-element, then you can set its height using the height
property, if desired.
Upvotes: 0