"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

Published on 2024-11-08
Browse:620

When creating a web application, including your preferred font is like icing on the cake. Fonts enhance the text, make the website more appealing, and provide a better user experience. Designers and developers love and hate some fonts, and using the default font might limit their creativity. Adding custom fonts gives developers the freedom to add an external font to their application.

Prerequisites

In this tutorial, I strongly recommend that you have basic knowledge of Tailwind CSS.

I assume the reader is familiar with Tailwind CSS and how to integrate Tailwind into an application. If you are new to Tailwind, you can check the official documentation for instructions on how to install it.

What is a Custom Font?

Custom fonts are fonts that are not available for use by default. Custom fonts do not exist in your system and are not readily available when needed. They include fonts you purchase, get online, create yourself or specially branded fonts that your company uses. A popular example of a custom font is the Google font.

Adding Custom Fonts to Your Project

When you install Tailwind on your project, it adds a file named tailwind.config. Inside the tailwind.config file is where we add custom fonts, colours, grid layout templates, font sizes, etc. To add custom fonts, place the custom properties between the extend object. See below how the tailwind.config file looks:

/* tailwind.config file */

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: { },
    },
  },
  plugins: [],
};

To add a custom font, I will use Google Fonts. Go to the google font website, click on Select Styles, then select your preferred font. For this tutorial, I will use this Rubik's font. See the pictorial representation of google-font website below, with circled numbers as a guide:

How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

To attach the Google link to your HTML file, take the following steps:

  • Copy the link from Google.

  • Go to the index.html file.

  • Find the head tag and paste the link from Google Fonts inside.





  
  
    React App
    

Using Custom Fonts

After pasting Rubik fonts inside the index.html file, the Rubik font should be available in your project, but you can't use it yet.

To use it:

Go to tailwind.config file.

Add the fontFamily inside the extend object.

Inside the font family, I will give the font a name, in this case, the name is rub. It can have any name. Open a bracket, add the font name ("Rubik"), and a backup font.

/* tailwind.config file */

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {
      fontFamily: {
        'rub': ["Rubik", "sans-serif"],
      },
    },
  },
  plugins: [],
};

Tailwind recognizes Rubik's font, but I have not put it to use. Go to the file or component you want to use the font on and add Rubik's font to its class=''/className='' attributes. To apply the custom font to your project, use rub, not Rubik. See the example below:

// the file/component 

import React from 'react'

function CustomFonts() {
  return (
    

Default Font

Hello My name is Emeka and I enjoy creating things that live on the internet. I also write technical articles.

Custom Font(Rubik Font)

Hello My name is Emeka and I enjoy creating things that live on the internet. I also write technical articles.

) } export default CustomFonts

Using Locally Downloaded Fonts

To use fonts downloaded locally, I will pick a random website. You can try any website of your choice. Go to the dafont website, search for a font in the search bar, and then download it to your local computer. See the pictorial representation of dafont website below, with circled numbers as a guide:

How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

Extract the zip file (I use WinRAR to extract), copy the extracted file, and paste it into a folder in your project. See the example below:

How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

The next step is to navigate to /index.css file and insert @font-face to bring the custom font into the project. I will use ADELIA for the font family and src: to specify where the font is available.

@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
    font-family: 'ADELIA';
    src: url('./fonts/ADELIA.ttf');
}

To integrate the Rubik font, navigate to the tailwind.config file and take the following steps:

  • Add a custom utility class name.

  • Open a bracket

  • Insert 'ADELIA', and 'cursive' as a backup font.

Here is an example:

/* tailwind.config file */

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {
      fontFamily: {
        'rub': ["Rubik", "sans-serif"],
        'adelia': ['ADELIA', 'cursive']
      },
    },
  },
  plugins: [],
};

We can now use the font in our project:

// the file/component 

import React from 'react'

function CustomFonts() {
  return (
    

Default font

Hello My name is Emeka and I enjoy creating things that live on the internet. I also write technical articles.

Custom Font(Rubik Font)

Hello My name is Emeka and I enjoy creating things that live on the internet. I also write technical articles.

) } export default CustomFonts

Conclusion

You can use the custom font in any component or file. There are no limitations to a specific file or component; you can use it in multiple components or files throughout your project. Also, you can add more than one custom font to the config file. I hope the article was helpful. Like, comment, and share so others can learn. Gracias.

Release Statement This article is reproduced at: https://dev.to/michaelanazodo1/how-to-add-custom-font-to-tailwind-for-web-and-locally-downloaded-fonts-1m89?1 If there is any infringement, please contact study_golang @163.comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3