Equivalent to bfavaretto, but compatible for older browsers:
function getStyle(elem, prop)
{
if (elem.currentStyle) { //IE8
prop = prop.replace(/-([a-z])/gi, function (value) {
return value.toLowerCase();
});
return elem.currentStyle[prop];
} else if (window.getComputedStyle) {//Navegadores modernos
prop = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
return window.getComputedStyle(elem, null).getPropertyValue(prop);
}
}
console.log(getStyle(document.getElementById('Teste'), 'font-size'));
console.log(getStyle(document.getElementById('Teste'), 'fontSize'));
html {font: normal 16px / 25px 'Montserrat';}
<p id="Teste">Foo bar</p>
Extra
Even using jQuery.css or getStyle there is a very important detail, in different browsers may occur of not returning in pixels (px
), can return values like em
or pt
.
To remove these characters I recommend using .replace
with regex and not use parseInt
without checking if the value is null
or something else (such as empty), because otherwise it can be a problem, do so:
tamanhoFonte = $(elemento).css('font-size'); //ou getStyle(elemento, 'font-size')
tamanhoFonte = tamanhoFonte.replace(/[a-z]/gi, ""); //Remove letras, mas mantem pontos e numeros
tamanhoFonte = tamanhoFonte ? parseInt(tamanhoFonte) : 0;
You want to catch "literally":
16px / 25px
or wants to take the "compensated"?– Guilherme Nascimento
So I want to just take the 16px, which is the font size
– Wendell Mosquini Pozzatti