Your Codepen example defaults to Arial font. If you look in Dev tools (Chrome) you'll see the textbox as inner dimensions of 195x18, with the top and bottom padding of 10px, that's your 38 pixels in height explained.
Now set the .my_input
class to use font-family: Tahoma;
, now the inner dimensions are 191x19.
Why? Your browser is computing a reasonable value depending on the font size set or inherited on the element.
Excellent deep dive here, https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align.
Here's a key takeaway:
The Arial font describes an em-square of 2048 units, an ascender of 1854, a descender of 434 and a line gap of 67. It means that font-size: 100px gives a content-area of 112px (1117 units) and a line-height: normal of 115px (1150 units or 1.15). All these metrics are font-specific, and set by the font designer.
Note the 1.15
value. If we calculate (font size) 16px x 1.15
the result is 18.4
. On Internet Explorer the height of the TextBox element is 18.4px
(Chrome displays only 18px
, an example of browsers treating this differently).
It's not the font size that's being changed, just the line height of the element.
In summary line-height
depends on font-family
, font-size
and your browser. For this reason height
is the more appropriate property to guarantee the result you expect in this case.