How to set a custom font in your Tailwind CSS config file

Updating the custom font setting in your TailwindCSS config.js file is a simple process that can add a unique and personalized touch to your project. In this blog post, we will walk you through the steps to update the custom font setting in your TailwindCSS config.js file.

Before we get started, it's important to note that in order to use a custom font in your project, you must first have the font files saved on your computer and uploaded to your project. You will also need to make sure that you have the correct permissions to use the font in your project.

Now, let's get started with the update process.

Step 1: Locate the config.js file

Locating the config.js file for your TailwindCSS project is the first step in updating the custom font setting. The config.js file is a vital part of your TailwindCSS setup, as it contains all of the configurations and settings for your project.

To locate the config.js file, you will need to navigate to the root directory of your project. This is typically the top-level folder that contains all of the files and subfolders for your project.

Once you are in the root directory, look for a file called "config.js". This is the file that you will need to edit in order to update the custom font setting.

If you are having trouble finding the config.js file, it's a good idea to do a search of your project folder. You can use the search function in your operating system to look for the config.js file. Just be sure to search within the root directory of your project, rather than your entire hard drive.

Step 2: Open the config.js file in a text editor

Once you have found the config.js file, open it in your text editor of choice. This can be a simple text editor like Notepad on Windows or TextEdit on Mac, or a more advanced code editor like Sublime Text or Atom.

Step 3: Locate the "fonts" section of the config.js file

Once you have located the config.js file, you can scroll down to the "fonts" section, which should look something like this:

module.exports = {
  theme: {
    fontFamily: {
      sans: [
        'system-ui',
        'BlinkMacSystemFont',
        '-apple-system',
        'Segoe UI',
        'Roboto',
        'Oxygen',
        'Ubuntu',
        'Cantarell',
        'Fira Sans',
        'Droid Sans',
        'Helvetica Neue',
        'sans-serif',
      ],
      serif: [
        'Constantia',
        'Lucida Bright',
        'Lucidabright',
        'Lucida Serif',
        'Lucida',
        'DejaVu Serif',
        'Bitstream Vera Serif',
        'Liberation Serif',
        'Georgia',
        'serif',
      ],
      mono: [
        'Menlo',
        'Monaco',
        'Consolas',
        'Liberation Mono',
        'Courier New',
        'monospace',
      ],
    },
  },
  variants: {},
  plugins: [],
}

This "fonts" section defines the default font families that are available to use in your project. The default font families are "sans", "serif", and "mono", but you can add additional font families as needed.

Step 4: Replace a default font with your custom font

To customize the fonts used in your project, you can either modify the default font families or add new ones. For example, to use the "Open Sans" font for the "sans" font family, you could update the "sans" font family like this:

sans: [
  'Open Sans',
  'system-ui',
  'BlinkMacSystemFont',
  '-apple-system',
  'Segoe UI',
  'Roboto',
  'Oxygen',
  'Ubuntu',
  'Cantarell',
  'Fira Sans',
  'Droid Sans',
  'Helvetica Neue',
  'sans-serif',
],

Note that it's generally a good idea to include a fallback font in case the custom font fails to load. In the example above, the "system-ui" font is used as a fallback for the "sans" font family.

Step 5: Save the config.js file

Once you have made your changes, save the config.js file and your new custom font setting will be applied to your project.

It's important to note that you can also use the custom font setting to apply different fonts to different parts of your project. For example, you could use one custom font for headings and another for body text. To do this, simply replace the appropriate default font in the relevant array with your custom font.

It's also worth noting that you can use the custom font setting to apply different font weights to different parts of your project. For example, you could use a lighter font weight for headings and a heavier font weight for body text.

That's it! With just a few simple steps, you can easily update the custom font setting in your TailwindCSS config.js file and give your project a unique and personalized look.

Additional resources
  • Frontend web development courses

    Beginner-friendly courses focusing on HTML, CSS, and JavaScript.

    View Courses
  • Frontend web development projects

    Beginner-friendly projects focusing on HTML, CSS, and JavaScript.

    View Projects
  • Free website templates

    Collection of free, high-quality website templates for your next project.

    View Templates