score:-2

in some cases your font resource maybe somewhere in your project directory. so you can load it like this using scss

 $list: (
      "black",
      "blackitalic",
      "bold",
      "bolditalic",
      "italic",
      "light",
      "lightitalic",
      "medium",
      "mediumitalic",
      "regular",
      "thin",
      "thinitalic"
    );
    
    @mixin setrobotofonts {
      @each $var in $list {
        @font-face {
          font-family: "roboto-#{$var}";
          src: url("../fonts/roboto-#{$var}.ttf") format("ttf");
        }
      }
    }
@include setrobotofonts();

score:-1

it could be the self-closing tag of link at the end, try:

<link href="https://fonts.googleapis.com/css?family=bungee+inline" rel="stylesheet"/> 

and in your main.css file try:

body,div {
  font-family: 'bungee inline', cursive;
}

score:1

edit index.css

@import url("https://fonts.googleapis.com/css2?family=poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");

body {
  margin: 0;
  font-family: "poppins", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

score:2

add link tag in index.html on root directory inside public folder.

<link href="https://fonts.googleapis.com/css?family=bungee+inline" rel="stylesheet"/>

then use it in any css file.

score:2

i can see there are various different ways to include google fonts in react app. let's explore the most preferred and optimum way.

@import vs link

the two options that google font provides are using link and @import. so now the question directs toward decision in between @import and link. there is already a stack overflow question regarding this comparison and here is a reference from the accepted answer

<link> is preferred in all cases over @import, because the latter blocks parallel downloads, meaning that the browser will wait for the imported file to finish downloading before it starts downloading the rest of the content.

so, it's most preferable to use the link tag that google font provides

how to use link and why in a react app?

i have seen few answers giving this method as a solution but i want to make it more clear why it is most preferable.

after using create-react-app to initialize the project, you can see a comment in the index.html file inside the public folder as below.

you can add webfonts, meta tags, or analytics to this file. the build step will place the bundled scripts into the tag.

so you can simply include the link tag that google font provides in the head section of the above file.

<link href="https://fonts.googleapis.com/css?family=bungee+inline" rel="stylesheet">

then you can use it in the css file and import in jsx

font-family: 'bungee inline', cursive;

score:3

i added the @import and the @font-face in my css file and it worked.enter image description here

score:3

if anyone looking for a solution with (.less) try below. open your main or common less file and use like below.

@import (css) url('https://fonts.googleapis.com/css?family=open+sans:400,700');

body{
  font-family: "open sans", sans-serif;
}

score:3

in case someone needs it, you can use @fontsource. they have all of the google fonts and seems easier than most of the solutions here.

score:5

another option to all of the good answers here is the npm package react-google-font-loader, found here.

the usage is simple:

import googlefontloader from 'react-google-font-loader';

// somewhere in your react tree:

<googlefontloader
    fonts={[
        {
            font: 'bungee inline',
            weights: [400],
        },
    ]}
/>

then you can just use the family name in your css:

body {
    font-family: 'bungee inline', cursive;
}

disclaimer: i'm the author of the react-google-font-loader package.

score:6

had the same issue. turns out i was using " instead of '.

use @import url('within single quotes'); like this

not @import url("within double quotes"); like this

score:13

here are two different ways you can adds fonts to your react app.

adding local fonts

  1. create a new folder called fonts in your src folder.

  2. download the google fonts locally and place them inside the fonts folder.

  3. open your index.css file and include the font by referencing the path.

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

here i added a rajdhani font.

now, we can use our font in css classes like this.

.title{
    font-family: rajdhani, serif;
    color: #0004;
}

adding google fonts

if you like to use google fonts (api) instead of local fonts, you can add it like this.

@import url('https://fonts.googleapis.com/css2?family=rajdhani:wght@300;500&display=swap');

similarly, you can also add it inside the index.html file using link tag.

<link href="https://fonts.googleapis.com/css2?family=rajdhani:wght@300;500&display=swap" rel="stylesheet">

(originally posted at https://reactgo.com/add-fonts-to-react-app/)

score:14

in your css file, such as app.css in a create-react-app, add a fontface import. for example:

@fontface {
  font-family: 'bungee inline', cursive;
  src: url('https://fonts.googleapis.com/css?family=bungee+inline')
}

then simply add the font to the dom element within the same css file.

body {
  font-family: 'bungee inline', cursive;
}

score:19

you should see this tutorial: https://scotch.io/@micwanyoike/how-to-add-fonts-to-a-react-project

import webfont from 'webfontloader';

webfont.load({
  google: {
    families: ['titillium web:300,400,700', 'sans-serif']
  }
});

i just tried this method and i can say that it works very well ;)

score:25

if you are using create react app environment simply add @import rule to index.css as such:

@import url('https://fonts.googleapis.com/css?family=anton');

import index.css in your main react app:

import './index.css'

react gives you a choice of inline styling, css modules or styled components in order to apply css:

font-family: 'anton', sans-serif;

score:104

google fonts in react.js?

open your stylesheet i.e, app.css, style.css (what name you have), it doesn't matter, just open stylesheet and paste this code

@import url('https://fonts.googleapis.com/css?family=josefin+sans');

and don't forget to change url of your font that you want, else working fine

and use this as :

body {
  font-family: 'josefin sans', cursive;
}

score:142

in some sort of main or first loading css file, just do:

@import url('https://fonts.googleapis.com/css?family=source+sans+pro:regular,bold,italic&subset=latin,latin-ext');

you don't need to wrap in any sort of @font-face, etc. the response you get back from google's api is ready to go and lets you use font families like normal.

then in your main react app javascript, at the top put something like:

import './assets/css/fonts.css';

what i did actually was made an app.css that imported a fonts.css with a few font imports. simply for organization (now i know where all my fonts are). the important thing to remember is that you import the fonts first.

keep in mind that any component you import to your react app should be imported after the style import. especially if those components also import their own styles. this way you can be sure of the ordering of styles. this is why it's best to import fonts at the top of your main file (don't forget to check your final bundled css file to double check if you're having trouble).

there's a few options you can pass the google font api to be more efficient when loading fonts, etc. see official documentation: get started with the google fonts api

edit, note: if you are dealing with an "offline" application, then you may indeed need to download the fonts and load through webpack.


Related Query

More Query from same tag