{"id":56,"date":"2013-07-02T21:00:37","date_gmt":"2013-07-02T21:00:37","guid":{"rendered":"http:\/\/aurelio.audero.it\/blog\/?p=56"},"modified":"2016-06-20T01:36:12","modified_gmt":"2016-06-20T01:36:12","slug":"how-to-solve-the-problem-of-font-face-inside-media-queries","status":"publish","type":"post","link":"https:\/\/www.audero.it\/blog\/2013\/07\/02\/how-to-solve-the-problem-of-font-face-inside-media-queries\/","title":{"rendered":"How to Solve the Problem of @font-face Inside Media Queries"},"content":{"rendered":"<p>Mobile connections aren&#8217;t as good as the usual desktop ones, therefore the download time of custom fonts can take up to several seconds. A good approach is to use the <code>@font-face<\/code> rule only for certain range of media and screens using <code>@media<\/code> queries. Unfortunately this approach doesn&#8217;t work for some browsers: all the versions of Internet Explorer and FireFox 10 and lower. This article discusses a solution for this problem that balances performances and hacks.<br \/>\n<!--more--><\/p>\n<div class=\"note\">\n  <ins cite=\"http:\/\/modernweb.com\/2013\/07\/08\/solving-the-problem-of-font-face-inside-media-queries\/#comment-1880\" datetime=\"2013-07-18T15:37:00+01:00\">MAJOR UPDATE: With certain constraints, a better approach than the one discussed in this article exists. <a href=\"#better-approach-updated\">More info here<\/a><\/ins>\n<\/div>\n<h2>What&#8217;s the Problem?<\/h2>\n<p>When I started creating my new website, I wanted to create a strongly mobile optimized design having also in mind the achievement of high performances. One of the thing I really wanted to avoid was the effect, seen on several websites when I access them through my smartphone, of a white block where it&#8217;s supposed to be the content. This issue is caused by a custom font, applied to the text, that it isn&#8217;t completely loaded as shown in the image below.<\/p>\n<p><a href=\"http:\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"64\" data-permalink=\"https:\/\/www.audero.it\/blog\/2013\/07\/02\/how-to-solve-the-problem-of-font-face-inside-media-queries\/aeon-megazine-mobile\/\" data-orig-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile.png\" data-orig-size=\"720,1280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Custom font not loaded\" data-image-description=\"&lt;p&gt;An example of a page with a white block where it\u2019s supposed to be the content due to the custom font still loading&lt;\/p&gt;\n\" data-image-caption=\"\" data-large-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile-576x1024.png\" src=\"\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile-168x300.png\" alt=\"A page with custom font not completely downloaded\" width=\"168\" height=\"300\" class=\"aligncenter size-medium wp-image-64\" srcset=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile-168x300.png 168w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile-576x1024.png 576w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/06\/aeon-megazine-mobile.png 720w\" sizes=\"auto, (max-width: 168px) 100vw, 168px\" \/><\/a><\/p>\n<p>So, what&#8217;s the solution? A good approach is to apply a custom font, using the <code>@font-face<\/code> rule, only for certain range of media and screens, using <code>@media<\/code> queries. You might think that the best solution would be to apply the custom font only to those users visiting the website with a fast connection, and you&#8217;re absolutely right. Unfortunately there isn&#8217;t yet such a system although it <a href=\"http:\/\/lists.whatwg.org\/htdig.cgi\/whatwg-whatwg.org\/2012-May\/035799.html\" target=\"_blank\">has been discussed<\/a> several times on the <a href=\"http:\/\/www.whatwg.org\/\" target=\"_blank\"><abbr title=\"Web Hypertext Application Technology Working Group\">WHATWG<\/abbr><\/a> mailing list.<\/p>\n<h2>@font-face inside a @media query<\/h2>\n<p>To better explain the solution, we&#8217;ll create two files: <code>index.html<\/code> and <code>style.css<\/code>. The first is the ideal page having the text with the custom font applied while the latter contains the style of the page.<\/p>\n<p>An hypothetical <code>index.html<\/code> page could be the following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n      &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; \/&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n      &lt;p&gt;\r\n         This is an amazing text because has a custom font.\r\n      &lt;\/p&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>As you can see, it&#8217;s very basic since it has only one paragraph and the link to the stylesheet.<\/p>\n<p>The following, instead, is an hypothetical source for the <code>style.css<\/code> file using a mobile first approach:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\np {\r\n   font-family: Arial,Helvetica,sans-serif;\r\n}\r\n\r\n@media only screen and (min-width: 980px) {\r\n   @font-face {\r\n      font-family: &quot;OctinSports&quot;;\r\n      src: url(&quot;fonts\/octinsports.eot&quot;);\r\n      src: url(&quot;fonts\/octinsports.eot?#iefix&quot;) format(&quot;embedded-opentype&quot;),\r\n      url(&quot;fonts\/octinsports.woff&quot;) format(&quot;woff&quot;),\r\n      url(&quot;fonts\/octinsports.ttf&quot;) format(&quot;truetype&quot;),\r\n      url(&quot;fonts\/octinsports.svg#OctinSports&quot;) format(&quot;svg&quot;);\r\n      font-weight: normal;\r\n      font-style: normal;\r\n   }\r\n\r\n   p {\r\n      font-family: &quot;OctinSports&quot;;\r\n   }\r\n}\r\n<\/pre>\n<p>The stylesheet shown, sets for all the paragraphs an Arial font, with several fallbacks. The font stack used is composed of fonts that are really common and highly present in the major operative systems. In fact, based on <a href=\"http:\/\/cssfontstack.com\/\" target=\"_blank\">CssFontStack<\/a>, Arial achieve the 99.84% on Windows and the 98.74% on Mac. In case none of the selected fonts is available, the CSS asks for a generic sans-serif font.<\/p>\n<p>The second part of <code>style.css<\/code> specifies the media query. With it, we&#8217;re targeting all the devices that have a screen and with the screen&#8217;s width of at least 980px (you can use a different breakpoint). Inside it, we&#8217;ve the <code>@font-face<\/code> rule to download the custom font, using the <a href=\"http:\/\/www.fontspring.com\/blog\/the-new-bulletproof-font-face-syntax\" target=\"_blank\">Fontspring Bulletproof @Font-Face Syntax<\/a>. Then, we&#8217;re simply applying the new font to all the paragraphs, overriding the previously set style.<\/p>\n<h3>Browsers Compatibility<\/h3>\n<p>This solution is really smart because it applies a custom font only for users that have a big screen and should not have problem for a little bandwidth overhead. Unfortunately, this approach is <em>not<\/em> supported by Internet Explorer 10 and lower, and <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=567573\" target=\"_blank\">Firefox 10 and lower<\/a>. Although you can think it&#8217;s a strange behavior, it actually comes from <a href=\"http:\/\/www.w3.org\/TR\/CSS21\/media.html#at-media-rule\" target=\"_blank\">the CSS2.1 specifications<\/a> that asserts: <q cite=\"http:\/\/www.w3.org\/TR\/CSS21\/media.html#at-media-rule\">At-rules inside @media are invalid in CSS2.1.<\/q><\/p>\n<p>So, while the last version of Internet Explorer still doesn&#8217;t support this feature, the company behind FireFox, Mozilla, <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=511909\">added the support<\/a> for versions after the 10th. Therefore, the issue is significant only for Internet Explorer. Another point to keep in mind is also that Internet Explorer 8 and lower doesn&#8217;t understand <code>@media<\/code> queries, so even if they updated new versions, the problem would remain for older ones.<\/p>\n<h2>How to (Partially) Solve the Problem<\/h2>\n<p>Is there a way to solve the problem of <code>@font-face<\/code> inside a <code>@media<\/code> query in Internet Explorer? The answer is yes, partially.<\/p>\n<p>As I said in the introduction, I came across this issue during the development of my website. I wasn&#8217;t aware of the compatibility problem of Internet Explorer, so, just like every good developer, the first thing I did to understand what was going on, was to use Google. During my research, I found an article titled <a href=\"http:\/\/robin.medvedi.eu\/do-not-put-font-face-inside-media-query\/\" target=\"_blank\">Do Not Put @font-face Inside @media Query<\/a> that gave the answer needed on the cause, but not on the solution. Besides, the article also suggested to not have <code>@font-face<\/code> inside <code>@media<\/code> queries. I was really determined to solve the issue, so I searched a little more. Unfortunately, I didn&#8217;t succeed, so I decided to create one by myself.<\/p>\n<h3>Solve the Problem for Internet Explorer 9 and Lower<\/h3>\n<p>Based on what I described so far, the problem is relevant only for Internet Explorer, so we can try to target this specific browser. The first thing that could came to mind is to create a separate CSS file, for example <code>fonts.css<\/code>, containing all the <code>@font-face<\/code> rules, not into a <code>@media<\/code> query, and use a <strong>conditional comment<\/strong>. Thus, the source of <code>fonts.css<\/code> should be like this:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n@font-face {\r\n   font-family: &quot;OctinSports&quot;;\r\n   src: url(&quot;fonts\/octinsports.eot&quot;);\r\n   src: url(&quot;fonts\/octinsports.eot?#iefix&quot;) format(&quot;embedded-opentype&quot;),\r\n   url(&quot;fonts\/octinsports.woff&quot;) format(&quot;woff&quot;),\r\n   url(&quot;fonts\/octinsports.ttf&quot;) format(&quot;truetype&quot;),\r\n   url(&quot;fonts\/octinsports.svg#OctinSports&quot;) format(&quot;svg&quot;);\r\n   font-weight: normal;\r\n   font-style: normal;\r\n}\r\n<\/pre>\n<p>Using this approach, we have the advantage of keeping the weight of the page the same as before, apart for the few bytes for the conditional comment part, for all the browsers but Internet Explorer. For the latter, instead, we&#8217;ll add also a little overhead because of the additional stylesheet to download. However, containing only the very few lines about <code>@font-face<\/code> rules, minimizing it, an average file should cause an <strong>additional weight of no more than 1-2kb<\/strong>, that in most cases is an acceptable compromise.<\/p>\n<p>The code that implements the approach, using the minified version, is the following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!--&#x5B;if IE]&gt;\r\n   &lt;link rel=&quot;stylesheet&quot; href=&quot;css\/fonts.min.css&quot;&gt;\r\n&lt;!&#x5B;endif]--&gt;\r\n<\/pre>\n<p>This solution is simple but not definitive because, as you might know, <a href=\"http:\/\/www.sitepoint.com\/microsoft-drop-ie10-conditional-comments\/\" target=\"_blank\">Internet Explorer 10 dropped support for conditional comments<\/a>, so this version will ignore the snippet. In conclusion, our problems aren&#8217;t ended yet.<\/p>\n<h3>Solve the Problem for Internet Explorer 10<\/h3>\n<p>To target IE10 we&#8217;ll use a less clean approach, because we need a hack, but that works fine. Specifically, the hack used is based on JavaScript and is discussed in the article <a href=\"http:\/\/www.impressivewebs.com\/ie10-css-hacks\/\" target=\"_blank\">IE10 CSS Hacks<\/a>. What this snippet does is to detect IE10 and test if the window&#8217;s width is greater than or equal to 980px. If these conditions are true, it adds the same <code>fonts.min.css<\/code> stylesheet to the <code>&lt;head&gt;<\/code> element of the page. Remember that since it&#8217;s based on JavaScript, if the latter is disabled the hack won&#8217;t work.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!--&#x5B;if !IE]&gt;&lt;!--&gt;\r\n   &lt;script&gt;\r\n       if (Function('\/*@cc_on return document.documentMode===10@*\/')() &amp;&amp; window.innerWidth &gt;= 980) {\r\n          var link  = document.createElement('link');\r\n          link.rel  = 'stylesheet';\r\n          link.href = 'css\/fonts.min.css';\r\n          document.getElementsByTagName('head')&#x5B;0].appendChild(link);\r\n       }\r\n   &lt;\/script&gt;\r\n&lt;!--&lt;!&#x5B;endif]--&gt;\r\n<\/pre>\n<h3>How this Solution Deals with a Responsive Web Design Approach?<\/h3>\n<p>A good question that you can arise is how this solution actually solves the responsive approach of a website. To answer this question, let&#8217;s split the discussion into two branches. The first regards mobile users where we <em>don&#8217;t<\/em> want the custom font, and the second regards desktop users where we <em>do<\/em> want the font to be applied.<\/p>\n<h4>Mobile Users<\/h4>\n<p>To start the discussion, let&#8217;s have a look at the <a href=\"http:\/\/gs.statcounter.com\/#mobile_browser-ww-monthly-201306-201306-bar\" target=\"_blank\">last statistics (June 2013) about mobile browsers usage<\/a> provided by <a href=\"http:\/\/statcounter.com\/\" target=\"_blank\">StatCounter<\/a>. The link gives us the following statistics:<\/p>\n<ol>\n<li>Android: 29.06%<\/li>\n<li>iPhone: 22.77%<\/li>\n<li>Opera: 16.06%<\/li>\n<li>UC Browser: 9.89%<\/li>\n<li>Nokia: 7.38%<\/li>\n<li>Chrome: 3.23%<\/li>\n<li>BlackBerry: 3.11%<\/li>\n<li>NetFront: 2.40%<\/li>\n<li>iPod Touch: 2.21%<\/li>\n<li>Others: 3.9%<\/li>\n<\/ol>\n<p>An important important point to get from these statistics is that none of them is Internet Explorer, and that its usage has been counted in the voice &#8220;Others&#8221;. However, thanks to <a href=\"http:\/\/www.optimalworks.net\/\" target=\"_blank\">Craig Buckler<\/a>, one of the regular and surely the most known blogger of SitePoint, I discovered that StatCounter report an usage of 1.4% for IEMobile (no distinction between versions). Therefore, having the <code>@font-face<\/code> rule inside the <code>@media<\/code> query, we&#8217;re achieving our goal of having a basic font at least for the 98.6% mobile users.<\/p>\n<p><small>Craig also wrote me that <a href=\"http:\/\/marketshare.hitslink.com\/browser-market-share.aspx?qprid=2&#038;qpcustomd=1\" target=\"_blank\">NetMarketShare report shows 1.31% usage for IE9 and 1.0% for IE10 mobile<\/a>. Of course statistics can vary a little from site to site but we got the general point.<\/small><\/p>\n<p>Now, let&#8217;s take into account the two major versions of Internet Explorer for mobile: 9 and 10.<\/p>\n<p>As we&#8217;ve seen in a previous section, we&#8217;re targeting IE10 using a JavaScript code that uses an hack and test for the window&#8217;s width. If the device has JavaScript enabled, being the window&#8217;s width less than 980px, the <code>fonts.min.css<\/code> stylesheet will not be added. Otherwise, if JavaScript is disabled, the snippet won&#8217;t run and the custom font won&#8217;t be loaded or set. Thus, our goal is achieved.<\/p>\n<p>About IE9, there isn&#8217;t much to do: the stylesheet will be loaded and the custom font applied anyway. However, this issue should affect no more than the 50% of those using IEMobile.<\/p>\n<p>To wrap up, the font is <em>not<\/em> applied <strong>in roughly the 99.5% of the cases<\/strong> with a fail only on IE9. A big success.<\/p>\n<h4>Desktop Users<\/h4>\n<p>As for the previous section, let&#8217;s have a look at the <a href=\"http:\/\/gs.statcounter.com\/#browser_version_partially_combined-ww-monthly-201306-201306-bar\" target=\"_blank\">last statistics (June 2013) about desktop browsers usage<\/a>. The desktop statistics are the following:<\/p>\n<ul>\n<li>Google Chrome: 42.71%<\/li>\n<li>Firefox: 20.01%<\/li>\n<li>Internet Explorer 10: 9.88%<\/li>\n<li>Safari: 8.39%<\/li>\n<li>Internet Explorer 8: 8.04%<\/li>\n<li>Internet Explorer 9: 6.79%<\/li>\n<li>Opera: 1.03%<\/li>\n<li>Internet Explorer 7: 0.49%<\/li>\n<li>Internet Explorer 6: 0.22%<\/li>\n<li>Others: 2.44%<\/li>\n<\/ul>\n<p>Since we kept the <code>@font-face<\/code> rule inside the <code>@media<\/code> query, we&#8217;re still optimizing the website for all the browsers that do support both <code>@media<\/code> queries (<em>not<\/em> IE8 and lower) and <code>@font-face<\/code> inside <code>@media<\/code> queries. As we&#8217;ve seen, these are all the browsers but Internet Explorer. In conclusion, we&#8217;ve a proper optimization for 74.58% users. A good starting point!<\/p>\n<p>For Internet Explorer 6-9, the custom font will be loaded, as we wanted, using a conditional comment. So, to the previous percentage we can add another 15.54% which lead to a partial total of 90.12%.<\/p>\n<p>Regarding Internet Explorer 10, since we rely on a JavaScript method to load the stylesheet, the custom font is loaded for all those who have JavaScript enabled. I&#8217;m not able to provide any statistics on this but those with JavaScript disabled should not exceed the 0.5%.<\/p>\n<p>To wrap up, the font is applied <strong>in roughly the 99.5% of the cases<\/strong>. Another big success.<\/p>\n<div class=\"note\" id=\"better-approach-updated\">\n  <ins cite=\"http:\/\/flippinawesome.org\/2013\/07\/08\/solving-the-problem-of-font-face-inside-media-queries\/#comment-1880\" datetime=\"2013-07-18T15:37:00+01:00\"><\/p>\n<h4>Update Thanks to <a href=\"http:\/\/timkadlec.com\/\" target=\"_blank\">Tim Kadlec<\/a><\/h4>\n<p><q cite=\"http:\/\/flippinawesome.org\/2013\/07\/08\/solving-the-problem-of-font-face-inside-media-queries\/#comment-1880\">Most browsers won&#8217;t download web fonts unless needed. This means you can declare all of your @font-face rules outside of any media queries, and then only use that font in the font-stack for certain resolutions. The exception is still IE < 9, which will download the fonts regardless of use. Example:\n[css]\n@font-face {\n  font-family: &quot;some-font&quot;;\n}\n\n@media (min-width: 400px) {\n  body {\n    font-family: &quot;some-font&quot;, Georgia, serif;\n  }\n}\n[\/css]\n<\/q><\/ins><ins datetime=\"2013-07-19T15:45:00+01:00\"><\/p>\n<h4>Following Update by Me<\/h4>\n<p>After his comment, I deepened a little more and found that the terms &#8220;unless needed&#8221; should be clarified. His note is true for all major browsers only if you use a mobile-first approach. In fact, if you used a desktop-first approach, all the versions of IE (IE 10 and below) aren&#8217;t able to skip the download of a font, even if it won&#8217;t be really used. So, if you have in your stylesheet a code like this:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n@font-face {\r\n  font-family: &quot;some-font&quot;;\r\n}\r\n\r\n\/* DESKTOP-FIRST APPROACH *\/\r\ndiv {\r\n    font-family: &quot;some-font&quot;;\r\n}\r\n\r\n@media screen and (max-width: 768px) {\r\n    div {\r\n        font-family: Calibri;\r\n    }\r\n}\r\n<\/pre>\n<p>And you&#8217;re viewing the page from a device with a screen resolution equal to or less than 768px using a browser like Google Chrome, FireFox and others, the custom font won&#8217;t be downloaded because it isn&#8217;t really used, as shown by the following screenshot:<\/p>\n<p><a href=\"http:\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"127\" data-permalink=\"https:\/\/www.audero.it\/blog\/2013\/07\/02\/how-to-solve-the-problem-of-font-face-inside-media-queries\/chrome-does-not-load-font-for-mobile\/\" data-orig-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile.png\" data-orig-size=\"1260,731\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Chrome does not load font for mobile\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile-1024x594.png\" src=\"\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile-1024x594.png\" alt=\"Chrome does not load font for mobile\" width=\"640\" height=\"371\" class=\"aligncenter size-large wp-image-127\" srcset=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile-1024x594.png 1024w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile-300x174.png 300w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/chrome-does-not-load-font-for-mobile.png 1260w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>On the contrary, Internet Explorer will download it anyway, as shown by the following screenshot:<\/p>\n<p><a href=\"http:\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"128\" data-permalink=\"https:\/\/www.audero.it\/blog\/2013\/07\/02\/how-to-solve-the-problem-of-font-face-inside-media-queries\/ie-loads-font-for-mobile\/\" data-orig-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile.png\" data-orig-size=\"1263,723\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"IE loads font for mobile\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile-1024x586.png\" src=\"\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile-1024x586.png\" alt=\"IE loads font for mobile\" width=\"640\" height=\"366\" class=\"aligncenter size-large wp-image-128\" srcset=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile-1024x586.png 1024w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile-300x171.png 300w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/ie-loads-font-for-mobile.png 1263w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><small>As you can see, both the screenshots are in Italian but you can easily understand that while Chrome downloads just the page, IE downloads the page <em>and<\/em> the font.<\/small><\/p>\n<h4>Final Notes<\/h4>\n<p>To wrap-up, if your website already uses a mobile-first approach, follow the Tim Kadlec&#8217;s suggestion. Otherwise, if you&#8217;re using a desktop-first approach and don&#8217;t want to make heavy changes, you can follow the method explained in this article.<\/p>\n<p>In case you want to play with the demo I created to test the behavior I described, you can <a href=\"http:\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/07\/font-face-demo.zip\">download the font-face demo here<\/a>.<\/ins>\n<\/div>\n<h2>Conclusions<\/h2>\n<p>Custom fonts can give a nice look to a website and more and more developers and designers are using them. However, we don&#8217;t have to forget to optimize for mobile devices due to the increasing traffic coming from them. As we&#8217;ve seen in this article, using a mobile first approach for web fonts having <code>@font-face<\/code> inside <code>@media<\/code> queries, together with the use of a couple of tricks that target Internet Explorer, is possible to achieve both the goals. We can trust this technique because they&#8217;re safe and let us achieve a really high success percentages: 99.5% both for mobile users and desktop users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mobile connections aren&#8217;t as good as the usual desktop ones, therefore the download time of custom fonts can take up to several seconds. A good approach is to use the <code>@font-face<\/code> rule only for certain range of media and screens using <code>@media<\/code> queries. Unfortunately this approach doesn&#8217;t work for some browsers: all the versions of Internet Explorer and FireFox 10 and lower. This article discusses a solution for this problem that balances performances and hacks.<\/p>\n","protected":false},"author":1,"featured_media":90,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[16,17,19,48,15,18,11],"class_list":["post-56","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","tag-font-face","tag-media","tag-browser","tag-css","tag-css3","tag-internet-explorer","tag-web"],"jetpack_featured_media_url":"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/07\/font-face-icon-100x100.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9Or4e-U","jetpack-related-posts":[{"id":197,"url":"https:\/\/www.audero.it\/blog\/2013\/12\/23\/enhancing-the-abbr-element-on-mobile\/","url_meta":{"origin":56,"position":0},"title":"Enhancing the abbr Element on Mobile","author":"Aurelio De Rosa","date":"December 23, 2013","format":false,"excerpt":"Today's world runs fast, really fast. When writing, a lot of people use abbreviations and acronyms to save time that we can find spread all over the web. They are so used that an HTML tag, <abbr>, was created to identify them and help the semantic of web pages. In\u2026","rel":"","context":"In &quot;CSS&quot;","block_context":{"text":"CSS","link":"https:\/\/www.audero.it\/blog\/category\/css\/"},"img":{"alt_text":"The abbr tag unstyled","src":"https:\/\/i0.wp.com\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/12\/abbr-unstyled.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":612,"url":"https:\/\/www.audero.it\/blog\/2016\/11\/29\/most-used-npm-commands\/","url_meta":{"origin":56,"position":1},"title":"My most used npm commands","author":"Aurelio De Rosa","date":"November 29, 2016","format":false,"excerpt":"If you're a front-end or a JavaScript developer, you're surely using npm. npm is a registry where people publish their software. At the beginning, it was used to share JavaScript libraries and frameworks, but today you can also find CSS frameworks, font icons, and much more. In addition to being\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/www.audero.it\/blog\/category\/javascript\/"},"img":{"alt_text":"npm","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/11\/npm.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/11\/npm.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/11\/npm.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/11\/npm.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":170,"url":"https:\/\/www.audero.it\/blog\/2013\/09\/16\/15-tips-to-improve-your-jquery-selectors\/","url_meta":{"origin":56,"position":2},"title":"15 Tips to Improve Your jQuery Selectors","author":"Aurelio De Rosa","date":"September 16, 2013","format":false,"excerpt":"Who don't know what jQuery is and how it can help in developing a web project. Based on the latest statistics, jQuery is used on ~60% of the Quantcast Top 100k websites. We use it constantly and sometimes we tend to think it's almost magic, that jQuery is capable to\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/www.audero.it\/blog\/category\/javascript\/"},"img":{"alt_text":"jquery logo","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2013\/09\/jquery-logo.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":329,"url":"https:\/\/www.audero.it\/blog\/2014\/09\/19\/resources-beginner-front-end-developers\/","url_meta":{"origin":56,"position":3},"title":"Resources for Beginner Front-end Developers","author":"Aurelio De Rosa","date":"September 19, 2014","format":false,"excerpt":"Few weeks ago I received an email from a developer asking me for suggestions on how to delve into the front-end world. After having replied to this email, I thought that it'd have been nice to share the same suggestions on my blog. That's exactly what you'll find in this\u2026","rel":"","context":"In &quot;Discussions &amp; Opinions&quot;","block_context":{"text":"Discussions &amp; Opinions","link":"https:\/\/www.audero.it\/blog\/category\/discussions-opinions\/"},"img":{"alt_text":"html5 css3 javascript logos","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/09\/front-end-stack.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/09\/front-end-stack.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/09\/front-end-stack.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/09\/front-end-stack.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/09\/front-end-stack.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":379,"url":"https:\/\/www.audero.it\/blog\/2015\/05\/27\/trick-of-the-day-sass-mixin-or-placeholder\/","url_meta":{"origin":56,"position":4},"title":"Trick of the day: Sass, Mixin or Placeholder?","author":"Aurelio De Rosa","date":"May 27, 2015","format":false,"excerpt":"Without any doubt, two of the best and most useful features of Sass are mixins and placeholders. A mixin is a directive similar to a function in programming languages in that you can pass arguments. However, a mixin outputs CSS code instead of returning a value as the result of\u2026","rel":"","context":"In &quot;Trick of the day&quot;","block_context":{"text":"Trick of the day","link":"https:\/\/www.audero.it\/blog\/category\/trick-of-the-day\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":404,"url":"https:\/\/www.audero.it\/blog\/2015\/07\/14\/trick-of-the-day-css-scroll-snap-points\/","url_meta":{"origin":56,"position":5},"title":"Trick of the day: CSS Scroll Snap Points","author":"Aurelio De Rosa","date":"July 14, 2015","format":false,"excerpt":"When you create a gallery, you usually want to implement a feature where every time the user clicks (or press the ENTER key) on an arrow, you move straight to the next (or previous) image. This effect can also be used to create a website that has several sections, each\u2026","rel":"","context":"In &quot;Trick of the day&quot;","block_context":{"text":"Trick of the day","link":"https:\/\/www.audero.it\/blog\/category\/trick-of-the-day\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2015\/07\/css-scroll-snap-points.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2015\/07\/css-scroll-snap-points.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2015\/07\/css-scroll-snap-points.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2015\/07\/css-scroll-snap-points.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2015\/07\/css-scroll-snap-points.png?resize=1050%2C600&ssl=1 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":61,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":543,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/media\/90"}],"wp:attachment":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}