Note: in this tutorial I am using hjs-webpack and React. The concepts here can be easily adapted to full blown webpack by copying hjs-webpack's base config.
Moving on!
Why @font-face is an exception to the convention of inline styles
Often in React apps, we shy away from global styles. We favor components either styling themselves or allowing parent components to pass in styles via props as to maximize reusability.
Google Fonts use CSS's @font-face via a link tag or an @import statement in order to bring fonts in from the web.
Using @font-face in CSS is an exception as it cannot be inlined. Thus, we must make an exception and use a global stylesheet for this case.
When using Webpack, we no longer rely on a CDN for our resources. Instead we use local resources. So how do we use Google Fonts locally and define global styles in our React app?
Getting started
Step 1: Install loaders
In order to load fonts, we're going to need some webpack loaders. These loaders will look for the fonts we need on the filesystem. You're probably used to using a CDN when you use Google fonts. Google's CDN automatically provides the right font type based on the file extension you provide. Webpack can do the same kind of things when using the correct loaders.
hjs-webpack is already configured to lazily use the following loaders but it is up to you to actually opt-in to using them.
To opt in, we use npm install. Remember that loaders should always be installed with --save-dev
and not --save
since they are not distributable code.
Create folder structure
I'll assume your source code lives in a src
directory in the root of your project. I'll also assume your main React app component is in src/App.jsx
. In your src
directory create a directory called assets
. Inside of that directory, create two directories, one called fonts
, the other called styles
.
Your initial structure will look something like this:
Download and extract font files
To find and download Google Fonts files you can use this fantastic tool which will let you download preview and download the fonts in a variety of font formats (all of which are needed to support older browsers):
https://google-webfonts-helper.herokuapp.com
For this example I've downloaded Open Sans. Once you have downloaded the zip file, extract the font files to src/assets/fonts
. If you want more than one font in your application, just put them in src/assets/fonts
as well.
Create and import the global styles
Now that we have some loaders installed, and our file structure created we can use the loaders to import global styles. Remember that this is an exception to the convention of using inline styles. Certainly you could put tons of CSS in the following file, but generally I'd try to keep it as lean as possible.
In src/assets/styles
, create a file called global.css
and place the following code in it:
The code above defines a new font and applies that font to the body tag of our application. You should omit body { font-family: 'Open Sans';}
if you do not want to apply your font to the body tag.
Next edit src/App.jsx
to import the global.css
file using the following:
Node: you only need to import this file once in your App.jsx. You do not need to import it in every component, even if that component uses a font listed in global.css. This is still CSS so the styles from global will cascade down into your components.
If you want to use more than one Google Font (for example Lato), just put the additional @font-face CSS from the Google Webfont Helper into global.css. You can then use the additional font in a component's render method using
<p style={{fontFamily: 'Lato'}}>Lorem ipsum</p>.