DEV Community

Cover image for How to apply Google Fonts to a Next.js and Tailwind CSS app.
Yasuhiro Nagata
Yasuhiro Nagata

Posted on

4 1

How to apply Google Fonts to a Next.js and Tailwind CSS app.

I googled and have found some solutions for this.

But, here I share a simpler one.

  • This is useful for one-off fonts on your project.

  • There's no need to create _document.js or edit tailwind.config.js.

Solution

  1. Go to Google Fonts and pick a font

    https://res.cloudinary.com/practicaldev/image/fetch/s--76-dZUNB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hl8jko1te5lm87yar2bl.png

  2. Select @import and copy the codes

    https://res.cloudinary.com/practicaldev/image/fetch/s--68flBBsR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m38ctc4fqafcizaxftb7.png

  3. Paste it to styles/globals.css

    @import url('https://fonts.googleapis.com/css2?family=Caveat&family=Oswald:wght@200&display=swap');
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
  4. Use it everywhere

    Type font-[<Your Font Name>] to apply.

<h1 className="font-bold text-4xl font-['Oswald']">This Is Title</h1>
Enter fullscreen mode Exit fullscreen mode

Tips

If you use fonts which have spaces inside their names, concatenate them using -

e.g. font-['Cormorant-SC'] for Cormorant SC

My environment

  • "next": "12.2.3"
  • "react": "18.2.0"
  • "tailwindcss": "^3.1.6"

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (1)

Collapse
 
ironcladmerc profile image
ironcladmerc

this works and is fairly easy, but I am getting a "font pop-in" when initially loading my pages. The font appears normal and a half second later the Google Font "pops in" and it changes to the correct font. Is there a way to avoid this?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it