Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Check if font-family is Honored

function checkFont(strFamily) {
  var objDiv = document.createElement('div');

  objDiv.style.fontFamily = strFamily;
  objDiv.appendChild(document.createTextNode('FONT TEST'));

  if (window.getComputedStyle) {
      return window.getComputedStyle(objDiv, null).getPropertyValue('font-family') === strFamily;
  }

  return objDiv.currentStyle.fontFamily === strFamily;
}

Usage

var iconFontHonored = checkFont('icomoon');

Reference URL

Comments

  1. Ryan McKay
    Permalink to comment#

    Neat! Useful, thanks :)

  2. Federico Brigante
    Permalink to comment#

    This is 100% incorrect. The value of window.getComputedStyle(objDiv, null).getPropertyValue('font-family') will always be what you declared.

    Here: http://codepen.io/anon/pen/HIKDq
    Chrome reports all are missing, Firefox reports all available.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

```
<script>
  function example() {
    element.innerHTML = "<div>code</div>";
  }
</script>
```