0

The following element:

<span>Test</span>

span {
  font-family: Verdana;
  font-size: 16px;
  font-weight: bold;
  line-height: 1.15;
}

Has a height of 19px in Chrome, and 21px in Firefox (fiddle). I tried applying all sorts of CSS resets/normalization, the height is still different. The text itself is rendered identically, but the element height is off by 2 pixels, which breaks my layout.

Any way to fix it without using (inline) block elements?

riv
  • 6,846
  • 2
  • 34
  • 63

5 Answers5

2

Use this :

span {
  font-family: Verdana;
  font-size: 16px;
  font-weight: bold;
  line-height: 1.15;
  display: inline-block;
}

The difference is due to different render of fonts in browsers.

Arash Hasanzade
  • 490
  • 3
  • 12
0

This is a well known problem. It is caused by the fact that Firefox and Chrome use different rendering engines, Gecko and Webkit respectively. Unfortunately there is no 'best' way to fix it.

You can find a few workarounds in this answer and this one.

Dryr
  • 113
  • 7
0

Because of span is inline element, you should re-write your code like following way:

document.querySelectorAll("span").forEach(el => {
  el.textContent = el.offsetHeight + "px";
});
span {
  font-family: Verdana;
  font-size: 16px;
  font-weight: bold;
  line-height: 1.15;
  display: inline-block; /* Key line */
  vertical-align: top; /* It is recommended when using "display: inline-block"*/
}
<span>Test</span>
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
Hanif
  • 3,739
  • 1
  • 12
  • 18
-1

The reason for that behaviour is that you are using a span which is an inline element. It does not change its container height based on the line height but based on its parent block element. Apparently, Chrome and Firefox have different default styles for that.

Either make the span a block element using display: block or replace it with a block element like div.

str
  • 42,689
  • 17
  • 109
  • 127
-2

About the height differences the issue is that you have added font-size, family and line-height as well.

So because of this 3 things :

font-family: Verdana;
font-weight: bold;
line-height: 1.15;

your size of text getting bigger then 16px.

KMG
  • 114
  • 7