Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .css('fontSize') produce different results in Edge?

Consider this code (also in a fiddle):

document.getElementById("span").innerHTML += $('#input').css('fontSize');
span input {
  font-size: inherit;
}

input {
  font-size: 15px;
}
<span id="span" style="font-size: 30px;">
  <input id="input"/>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

In Chrome and Firefox, the .css('fontSize') will return 30px, in Edge and IE it's 15px. Why does it do that? The DOM Explorer in Edge even shows the 15px in strikethrough, and therefore should take the inherited 30px as the fontSize:

And the input is rendered with a 30px font, so IE/Edge is picking it up for rendering purposes.

like image 833
Shlx Avatar asked Nov 10 '16 12:11

Shlx


People also ask

Is fontsize inherited?

When you set font-size on an element, the element certainly does not inherit font size. This is about the effect of the relative nature of the em unit.

What is 1em equal to?

In an element with a 2in font, 1em thus means 2in.

What is PT size in CSS?

Points (pt) as CSS font size Points are a unit of measurement used in print. Points are based on an inch on a ruler, and one inch is equal to 72 points.


Video Answer


1 Answers

Update: The bug below is now fixed; FremyCompany says he/she is a program manager from the Edge team and the fix will reach customers in early 2017.


It looks very much like an IE and Edge bug. Not having found it, I reported it.

Here's an update to the snippet that sees what IE/Edge is telling jQuery via getComputedStyle or currentStyle:

var input = $("#input");
console.log("jQuery: " + input.css('fontSize'));
if (window.getComputedStyle) {
  console.log("getComputedStyle: " + getComputedStyle(input[0]).fontSize);
}
if (input[0].currentStyle) {
  console.log("currentStyle: " + input[0].currentStyle.fontSize);
}
span input {
  font-size: inherit;
}

input {
  font-size: 15px;
}
<span id="span" style="font-size: 30px;">
  <input id="input"/>
  <span id="size"></span>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

For me, IE11 returns 15px from both getComputedStyle and the Microsoft-specific currentStyle property (it's reassuring that they do at least say the same thing):

enter image description here

So it's not a jQuery bug, it's a Microsoft bug when reporting the size via JavaScript (looks like when inherit is the governing rule), even though it's rendering it correctly.

I tried to find a way to make this a grey area, but couldn't think of anything. For instance, according to the spec, having an input inside a span is entirely valid.

like image 72
T.J. Crowder Avatar answered Sep 22 '22 08:09

T.J. Crowder