3 quick ways to add fonts to your React app
Tags: javascript, react, css, codenewbie
Published: 2021-01-11 08:37:21

In HTML, `font` is used to specify the font face, font size, typography of the text. You can add fonts to your React application in different ways. This article aims to explain three quick ways of adding fonts to your React app.

> All the examples in the article assumes the React code structure provided by the [create-react-app](https://create-react-app.dev/). You can choose to use the examples in any other code structure too.

# ✨ Using the Font link

We can link to any fonts hosted online using the `<link>` tag inside an HTML file. Let's take an example of applying `Google Fonts` using the `<link>` tag.

*   Go to \[https://fonts.google.com/]\(go to https://fonts.google.com/).

*   Click on a Font of your choice,

> ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610093815826/jvmbLIxJv.png?border=1,CCCCCC\&auto=compress)

*   Click on the `+Select this style` button

> ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610094002931/zLVRpQUZQ.png?border=1,CCCCCC\&auto=compress)

*   Go to the section, `Use on the web` and copy the code under the `<link>` section

> ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610094238924/BodHigyQO.png?border=1,CCCCCC\&auto=compress)

*   Go to the `index.html` file of your project. If your app is based on the `create-react-app`, you will find it under the `public` folder. Paste the copied lines inside the `<head>` section. Here is an example,

```html
 <link rel="preconnect" href="https://fonts.gstatic.com">
 <link href="https://fonts.googleapis.com/css2?family=Hanalei+Fill&display=swap" rel="stylesheet">
```

*   Go to your CSS file and add a style like,

```css
.font-link {
   font-family: 'Hanalei Fill', cursive;
 }
```

Here we are using the same font-family that linked in the above step.

*   Last, you can add this style anywhere in your React component.

```js
const FontLink = () => {
   return(
       <div className="card">
           <span className="font-link">
               This is with Font Link. We are linking the fonts from the Google Fonts.
           </span>
       </div>
   )
};

export default FontLink;
```

Please note, we are using the class name with the `<span>` element in the React component.

This is how the component may look like,

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610100186397/ipOeEMaGI.png?border=1,CCCCCC\&auto=compress)

# ✨ Using the Web Font Loader

The [Web Font Loader](https://github.com/typekit/webfontloader) helps you to load fonts from  [Google Fonts](http://www.google.com/fonts/), [Typekit](http://www.typekit.com/), [Fonts.com](http://www.fonts.com/), and [Fontdeck](http://fontdeck.com/), as well as self-hosted web fonts. It is co-developed by Google and Typekit.

Let us see how to load multiple fonts from Google Fonts and use them in a React component.

*   Install `webfontloader`

```shell
yarn add webfontloader # Or, npm i webfontloader
```

*   Import `webloader` to your component

```js
import WebFont from 'webfontloader';
```

*   Load the desired fonts using the font name. It is better to use the `useEffect` hook and let it run once when the component loads. As the fonts need to be loaded just once in the app, you can load them in the `index.js` file.

```js
 useEffect(() => {
   WebFont.load({
     google: {
       families: ['Droid Sans', 'Chilanka']
     }
   });
  }, []);
```

Here we are loading fonts, 'Droid Sans' and 'Chilanka'.

*   Now you can use these fonts in a React component using the `className` or `style` attribute. To use with the `className` attribute, create a CSS class in the .css file,

```css
.font-loader {
   font-family: 'Chilanka';
}
```

Then, in the component's `render()` method,

```html
 <div className="font-loader">
     This is with Web Font Loader using the class attribute. 
     We are loading the <u><b>Chilanka</b></u> font from the Google Fonts.
 </div>
```

With the `style` attribute,

```html
 <div style={{fontFamily: 'Droid Sans'}}>
    This is with Web Font Loader using the style attribute. 
    We are loading the <u><b>Droid Sans</b></u> fonts from the Google Fonts.
 </div>
```

This is how the component may look like,

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610100263810/n2wh4dP3m.png?border=1,CCCCCC\&auto=compress)

Read more about the `Web Font Loader` [from here](https://github.com/typekit/webfontloader).

# ✨ Using `@font-face`

In some situations, you may not be allowed to connect to a font repository online and link/load it. A classic example is, your app users use `intranet` and they have restricted access to the internet. In these situations, the fonts must be downloaded locally and packaged within the app.

`@font-face` is a CSS rule to define a font name by pointing to a font with a URL.

*   Create a folder called `fonts` under `src`.
*   Download the required fonts into the `src\fonts` folder. In this example, we have downloaded two fonts, `Goldman` and `Lobster`.

> ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610098339208/2v7UqM-Yb.png?border=1,CCCCCC\&auto=compress)

*   Next, import the fonts into the `index.js` file.

```js
 import './fonts/Goldman/Goldman-Bold.ttf';
```

*   In the `index.css` file add,

```css
@font-face {
 font-family: "GoldmanBold";
 src: local("GoldmanBold"),
  url("./fonts/Goldman/Goldman-Bold.ttf") format("truetype");
 font-weight: bold;
}
```

*   Now add a class name in the `App.css` file that uses this family name.

```css
.font-face-gm {
  font-family: "GoldmanBold";
}
```

*   Use this class name in your React component,

```js
const FontFace = () => {
   return(
       <div className="card">
           <div className="font-face-gm">
               This is using Font Face. 
               We are linking the <u><b>Goldman</b></u> font from the Google Fonts.
           </div>
       </div>
   )
}
export default FontFace;
```

This is how the component may look like,
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610100305057/t-3MHunUd.png?border=1,CCCCCC\&auto=compress)

# Before we end...

Hope it was useful. Please Like/Share so that it reaches others as well. But, a few more points before we finish,

*   All the mechanisms discussed here are also applicable to a vanilla JavaScript application.
*   We can use multiple fonts in one app.
    ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610102041610/3NTXjef61.png?border=1,CCCCCC\&auto=compress)
*   All the source code used in this article is in my GitHub repository.

{% github atapas/font-loader no-readme %}

Let's connect. You can @ me on [Twitter (@tapasadhikary)](https://twitter.com/tapasadhikary) with comments, or feel free to follow. You may also enjoy reading,

---
# Other articles of mine that you might like
- [The definitive guide to JavaScript Debugging [2021 Edition]](https://blog.greenroots.info/the-definitive-guide-to-javascript-debugging-2021-edition)
- [Princess Finder using React, ml5.js, and Teachable Machine Learning](https://blog.greenroots.info/princess-finder-using-react-ml5js-and-teachable-machine-learning)
- [How to create React form with a single change event handler?](https://blog.greenroots.info/how-to-create-react-form-with-a-single-change-event-handler)
- [I've completed a HackFest project, here is what I've learned](https://blog.greenroots.info/ive-completed-a-hackfest-project-here-is-what-ive-learned)
- [What to expect from your performance appraisal meeting?](https://blog.greenroots.info/what-to-expect-from-your-performance-appraisal-meeting)