![]() |
In React Setting a global font family means defining a specific font style to be used throughout a website or application, ensuring consistent typography across all elements and text content. When setting a global font family, developers specify a single font that will be universally applied across all components in their React application. This not only ensures consistent visual appeal but also simplifies the design process by eliminating the need to individually style fonts for each component. Prerequisites:These are the approaches to set Global Font Family in React JS Table of Content Steps to Create the React ApplicationStep 1: Create a react application by using this command npx create-react-app font-family-app
Step 2: After creating your project folder, i.e. font-family-app, use the following command to navigate to it cd font-family-app
Project StructureApproach 1: Using Google Fonts CDNIn this appraoch, we are integrating external fonts from Google Fonts CDN into the HTML file of a project. By adding a link in the <head> section of the HTML file (like this: https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap), developers can make the specified font globally accessible. This saves time during development and allows for a wide selection of fonts from Google’s extensive library to be used across the React app.
public/index.html <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@700&display=swap" rel="stylesheet" />
Example: In this above public/index.html, we’re linking to the “Roboto” font family using the Google Fonts CDN. Different weights, including bold (700), are available. To enhance the user experience, we utilize the display=swap attribute. This ensures that while the Google Font is loading, the text will be displayed with the default font. Javascript
CSS
HTML
Steps to run: To open the application, use the Terminal and enter the command listed below. npm start
Output: ![]() Setting Global Font Family In React Example 1 Approach 2: Using Emotion LibraryEmotion, the CSS-in-JS library for React, offers a seamless way to define and implement global styles. By leveraging Emotion’s Global and css functions within a custom component, we can efficiently set a universal font family for our React application. Step to Install the emotion Library: npm i @emotion/react @emotion/styled
Open the public/index.html file in your React project.In the <head> section of the HTML file, add a link to the Google Fonts CDN with the desired font family.
Example: In this example, the React application sets global styles using Emotion CSS-in-JS. It styles the body, defining fonts, colors, and background, ensuring consistent styling for the entire app. Javascript
HTML
Steps to run: To open the application, use the Terminal and enter the command listed below. npm start
Output: This output will be visible on the http://localhost:3000/ on the browser ![]() Set A Global Font Family In React Using Emotion |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |