3 quick ways to add fonts to your React app

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Most welcome, Shobit.
Hi, The content is written perfectly to solve the exact solutions that I needed. Congrats Tapas Adhikary 👏
I tried importing the exact way you mentioned to import into the index.js file, and still returns an error not found when the path matches.
import './assets/fonts/AvenirNextLTPro-Regular.otf';
in /src/index.js
Module not found: Error: Can't resolve './assets/fonts/AvenirNextLTPro-regular.otf' in 'MyPathToTheProject/src'
Hi Charles, it should work as long as the import is correct and the project infra supports the import keyword.
Did you check the GitHub project I mentioned above? That got all the examples including this one.
Great tactics mate! This was much quicker than watching a 5-minute youtube video, thank you.
You are most welcome. Thanks a lot 🌟
Sweet and simple! I use @font-face most often but nice to know the other quick alternatives :D
Find all my ReactJS articles under one roof. Don't forget to check out [ReactPlay](https://reactplay.io) for practising ReactJS concepts.
An HTML form allows users to enter data using input fields that accept text, password, email, number, color, telephone number, date, etc. Users can type lengthy texts in the textarea, can select one or many items from a select box, can check or unche...
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
In HTML, font is used to specify the font face, size, and typography. You can add fonts to your React application in different ways. This article explains 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. You can choose to use the examples in any other code structure too.
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,
+Select this style buttonUse on the web and copy the code under the <link> sectionindex.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, <link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Hanalei+Fill&display=swap" rel="stylesheet">
.font-link {
font-family: 'Hanalei Fill', cursive;
}
Here we are using the same font-family that linked in the above step.
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,

The Web Font Loader helps you to load fonts from Google Fonts, Typekit, Fonts.com, and Fontdeck, 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.
webfontloaderyarn add webfontloader # Or, npm i webfontloader
webloader to your componentimport WebFont from 'webfontloader';
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. useEffect(() => {
WebFont.load({
google: {
families: ['Droid Sans', 'Chilanka']
}
});
}, []);
Here we are loading fonts, 'Droid Sans' and 'Chilanka'.
className or style attribute. To use with the className attribute, create a CSS class in the .css file,.font-loader {
font-family: 'Chilanka';
}
Then, in the component's render() method,
<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,
<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,

Read more about the Web Font Loader from here.
@font-faceIn 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. The fonts must be downloaded locally and packaged within the app in these situations.
@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.
index.js file. import './fonts/Goldman/Goldman-Bold.ttf';
index.css file add,@font-face {
font-family: "GoldmanBold";
src: local("GoldmanBold"),
url("./fonts/Goldman/Goldman-Bold.ttf") format("truetype");
font-weight: bold;
}
App.css file that uses this family name..font-face-gm {
font-family: "GoldmanBold";
}
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,

Here is something for you:
Learn React, Practically
I've been developing professional apps using ReactJS for many years now. My learning says you need to understand ReactJS fundamentally, under the hood to use it practically.
With that vision, I have created a ReactJS PlayLIst that is available for the developer community for FREE. Take a look:
I 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.

All the source code used in this article is in my GitHub repository.
Let's connect. You can @ me on Twitter (@tapasadhikary) with comments, or feel free to follow. You may also enjoy reading,