
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use Google Fonts in React
Google Fonts is a widely used library of open-source web fonts that provides a vast collection of typefaces to enhance the visual appeal of web applications. It allows developers to easily integrate high-quality fonts into their React applications.
In this React Google Fonts article, we will cover three different methods to add custom fonts (Google font) to your React application. Before adding custom fonts to your project, you should be familiar with the following concepts: NPM & Node.js, React.js, and Styled Components.
Why Use Google Fonts?
Google Fonts offers several benefits:
- Wide Selection: Access to over 1,000 fonts.
- Free to Use: No licensing fees.
- Easy Integration: Simple to incorporate into any web project.
- Performance: Optimized for fast loading and performance.
How to Use Google Fonts in React?
To use Google fonts in React we can manually add the google fonts cdn using link tag, dynamically add it using styled components, and also with the help of @import rule in CSS.
Approaches to Use Google Fonts in React
- Manually Adding Google Fonts
- Importing Google Fonts in JavaScript
- Using Google Fonts with Styled Components
Manually Adding Google Fonts
To manually add Google Fonts in React, we include the link tag with the Google Fonts URL in the head section of public/index.html, then apply the font using CSS in React components.
Step 1: Select a Google Font from their website.
Visit the link- Browse Fonts - Google Fonts and select the font you want to add. Here we are using the montserrat Font for instance.
Step 2: Integrating the Font into React
Copy the link tag of the font into the public/index.html file of your React app to use in your project:
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet">
Step 3: Use the Imported Fonts
Apply the font in your CSS file:
body { font-family: 'Montserrat', sans-serif; }
Example
The following example demonstrates how you can manually add google fonts.
<head> <link href="https://fonts.googleapis.com" rel="preconnect" > <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet"> </head>
Common Errors While Using Custom Font in React
Some common errors faced by users while adding custom fonts to their React projects are:
1. Font Not Found or Not Loading
Custom font may not be applied to the application because of font file error. Check if the font file path is correct and if the font file is imported correctly.
2. Using Unsupported Font Formats
React primarily supports .ttf and .otf formats. Make sure to convert unsupported file types before adding them to your project.
3. Font Loading Delay
Initially, the application displays default fonts and later changes it to the custom font. You can try optimizing fonts or preloading them.
4. Performance Issues
Large custom font files can impact the application's performance. Make sure to compress large font files before using or use font optimization.
5. Platform Specific Errors
Each platform (android and IOS ) has different font handling operations. Some fonts may not be compatible with Android or IOS. Follow specific guidelines for each platform.
Importing Google Fonts in JavaScript
To load fonts dynamically in React components, you can import Google Fonts inside your JavaScript file.
Example:
Here is an example of how we can import Google Fonts in JavaScript:
import './App.css'; // OR import in JS (not recommended for large projects) const link = document.createElement('link'); link.href = "https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap"; link.rel = "stylesheet"; document.head.appendChild(link); function App() { return ( <div style={{ fontFamily: "'Roboto', sans-serif" }}> Hello, this is using Google Fonts! </div> ); } export default App;
Using Google Fonts with Styled Components
If you are using a styled-components, then you can use Google fonts as explained in the example given below:
Example
Here is an example to use Google fonts with styled components:
import { createGlobalStyle } from 'styled-components'; const GlobalStyle = createGlobalStyle` @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap'); body { font-family: 'Roboto', sans-serif; } `; function App() { return ( <> <GlobalStyle /> <div> Hello, styled-components with Google Fonts!</div> </> ); } export default App;
Conclusion
In this article 'to use Google Fonts in React', we have discussed three different approaches. These approaches are: by manually adding Google fonts, by importing in javascript and, by using with styled components. Any of the approaches can be used for completing the task.