Languages
[Edit]
EN

React - import Google Fonts

0 points
Created by:
Kadeem-Craig
516

In this article, we would like to show you how to use Google Fonts with React.

Quick solution:

1. Select Font and copy the <link> element from the Use on the web section.

2. Paste the <link> element into the <head> section of the index.html file in the public/ folder.

3. Now you can use the font inside .css files in your app.

 

Detailed solution with examples

1. Go to the Google Fonts page, search and select the font. In this example, we select the Roboto font.

React - choose Google Font
React - choose Google Font

2. Select the font style and open View your selected families.

React - select Google Font style
React - select Google Font style

3. Select and copy the link elements.

React - copy Google Font code
React - copy Google Font code

4. Paste link elements to the <head> of your index.html file.

React - paste Google Font code into index.html file
React - paste Google Font code into index.html file

5. Assign the CSS rules into the element where you want to place the font.

React - Google Font in .css file
React - Google Font in .css file

6. Import the CSS style into the .js file and run the app.

React - import .css file with Google Font
React - import .css file with Google Font

Result:

React - Google Fonts result
React - Google Fonts result

Project files

index.html

<!DOCTYPE html>

<head>
    <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=Roboto:wght@100&display=swap" rel="stylesheet">
</head>

<body>
    <div id="root"></div>
</body>

</html>

App.css

.roboto-mono {
    font-family: 'Roboto', sans-serif;
}

App.js

import React from 'react';
import './App.css';

const App = () => {
    return (
        <div>
            <div className="roboto-mono">Example text...</div>
        </div>
    );
};

export default App;

See also

  1. CSS - Google Fonts

References

  1. Browse Fonts - Google Fonts

Alternative titles

  1. How to use Google Fonts with React?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join