Tuesday, June 6, 2023
HomeSoftware DevelopmentThe right way to use font from native information globally in Tailwind...

The right way to use font from native information globally in Tailwind CSS ?


Tailwind CSS offers a set of default font households that you should use in your initiatives. These font households are outlined as utility lessons within the type of font-{family-name} and could be simply utilized to any HTML aspect.

These are the default fonts which are offered by the tailwind CSS:

  • sans:  sans-serif font household, together with system fonts corresponding to Arial, Verdana, and sans-serif.
  • serif: serif font household, together with system fonts corresponding to Occasions New Roman, Georgia, and serif.
  • mono: monospace font household, together with system fonts corresponding to Courier New, Lucida Console, and monospace.

we will use them by simply together with their class identify within the HTML tags.

<p class="font-mono">
    It is a paragraph with a monospace font.
</p>

however tailwind CSS just isn’t solely restricted to those three fonts even you’ll be able to add your customized fonts domestically there aren’t any limitations to including customized fonts.

Why native fonts? 

  • Your web site or software’s efficiency and loading time could be enhanced through the use of native fonts in Tailwind CSS.
  • Through the use of domestically saved fonts, the browser doesn’t need to make further requests to load the fonts from an exterior server
  • Moreover, utilizing native fonts can be sure that the font will probably be displayed accurately even when the exterior server is down or unavailable. It additionally can assist in case of a gradual web connection.

Syntax:

fontFamily:{
    glory: ["glory","sans-serif"],
    pop: ["pop","sans"],
}

For Including native fonts to your venture it’s important to comply with a number of steps. These steps are as follows

Step 1: Obtain the font domestically in your machine that you simply wish to use in your tailwind and Add the font information to your venture’s belongings(you can provide any identify as per your comfort) folder.

Step 2: Earlier than implementation, now we have to inform the tailwind the placement of the font and assign the identify to it. import the font information utilizing the @font-face rule.

Step 3: Add the fonts within the tailwind.config.js file to make use of the fonts within the venture and provides them the identify as you need but it surely’s really useful to present them the related names.

Step 4: use the desired font class for the customized font within the HTML tag . as a font-{family-name}

Folder construction :

How to use font from local files globally in Tailwind CSS

The right way to use font from native information globally in Tailwind CSS

Instance 1: This instance reveals the right way to use font from native information globally in Tailwind CSS.

index.html

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Appropriate" content material="IE=edge">

    <meta identify="viewport" content material="width=device-width, initial-scale=1.0">

    <hyperlink rel="stylesheet" href="output.css">

    <title>Customized Fonts</title>

</head>

    <physique>

        <h1 class="text-green font-bold text-4xl m-8 font-pop">

              Geeks For Geeks

          </h1>

    </physique>

</html>

tailwind.config.js 

CSS

@tailwind base;

@tailwind elements;

@tailwind utilities;

  

  

@font-face {

    font-family: "pop";

    src: url("../public/belongings/pop.ttf");

}

@font-face {

    font-family: "glory";

    src: url("../public/belongings/glory.ttf");

}

As you’ll be able to see that now we have included two fonts “pop”, and “glory” within the CSS file .ttf is the format of the fonts. In src embody the handle/location of the font that you’ve got domestically downloaded in your venture listing of the belongings folder.

enter.css

CSS

module.exports = {

  content material: ["public/*.{html,js}"],

  theme: {

    lengthen: {

      colours:{

        'inexperienced': '#008000',

      },

      fontFamily :{

        glory: ["glory","sans-serif"],

        pop: ["pop","sans"],

      }

    },

  },

  plugins: [],

}

You must add your fonts as an array if one way or the other your specified font just isn’t discovered within the listing then the second goes to be utilized, e.g “glory” just isn’t discovered within the listing then the `sans-serif` going to use. See the category now we have added the pop  font with the identical identify as specified within the config file with the prefix font `font-pop`

Output: 

  

How to use font from local files globally in Tailwind CSS

The right way to use font from native information globally in Tailwind CSS

Instance 2: This instance reveals the right way to use font from native information globally in Tailwind CSS.

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Appropriate" content material="IE=edge">

    <meta identify="viewport" content material="width=device-width, initial-scale=1.0">

    <hyperlink rel="stylesheet" href="output.css">

    <title>Customized Fonts</title>

</head>

    <physique>

        <h1 class="text-[#008000] text-4xl m-8 font-glory">Geeks For Geeks</h1>

    </physique>

</html>

tailwind.config.js 

CSS

module.exports = {

    content material: ["public/*.{html,js}"],

          theme: {

        lengthen: {

          colours:{

            'inexperienced': '#008000',

      },

      fontFamily :{

          glory: ["glory","sans-serif"],

          pop: ["pop","sans"],

      }

    },

  },

  plugins: [],

}

enter.css

CSS

@tailwind base;

@tailwind elements;

@tailwind utilities;

  

@font-face {

    font-family: "pop";

    src: url("../public/belongings/pop.ttf");

}

@font-face {

    font-family: "glory";

    src: url("../public/belongings/glory.ttf");

}

Output :

How to use font from local files globally in Tailwind CSS

The right way to use font from native information globally in Tailwind CSS

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments