Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including Google Fonts link or import?

What is the preferred way of including Google Fonts on a page?

  1. Via the <link> tag
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Judson:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
    
  2. Via import in a stylesheet
    @import url('https://fonts.googleapis.com/css2?family=Kameron:wght@400;700&display=swap');
    
  3. Using the Web Font Loader
like image 830
kajo Avatar asked Oct 14 '22 21:10

kajo


People also ask

Should I use import or link for Google Fonts?

Should I use <link> or @import ? # Loading Google fonts in the HTML reduces the critical request depth and speeds up page load Always import your fonts from HTML, not CSS.

Where do I put a Google Font link?

To add google fonts, search for the google fonts, select the font category and family, then select the font style of your choice. Once you select the font, “copy the font link”, from the “selected families windows” and paste it in the head section of the HTML file.

Is it better to import font in CSS or HTML?

CSS files support web fonts basically in every browser by naming the font-family declaring the source file. The best way is for me is to include it into my . css file and linking it into my header.


1 Answers

For 90%+ of the cases you likely want the <link> tag. As a rule of thumb, you want to avoid @import rules because they defer the loading of the included resource until the file is fetched.. and if you have a build process which "flattens" the @import's, then you create another problem with web fonts: dynamic providers like Google WebFonts serve platform-specific versions of the fonts, so if you simply inline the content, then you'll end up with broken fonts on some platforms.

Now, why would you use the web font loader? If you need complete control over how the fonts are loaded. Most browsers will defer painting the content to the screen until all of the CSS is downloaded and applied - this avoids the "flash of unstyled content" problem. The downside is.. you may have an extra pause and delay until the content is visible. With the JS loader, you can define how and when the fonts become visible.. for example, you can even fade them in after the original content is painted on the screen.

Once again, the 90% case is the <link> tag: use a good CDN and the fonts will come down quick and even more likely, be served out of the cache.

For more info, and an in-depth look at Google Web Fonts, check out this GDL video

like image 409
igrigorik Avatar answered Oct 17 '22 10:10

igrigorik