JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Setting global font family in React

Author:JIYIK Last Updated:2025/03/16 Views:

To set a global font family in React, you need to set the style on the html element in the index.cssfont-family file and then import the file into your index.js file. The global CSS should be imported in the index.js to ensure that it is loaded on all pages of your React application.

Here is an example of setting CSS properties in the index.css file.font-family

body,
html {
  font-family: Roboto, Helvetica, sans-serif;

  /* 👇️ 或使用操作系统默认字体 👇️ */
  /* font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif; */
}

Here is how we can import the index.css file in the index.js file .

// 👇️ import css
import './index.css';

import {createRoot} from 'react-dom/client';
import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <App />
);

The above example assumes that our index.css file is in the same directory as our index.js file.

When importing a global CSS file in React, it is best to import the CSS file into the index.js file.

The index.js file is the entry point of your React application, so it will always be run.

On the other hand, if we import a CSS file into a component, the CSS styles may be removed after the component is unmounted.

If we have already downloaded the font and need to load it from a local file, simply create a fontface in our src folder and move our font there.

Now we can import them into our index.css file.

The following examples assume that we have downloaded the Poppins font into the src/fonts directory.

body,
html {
  font-family: 'Poppins', sans-serif;
}

@font-face {
  font-family: 'Poppins';
  src: local('Poppins'), url(./fonts/Poppins-Regular.ttf) format('truetype');
}

@font-face {
  font-family: 'Poppins';
  font-weight: 900;
  src: local('Poppins'), url(./fonts/Poppins-Bold.ttf) format('truetype');
}

@font-face {
  font-family: 'Poppins';
  font-weight: 900;
  src: local('Poppins'), url(./fonts/Poppins-Black.ttf) format('truetype');
}

For .ttfthe format, we pass truetype to format()the function.

For .woffthe format, we pass woff to format()the function.

For .otfthe format, we pass opentype to format()the function.

We can download fonts by selecting a font from Google Fonts and clicking the Download All button.

If we want to set the global font-family based on external styles, e.g. from google fontsa CDN, make sure to load the styles in the public/index.html file.

For example, with google fonts, we can click on the font of our choice and then click on “Select this style” and we will get a link that we can add to the head tag of our index.html file.

Setting global font family in React

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- 👇️ loading font-family from google fonts -->

    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Square+Peg&display=swap"
      rel="stylesheet"
    />

    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

After copying the link tag to the <link> section of the index.html file in the public/head directory , we still need to set the <link> property in the index.css file.font-family

body, html {
  font-family: 'Square Peg', cursive;
}

font-familyThe value of the attribute depends on the specific font we selected. We should be able to find it below the Get Link tag.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:85 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial