YogeshChauhan . com

How to host Google fonts on your server and add them using CSS?

in CSS & Misc on February 19, 2021

Everyone uses Google Fonts. Well almost everyone! Look at their analytics.

They are one of the most used third-party fonts across websites. And why not to use it? It’s free and looks way better than the regular fonts.

Google Fonts have almost 1000 free and unique font families to choose from.

Bue when you test your site’s speed in Google Pagespeed insights or lighthouse or any other similar tools, they might show you warnings like this:

A screenshot of the Lighthouse Ensure text remains visible during webfont loads audit
Source: Lighthouse

Sometimes the warnings are not straight forwards but let me explain how Google fonts load into the website. Google fonts are hosted externally if you just use the link from Google to import them.

Many times externally hosted fonts or other assets are heavy to load. They add additional page weight. Also, your website will have to make additional requests to load them.

Imagine what would happen when we add multiple Google Fonts via external links!

I thought of loading the fonts from my own server rather than loading them from Google’s CDN. It actually loaded the fonts faster.

Let’s understand how the fonts loading from Google’s CDN works.



...
<link href="https://fonts.googleapis.com/css2?family=Roboto&#038;display=swap" rel="stylesheet">
...


We can also add more weights like this:



...
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&#038;display=swap" rel="stylesheet">
...


If you open that link into a browser it’ll show you the fonts CSS code:


/* cyrillic-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu5mxKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* greek-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu7mxKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+1F00-1FFF;
}
/* greek */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4WxKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+0370-03FF;
}
/* vietnamese */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu7WxKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu7GxKKTU1Kvnz.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}


So, the browser connect to the domain fonts.googleapis.com first and download the CSS. After that it connects to fonts.gstatic.com to download the fonts.

Making an HTTPS connection takes few microseconds (sometimes seconds) and in this case, it makes 2 HTTPS requests.

If we just host the fonts on our own server then the browser doesn’t need to make those HTTPS requests to other domains.

There are different ways we can download the Google Fonts. First one is from Google Fonts directly. After you the fonts, you’ll get an option to download the fonts on the top right corner. Click on it and download it.

Second option is from this Github repo. It automatically generates CSS file for you so that you don’t need to worry about adding the CSS code.

Here is the third option. This website gives you option to select the fonts and makes the CSS file as well.

Make sure you don’t forget to add font-display: swap;

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to refresh a page using PHP at regularly occurring intervals?PHPHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQLCheck If a String Contains a Specific Word in PHPPHPHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLWhat is a component helper function in React?ReactHow to add Date and Time picker in HTML and get the values using JavaScript?HTML