Add Custom Tailwind CSS Fonts to your website
Tags: tailwindcss, fonts, tutorial
Published: 2021-09-17 07:17:14

<small>Orginally published at Medium on March 17th 2020.</small>

extend Tailwind CSS configuration with local fonts or Google Fonts

![Barcelony font](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8wipomo6h3lvtw7ppul.png)

Tailwind CSS offers developers powerful capabilities to build visually appealing websites in a short time frame. To give your website a unique look and feel, you can choose to add new fonts to the Tailwind configuration.

> To give your website a unique look and feel, you can choose to add new fonts to the Tailwind configuration.

The [default Tailwind classes](https://tailwindcss.com/docs/font-family) include 3 different fonts. Font-sans is the default font that will be applied even when you don’t explicitly set the font-sans class. If you want to add more fonts to custom your website, Tailwind got your back too!

To use a different font, you could either use an @import from Google fonts or import the fonts locally into your project. For example, when Google Fonts does not have the specific font, you are looking for.

![Tailwind fonts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa17s3qkkdemmg37mvxf.png)

## 1a. Import Google Fonts
Visit [Google Fonts](https://fonts.google.com/) and search for the specific font you like. Select the style variants from thin (100) to bold (900). Copy the import statement in between the style tags.

![Google fonts how](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ega4apae637y32lfl71.png)

Import the new font in the CSS file where you import tailwind classes, i.e., styles.css. Note, your fonts must be loaded at the top of the file.

![Import](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgkqf0s1t31kwgawckhg.png)

## 1b. Import local fonts
To create something more unique, find an appealing font that is not on Google Fonts, such as the handwritten [Barcelony](https://www.dafont.com/barcelony.font).

Create a fonts folder in the assets pipeline (i.e., assets/fonts). In the fonts folder, add one of your custom font files, for example:

- `.ttf` for TrueType Fonts
- `.woff` for Web Open Format
- `.otf` for OpenType

With Rails the trick is explicitly to load the newly created font folder into the application. In the `application.rb` file add:

![Rails](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd86gbcptjp6eq95y8cq.png)

Make sure to restart the server.

Next, add the font to the bottom of the styles.css file.

![Css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilgyaw0657rgr36rn4fa.png)

- __font-family__ is the name that will be set into the Tailwind config file.
- __src__ is the path where the local font can be found.

## 2. Overwrite or extend

You can either overwrite the [default Tailwind fonts](https://tailwindcss.com/docs/font-family). Or you can extend and add your own. Using extend will add the newly specified font families without overriding Tailwind’s entire font stack.

If your app is already built, it makes sense to overwrite the font, so you save yourself the need to re-visit every paragraph element on your website to add the custom class.

In your tailwind.config.js file, add the chosen font-family.

![JS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2m6s8xigcodbawvt6m3.png)

Font-sans is now using Roboto. As Font-sans is the default, it’s not necessary to explicitly include that class on an element. Additionally, a new font-heading class is added. As the font-heading class was non-existing, it will be created. You could also have chosen to add it within the extend curly braces.

Font-heading is the newly added Poppins font for our heading/titles. To apply the Poppins font, you can add to the base layer or include the class manually in the HTML where needed.

## 3. Apply styles

![Apply](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i13lea26mrnnoq33k0bw.png)

Apply styles globally. In the file where you import the Tailwind classes add:

Any `<h1>` tag will now by default have the Poppins font applied. Be aware of order specificity. If you have add a font-class, i.e. font-mono, in the HTML on the h1 tag itself, font-mono will overwrite the global defined font-heading class.

Thanks for reading!

*<small>image source: https://www.dafont.com/barcelony.font</small>