How to use Google fonts in React.js?

前端 未结 12 804
既然无缘
既然无缘 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

    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.

    
    

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

提交回复
热议问题