johannchopin
johannchopin

Reputation: 14873

Import Google Font using real url to font file

I use GoogleFonts in my project and for some reason I can't import a font without using the 'embed' system.

For example if you want to import the Titan One font you can achieve it using:

@import url('https://fonts.googleapis.com/css2?family=Titan+One&display=swap');

font-family: 'Titan One', cursive;

This url actually refers to a css file declaring the font:

/* latin-ext */
@font-face {
  font-family: 'Titan One';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Titan One'), local('TitanOne'), url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.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: 'Titan One';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Titan One'), local('TitanOne'), url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjClDgm-khykw.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 I decide to skip the @import step to directly import the font from its static url (here: https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.woff2):

<!DOCTYPE html>
<html>
<head>
  <style>
    @font-face {
      font-family: 'Titan One';
      font-style: normal;
      font-weight: 400;
      src: url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.woff2);
    }
    
    h1 {
      font-family: 'Titan One';
    }
  </style>
</head>

<body>

  <h1>The @font-face Rule</h1>

</body>

</html>

But as you can see, the font does not apply to my title. Do you have any idea where the mistake is?

Upvotes: 0

Views: 1272

Answers (1)

Temani Afif
Temani Afif

Reputation: 273944

You need to consider the latin file not the latin-ext but I recommend that you copy the whole Google file to make sure it works fine because the unicode-range is important

The unicode-range CSS descriptor sets the specific range of characters to be used from a font defined by @font-face and made available for use on the current page.

The purpose of this descriptor is to allow the font resources to be segmented so that a browser only needs to download the font resource needed for the text content of a particular page. ref

@font-face {
  font-family: 'Titan One';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjClDgm-khykw.woff2);
}

h1 {
  font-family: 'Titan One';
}
<h1>The @font-face Rule</h1>

Upvotes: 1

Related Questions