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
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
create a new folder called
fonts
in yoursrc
folder.download the google fonts locally and place them inside the
fonts
folder.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.
Source: stackoverflow.com
Related Query
- How to use Google fonts in a React application using Next.js
- How to use Google fonts in a window.open in React
- How do I use Google Fonts in React JS
- React - How to use GlobalStyles for Google Fonts with Emotion
- How to use Google Fonts API in React JS?
- How to use Google fonts in React.js?
- How can I use React with Google Places API, to display place markers on a Google map?
- How to use google translation api with react
- How to use google fonts in a typescript react-app with JSS?
- How to use a react component for showing Google Maps InfoWindow
- how to use react and node to upload string as file to google drive
- How to use react to implement google Place's Searches, to create markers on the map?
- How to use getDetails() in react Google Map
- How to get classname or props from NavLink to use with google tag manager with react and redux in a stateless functional component?
- how to use a font from google font just to be used in one react component with material UI?
- How can I use Google Analytics code in React JS app?
- How to use different font from Google Fonts on "Headings" of bootstrap?
- How to use componentWillMount() in React Hooks?
- How to use `setState` callback on react hooks
- How to use comments in React
- How to use switch statement inside a React component?
- How to use refs in React with Typescript
- How to use callback with useState hook in react
- How to use children with React Stateless Functional Component in TypeScript?
- How can I make use of Error boundaries in functional React components?
- How to use throttle or debounce with React Hook?
- How to use onClick event on react Link component?
- react Material-ui, How do I know I can use onClick for button?
- How to use google analytics with next.js app?
- How can I use React hooks in React classic `class` component?
More Query from same tag
- TypeError: firebase.analytics is not a function when mocking with jest
- Toggle effect in a react component
- TypeError: setCreateType is not a function error in React
- ReactJS DropZone browser attempts to open file on drop
- npm run-script ssr (server side rendering not working)
- axios post method 400 bad request
- Adding object inside state's array
- Accessing deeper array keys
- Typescript compiler giving " 'type aliases' can only be used in a .ts file." in a js file
- How do I simulate browser events when writing tests for React js?
- How to map through the data and return the documents
- React unable to read property of undefined
- How to host multiple react apps from a single Netlify project?
- React, show quantity of how many elements match after filtering
- best practice for calling api request in action?
- Why is my object being split from char to char?
- How should you ensure state update order in react?
- Link is appending instead of opening new link
- SyntaxError: Unexpected token import with Jest
- missing some cases in createcontext
- Material UI, Appbar logo on left, Tabs in the center
- Return a filtered array based on user input javascript
- fetch() injecting a query variable/header value to every call
- React component doesnnt re render
- Can't get the actual value of a input in react
- React js - check for element value in array of Options
- React Hooks - keep arguments reference in state
- react-redux re-rendering on componentDidMount
- The Component view does not get updated but the redux store is getting updated
- Eslint Error - Unexpected block statement surrounding arrow body; move the returned value immediately after the =>