How to use Google fonts in React.js?

前端 未结 12 736
既然无缘
既然无缘 2020-12-02 18:06

I had built a website with React.js and webpack.

I want to use Google fonts in the webpage, so I put the link in the section.

Go

相关标签:
12条回答
  • 2020-12-02 18:21

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 18:21

    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/)

    0 讨论(0)
  • 2020-12-02 18:29

    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;
    
    0 讨论(0)
  • 2020-12-02 18:32

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 18:33

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 18:34

    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.

    0 讨论(0)
提交回复
热议问题