<p>Suppose I have the following CSS rule in my page:</p>
<pre class="lang-css prettyprint-override"><code>body {
    font-family: Calibri, Trebuchet MS, Helvetica, sans-serif;
}
</code></pre>
<p>How could I detect which one of the defined fonts were used in the user's browser?</p>
<p>For people wondering why I want to do this is because the font I'm detecting contains glyphs that are <em>not</em> available in other fonts. If the user does <em>not</em> have the font, then I want it to display a link asking the user to download that font (so they can use my web application with the correct font).</p>
<p>Currently, I am displaying the download font link for all users. I want to only display this for people who do <em>not</em> have the correct font installed.</p>

[{'answer_id': 849, 'author': 'MojoFilter', 'author_id': 93, 'author_profile': 'https://Stackoverflow.com/users/93', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve seen it done in a kind of iffy, but pretty reliable way. Basically, an element is set to use a specific font and a string is set to that element.  If the font set for the element does not exist, it takes the font of the parent element.  So, what they do is measure the width of the rendered string.  If it matches what they expected for the desired font as opposed to the derived font, it\'s present. This won\'t work for monospaced fonts.</p>\n\n<p>Here\'s where it came from:\n<a href="http://ajaxian.com/archives/javascriptcss-font-detector" rel="noreferrer">Javascript/CSS Font Detector (ajaxian.com; 12 Mar 2007)</a></p>\n'}, {'answer_id': 27035, 'author': 'runeh', 'author_id': 2906, 'author_profile': 'https://Stackoverflow.com/users/2906', 'pm_score': 4, 'selected': False, 'text': '<p>A technique that works is to look at the computed style of the element. This is supported in Opera and Firefox (and I recon in safari, but haven\'t tested). IE (7 at least), provides a method to get a style, but it seems to be whatever was in the stylesheet, not the computed style. More details on quirksmode: <a href="http://www.quirksmode.org/dom/getstyles.html" rel="noreferrer">Get Styles</a></p>\n\n<p>Here\'s a simple function to grab the font used in an element:</p>\n\n<pre><code>/**\n * Get the font used for a given element\n * @argument {HTMLElement} the element to check font for\n * @returns {string} The name of the used font or null if font could not be detected\n */\nfunction getFontForElement(ele) {\n    if (ele.currentStyle) { // sort of, but not really, works in IE\n        return ele.currentStyle["fontFamily"];\n    } else if (document.defaultView) { // works in Opera and FF\n        return document.defaultView.getComputedStyle(ele,null).getPropertyValue("font-family");\n    } else {\n        return null;\n    }\n}\n</code></pre>\n\n<p>If the CSS rule for this was:</p>\n\n<pre><code>#fonttester {\n    font-family: sans-serif, arial, helvetica;\n}\n</code></pre>\n\n<p>Then it should return helvetica if that is installed, if not, arial, and lastly, the name of the system default sans-serif font. Note that the ordering of fonts in your CSS declaration is significant.</p>\n\n<p>An interesting hack you could also try is to create lots of hidden elements with lots of different fonts to try to detect which fonts are installed on a machine. I\'m sure someone could make a nifty font statistics gathering page with this technique.</p>\n'}, {'answer_id': 887291, 'author': 'philoye', 'author_id': 109864, 'author_profile': 'https://Stackoverflow.com/users/109864', 'pm_score': 4, 'selected': False, 'text': '<p>@pat Actually, Safari does not give the font used, Safari instead always returns the first font in the stack regardless of whether it is installed, at least in my experience.</p>\n\n<pre><code>font-family: "my fake font", helvetica, san-serif;\n</code></pre>\n\n<p>Assuming Helvetica is the one installed/used, you\'ll get:</p>\n\n<ul>\n<li>"my fake font" in Safari (and I believe other webkit browsers).</li>\n<li>"my fake font, helvetica, san-serif" in Gecko browsers and IE. </li>\n<li>"helvetica" in Opera 9, though I read that they are changing this in Opera 10 to match\nGecko.</li>\n</ul>\n\n<p>I took a pass at this problem and created <a href="http://github.com/philoye/fontunstack/tree/master" rel="noreferrer">Font Unstack</a>, which tests each font in a stack and returns the first installed one only. It uses the trick that @MojoFilter mentions, but only returns the first one if multiple are installed. Though it does suffer from the weakness that @tlrobinson mentions (Windows will substitute Arial for Helvetica silently and report that Helvetica is installed), it otherwise works well.</p>\n'}, {'answer_id': 6083463, 'author': 'Derek 朕會功夫', 'author_id': 283863, 'author_profile': 'https://Stackoverflow.com/users/283863', 'pm_score': 5, 'selected': False, 'text': '<p>I wrote a simple JavaScript tool that you can use it to check if a font is installed or not.<br>\nIt uses simple technique and should be correct most of the time.\n<br></p>\n\n<p><strong><a href="https://github.com/derek1906/jFont-Checker/" rel="noreferrer">jFont Checker</a></strong> on github</p>\n'}, {'answer_id': 7866425, 'author': 'Facebiz', 'author_id': 1009517, 'author_profile': 'https://Stackoverflow.com/users/1009517', 'pm_score': 3, 'selected': False, 'text': '<p>A simplified form is:</p>\n\n<pre><code>function getFont() {\n    return document.getElementById(\'header\').style.font;\n}\n</code></pre>\n\n<p>If you need something more complete, check <a href="http://www.lalit.org/lab/javascript-css-font-detect/#comment-43" rel="noreferrer">this</a> out.</p>\n'}, {'answer_id': 8307897, 'author': 'Naeem Ul Wahhab', 'author_id': 1067051, 'author_profile': 'https://Stackoverflow.com/users/1067051', 'pm_score': 3, 'selected': False, 'text': "<p>There is a simple solution - just use <code>element.style.font</code>:</p>\n\n<pre><code>function getUserBrowsersFont() {\n    var browserHeader = document.getElementById('header');\n    return browserHeader.style.font;\n}\n</code></pre>\n\n<p>This function will  exactly do what you want. On execution It will return the font type of the user/browser. Hope this will help.</p>\n"}, {'answer_id': 8741592, 'author': 'PaulnOZ', 'author_id': 1131991, 'author_profile': 'https://Stackoverflow.com/users/1131991', 'pm_score': 3, 'selected': False, 'text': '<p>Another solution would be to install the font automatically via <code>@font-face</code> which might negate the need for detection.</p>\n<pre class="lang-css prettyprint-override"><code>@font-face {\n  font-family: &quot;Calibri&quot;;\n  src: url(&quot;http://www.yourwebsite.com/fonts/Calibri.eot&quot;);\n  src: local(&quot;Calibri&quot;), url(&quot;http://www.yourwebsite.com/fonts/Calibri.ttf&quot;) format(&quot;truetype&quot;);\n}\n</code></pre>\n<p>Of course it wouldn\'t solve any copyright issues, however you could always use a freeware font or even make your own font. You will need both <code>.eot</code> &amp; <code>.ttf</code> files to work best.</p>\n'}, {'answer_id': 13385418, 'author': 'Mark Kimitch', 'author_id': 1090092, 'author_profile': 'https://Stackoverflow.com/users/1090092', 'pm_score': 3, 'selected': False, 'text': "<p>Calibri is a font owned by Microsoft, and shouldn't be distributed for free. Also, requiring a user to download a specific font isn't very user-friendly. </p>\n\n<p>I would suggest purchasing a license for the font and embedding it into your application.</p>\n"}, {'answer_id': 29582806, 'author': 'user2267379', 'author_id': 2267379, 'author_profile': 'https://Stackoverflow.com/users/2267379', 'pm_score': 2, 'selected': False, 'text': '<p>You can use this website :</p>\n\n<p><a href="http://website-font-analyzer.com/" rel="nofollow noreferrer">http://website-font-analyzer.com/</a></p>\n\n<p>It does exactly what you want...</p>\n'}, {'answer_id': 42756664, 'author': 'Woppi', 'author_id': 2098493, 'author_profile': 'https://Stackoverflow.com/users/2098493', 'pm_score': 2, 'selected': False, 'text': '<p>I am using Fount. You just have to drag the Fount button to your bookmarks bar, click on it and then click on a specific text on the website. It will then show the font of that text. </p>\n\n<p><a href="https://fount.artequalswork.com/" rel="nofollow noreferrer">https://fount.artequalswork.com/</a></p>\n'}, {'answer_id': 47571799, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 2, 'selected': False, 'text': '<p>You can put <a href="https://technet.microsoft.com/en-us/library/cc939627.aspx" rel="nofollow noreferrer">Adobe Blank</a> in the font-family after the font you want to see, and then any glyphs not in that font won\'t be rendered.</p>\n\n<p>e.g.:</p>\n\n<pre><code>font-family: Arial, \'Adobe Blank\';\n</code></pre>\n\n<p>As far as I\'m aware there is no JS method to tell which glyphs in an element are being rendered by which font in the font stack for that element.</p>\n\n<p>This is complicated by the fact that browsers have user settings for serif/sans-serif/monospace fonts and they also have their own hard-coded fall-back fonts that they will use if a glyph is not found in any of the fonts in a font stack. <strong>So browser may render some glyphs in a font that is not in the font stack or the user\'s browser font setting.</strong> <a href="https://developers.google.com/web/updates/2013/09/DevTools-answers-What-font-is-that" rel="nofollow noreferrer">Chrome Dev Tools will show you each rendered font for the glyphs in the selected element</a>. So on your machine you can see what it\'s doing, but there\'s no way to tell what\'s happening on a user\'s machine.</p>\n\n<p>It\'s also possible the user\'s system may play a part in this as e.g. <a href="https://learn.microsoft.com/en-us/globalization/input/font-technology#font-linking" rel="nofollow noreferrer">Window does Font Substitution</a> at the glyph level.</p>\n\n<p>so...</p>\n\n<p>For the glyphs you are interested in, you have no way of knowing whether they will be rendered by the user\'s browser/system fallback, even if they don\'t have the font you specify.</p>\n\n<p>If you want to test it in JS you could render individual glyphs with a font-family including Adobe Blank and measure their width to see if it is zero, <strong>BUT</strong> you\'d have to iterate thorough each glyph and <strong>each font you wanted to test</strong>, but although you can know the fonts in an elements font stack there is no way of knowing what fonts the user\'s browser is configured to use so for at least some of your users the list of fonts you iterate through will be incomplete. (It is also not future proof if new fonts come out and start getting used.)</p>\n'}]
['https://Stackoverflow.com/questions/845', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/238/']