I want to get the font
css property from an element. I'm using:
window.getComputedStyle(element)
.getPropertyValue('font');
It works perfectly in Chrome, but returns ""
in IE, Edge, and Firefox.
Getting each font property (font-style
, font-variant
, font-weight
, font-size
, line-height
, font-family
) separately works, but I'm trying to avoid building the font
property myself.
Here is the HTML I'm using (and the jsbin):
<div id="elm">Foo</div>
<script>
var elm = document.getElementById('elm');
function css(element, property) {
return window
.getComputedStyle(element)
.getPropertyValue(property);
}
console.log(css(elm, 'font'));
console.log(css(elm, 'font-style'));
console.log(css(elm, 'font-weight'));
console.log(css(elm, 'font-variant'));
console.log(css(elm, 'font-family'));
console.log(css(elm, 'font-size'));
</script>
Edit:
There are similar questions for background and for border. The former is solved by using css classes, and the later by copying the style from one element to another.
I'm looking for the browser to give me the shorthand property.