Open In App

How to use font from local files globally in Tailwind CSS ?

Last Updated : 25 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tailwind CSS is a utility-first framework that comes with a wide range of pre-configured styles. Among these are default font families such as sans, serif, and mono, which offer system fonts like Arial, Times New Roman, and Courier New. These fonts can be easily applied to HTML elements by adding the corresponding class names, for example:

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

Adding Custom Local Fonts to Tailwind CSS

To include custom fonts in your Tailwind CSS project, follow these simple steps:

Step 1: Download the Font

First, download the font you wish to use. You can find free or licensed fonts from resources like Google Fonts, Font Squirrel, or even your custom font designs. Save the downloaded font files in your project’s assets folder or any other folder you prefer.

Step 2: Define Font in the CSS File

Once your fonts are stored locally, you need to define them using the @font-face rule in your CSS file.

Create or edit the input.css file where you’ll add the @font-face declarations. Ensure you correctly specify the file paths for each font.

Step 3: Update Tailwind Configuration

Next, you’ll need to update the tailwind.config.js file to make Tailwind aware of your custom fonts.

In your tailwind.config.js file, add the following code to the extend section under theme.fontFamily. Here, we’re defining two custom fonts—Glory and Pop—and setting a fallback to system fonts like sans-serif in case the custom fonts are not available.

Step 4: Use Custom Fonts in HTML

Now that your fonts are set up, using them in your HTML is as simple as applying the font-{family-name} utility class.

Folder structure :

How to use font from local files globally in Tailwind CSS

How to use font from local files globally in Tailwind CSS

Example 1: This example shows how to use font from local files globally in Tailwind CSS.

HTML
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="output.css">
    <title>Custom Fonts</title>
</head>
    <body>
        <h1 class="text-green font-bold text-4xl m-8 font-pop">
              Geeks For Geeks
          </h1>
    </body>
</html>
CSS
/* tailwind.config.js  */
@tailwind base;
@tailwind components;
@tailwind utilities;


@font-face {
    font-family: "pop";
    src: url("../public/assets/pop.ttf");
}
@font-face {
    font-family: "glory";
    src: url("../public/assets/glory.ttf");
}
CSS
/* input.css */
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["public/*.{html,js}"],
  theme: {
    extend: {
      colors:{
        'green': '#008000',
      },
      fontFamily :{
        glory: ["glory","sans-serif"],
        pop: ["pop","sans"],
      }
    },
  },
  plugins: [],
}

You should add your fonts as an array if somehow your specified font is not found in the directory then the second one is going to be applied, e.g “glory” is not found in the directory then the `sans-serif` going to apply. See the class we have added the pop  font with the same name as specified in the config file with the prefix font `font-pop`

Output:

How to use font from local files globally in Tailwind CSS

How to use font from local files globally in Tailwind CSS

Example 2: This example shows how to use font from local files globally in Tailwind CSS.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="output.css">
    <title>Custom Fonts</title>
</head>
    <body>
        <h1 class="text-[#008000] text-4xl m-8 font-glory">Geeks For Geeks</h1>
    </body>
</html>
CSS
/* tailwind.config.js*/
/** @type {import('tailwindcss').Config} */
module.exports = {
    content: ["public/*.{html,js}"],
          theme: {
        extend: {
          colors:{
            'green': '#008000',
      },
      fontFamily :{
          glory: ["glory","sans-serif"],
          pop: ["pop","sans"],
      }
    },
  },
  plugins: [],
}
CSS
/* input.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
    font-family: "pop";
    src: url("../public/assets/pop.ttf");
}
@font-face {
    font-family: "glory";
    src: url("../public/assets/glory.ttf");
}

Output :

How to use font from local files globally in Tailwind CSS

How to use font from local files globally in Tailwind CSS



Next Article

Similar Reads