Adding New Fonts in React JS

Overview

We are going to explore how to add new fonts in React js project and for that we should know how to add styles using style.css file. Adding new fonts is quite similar to adding new font in CSS but there are some minor differences which we are going to explain.

From these concept the react is able to add new fonts with same facilities which is provided by styles. Let’s understand how it works and what is the scenario behind it.

Creating A React Project

First we should have one React JS Project ,if we don’t have then create new one:

Example-

note-(npx comes with npm 5.2+ and higher.so in this app we used NPM 5.6.0.
It will generate a react app, name will be my-react-app.And here is the folder structure as follows:

Analyse the changes to be

Now we can see that Public folder,so here we are going to add all our new files related to that new fonts with different extensions or type. And we should have all those files in one common folder (like fonts folder we can create and put all the fonts file inside this fonts folder)

Example-

Now we need to add a style.css file inside our app(Folder)>style.css, which will contain following lines of code and here we are taking example code like how we can add New font (Miriam) in our app .

Inside style.css we can add our new fonts as :

Here,we can see how we are going to add ‘Miriam’ font to our project and inside this file we can change font-style and font-weight also. So for that we need to have basic idea about the files we have in fonts folder(all fonts files). So in fonts folder we have different types of files with their different types of extensions which will support to apply our font on every types of environment and for adding those files for our app we need to give relative path for them also in our style.css file.

Note– Here we can see we have some different types of files with different types of extensions too. So all of these things we will explain once we will define the loader of these files.

Process to add new Font

There are following steps to add new fonts:

Step-1=>

run npm install –save-dev url-loader inside our app to load files related to new font. The url-loader works like the file-loader, but can return a DataURL if the file is smaller than a byte limit.

Step-2=>

Now inside our Webpack.dev.js we need to add following lines of codes:

Inside module.exports>loaders(Array) we need to add our loader to load files for that new font:

Description of Loaded Files

This url-loader will load different types of files to add new font about which we are as follows:

Embedded Open Type (EOT) files-
EOT fonts are designed by Microsoft.it is a compact form of Open Type of Fonts. It is designed to use Embedded fonts on Web.

Web Open Font Format (WOFF) files-
WOFF fonts basically support all types of browsers.
Web Open Font Format (WOFF2) files-
WOFF2 is the next generation of WOFF.

TrueType Font (TTF) files-
TrueType Fonts format was developed by Apple and Microsoft as a response to the PostScript font format.

Scalable Vector Graphics font (SVG) files-
SVG fonts are defined using the SVG’s ‘font’ element. In iPhone and iPad we should use SVG fonts.

Conclusion : –

So we have done all about adding new font in our application now we can use our new font(new added) as :
fontFamily:”Miriam”.

NOTE:
So it was for our entire app but if we want to add new font for a single component then we have to do all the things we have done except that style.css file . Now in this case we will add that style.css file for that component only not for entire app. And rest of steps will be same and we can use that CSS as:
fontFamily:”Miriam”.

mm

Surya Singh Frontend Developer

I am Surya Pratap Singh, working as a Frontend Developer at Yudiz Solutions Pvt. Ltd. - a leading mobile app, mobile games and Web Development Company.Coding is my passion and I like to learn new technologies.

Comments are closed.

Top