1. Download your fonts
If you are using Google Fonts, a good tool for downloading the fonts used is the Google Webfonts Helper. Many type foundries also give the option of downloading web font files. For this tutorial, we will be using Roboto from Google Fonts. Select the font weights and download the font files.
2. Host your fonts on your Shopify store.
Simply upload the font files to your Shopify files folder.
The assets folder is also an option but I find the files folder a more suitable place to keep fonts.
Upload font files
3. Create the CSS
Google Webfonts Helper will auto-generate the @font-face CSS styles for the selected fonts.
Create a file called css-fonts.liquid
in your Snippets folder.
Paste the generated @font-face CSS styles.
Replace the URLs with the URLs of your Shopify hosted font files.
Note: the CSS font declarations are between <style></style>
tags and we removed the query string from the Shopify hosted files URL.
Sample css-fonts.liquid
4. Include the fonts in theme.liquid
Edit the theme.liquid
file located in your Layout folder and include the newly created css-fonts.liquid inside the <head></head>
element:
{% render 'css-fonts' %}
5. Include the preload directive in theme.liquid
At this point you should have: fonts downloaded and uploaded to Shopify files, a Snippets/css-fonts.liquid file containing the CSS @font-face declarations
Edit theme.liquid file located in your templates folder and add the following code in inside the <head></head>
element:
Note: the preload directives should be added above {% render 'css-fonts' %}
. Add them early in the DOM so the browser can start the download fast.