Next JS Sep 3, 2023

Installing Custom Fonts Next JS 13

profile picture of author
Author

KACEY

Next JS 13: Installing Custom Fonts In Minutes

Custom Fonts and Tailwind CSS in Next.js: A Step-by-Step Guide

In this tutorial, we'll walk you through the process of integrating custom fonts and leveraging Tailwind CSS in a Next.js project. Custom fonts can significantly enhance the aesthetics of your website, and Tailwind CSS simplifies the styling process. Combining the two can help you create visually appealing web applications.

Prerequisites

Before we get started, make sure you have the following prerequisites in place:

  1. Node.js and npm installed on your computer.
  2. A basic understanding of Next.js and React.
Step 1: Installing the Required Dependencies

First, let's set up our project and install the necessary dependencies. Create a Next.js project if you haven't already:

npx create-next-app my-custom-font-app cd my-custom-font-app

Now, install the required packages for custom fonts and Tailwind CSS:

npm install @fontsource/luckiest-guy npm install tailwindcss npm install postcss postcss-preset-env
Step 2: Configuring Custom Fonts

Open your tailwind.config.js file and configure the custom font. Add the following code within the theme section:

module.exports = { // ... Other configurations theme: { extend: { fontFamily: { luckiestGuy: ["Luckiest_Guy"], }, }, }, // ... Other configurations };
Step 3: Setting Up the Custom Font

In your Next.js project, import and set up the custom font in a JavaScript or TypeScript file:

import { Luckiest_Guy } from "@fontsource/luckiest-guy"; const luckiestGuy = Luckiest_Guy({ subsets: ["latin"], variable: "--font-luckiest-guy", weight: "400", });
Step 4: Applying the Custom Font

Now, apply the custom font to your HTML or React components. Here's an example of using it in a Next.js component:

import React from "react"; const MyComponent = ({ children }) => { return ( <html lang="en"> <body className={` ${luckiestGuy.variable}`}> {children} </body> </html> ); }; export default MyComponent;
Step 5: Using Tailwind CSS Classes

Finally, you can use Tailwind CSS classes to apply your custom font to elements in your project. For example:

import React from "react"; const Home = () => { return ( <div> <h1>Hello World Again</h1> <h1 className="font-luckiestGuy">Hello World Luckiest Guy Font</h1> </div> ); }; export default Home;
Step 6: Start Your Next.js Development Server

To see the changes, start your Next.js development server:

npm run dev

Now, you have successfully integrated custom fonts and Tailwind CSS into your Next.js project.

That's it! You've learned how to set up custom fonts and use Tailwind CSS in a Next.js application. Feel free to explore further customization options with Tailwind CSS and experiment with different fonts to give your project a unique look.

Happy coding!

Related Articles