search
HomeWeb Front-endJS TutorialHow to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts

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.

<!-- index.html file -->



  <!-- the heade tag -->
  
    <meta charset="utf-8">
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="theme-color" content="#000000">
    <meta name="description" content="Web site created using create-        
    react-app">
    <!-- google link -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?%20%20%20%20family=Abril+Fatface&family=Mulish:wght@200;300;400;500;600;700;800;900;1%20%20%20%20000&family=Rubik:wght@400;500&display=swap" rel="stylesheet">
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png">
      <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <title>React App</title>
  
  
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  

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 (
    <div classname="flex justify-center">
        <div>
            <!-- without custom font -->
            <h1 id="Default-Font">Default Font</h1>
            <p>Hello My name is Emeka and I enjoy creating things that     
            live on the internet. I also write technical articles.</p>
        </div>
        <div>
            <!-- with custom font -->
            <h1 id="Custom-Font-Rubik-Font">Custom Font(Rubik Font)</h1>
            <p classname="font-rub">Hello My name is Emeka and I enjoy         
            creating things that live on the internet. I also write             
            technical articles.
        </p>
</div>
    </div>
  )
}

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 (
    <div classname="flex justify-center">
        <div>
            <!-- without custom font -->
            <h1 id="Default-font">Default font</h1>
            <p>Hello My name is Emeka and I enjoy creating things that     
            live on the internet. I also write technical articles.</p>
        </div>
        <div>
            <!-- with custom font -->
            <h1 id="Custom-Font-Rubik-Font">Custom Font(Rubik Font)</h1>
            <p classname="font-adelia">Hello My name is Emeka and I enjoy         
            creating things that live on the internet. I also write             
            technical articles.
        </p>
</div>
    </div>
  )
}

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.

The above is the detailed content of How to add Custom Font to Tailwind - For Web and Locally Downloaded Fonts. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools