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.

  3. Robert
    Permalink to comment#

    If the topic is validating available fonts I’m not getting how this will help. What I need, and what I think most designers need, is a script that will tell me which of the fonts in the font stack appears when the page appears on each browser and OS.

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>
```