Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to use Google Fonts in TailwindCSS
Avneesh Agarwal
Avneesh Agarwal

Posted on • Edited on • Originally published atblog.avneesh.tech

     

How to use Google Fonts in TailwindCSS

We would like to use some beautiful fonts in our app, so I will teach you how to do that in this article. I am going to use Next.js today but you can use it in any other framework/library or vanilla as well. The procedure is going to be pretty much the same.

let's get started

Setting up the app

Creating a Next.js app

npx create-next-app next-tailwind-ts-demo
Enter fullscreen modeExit fullscreen mode

Cleanup

Delete everything under the Head tag until the footer like this

import Head from "next/head";export default function Home() {  return (    <div>      <Head>        <title>Create Next App</title>        <meta name="description" content="Generated by create next app" />        <link rel="icon" href="/favicon.ico" />      </Head>    </div>  );}
Enter fullscreen modeExit fullscreen mode

Adding Tailwind to the app

Installing the dependencies -

yarn add -D tailwindcss@latest postcss@latest autoprefixer@latest # yarnnpm install -D tailwindcss@latest postcss@latest autoprefixer@latest # npm
Enter fullscreen modeExit fullscreen mode

Generating the config files -

npx tailwindcss init -p
Enter fullscreen modeExit fullscreen mode

This will generatetailwind.config.js andpostcss.config.js

Adding the files to purge through
Replace the purge intailwind.config.js with this

  purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
Enter fullscreen modeExit fullscreen mode

Change your globals.css

@tailwind base;@tailwind components;@tailwind utilities;
Enter fullscreen modeExit fullscreen mode

Starting the app

yarn dev # yarnnpm run dev # npm
Enter fullscreen modeExit fullscreen mode

Getting our custom font

Go toGoogle Fonts and select the font you want in your app.

I am gonna useRampart One for this demo.

  • Click on "Select this style" and a sidebar should pop in.
  • Now copy the import URL given in Use on the web section.

image.png

Using the font

To use the font follow these steps-

  • Paste the import inglobasl.css
@import url("https://fonts.googleapis.com/css2?family=Rampart+One&display=swap");@tailwind base;@tailwind components;@tailwind utilities;
Enter fullscreen modeExit fullscreen mode
  • Inside themes > extend add a new property offontFamily and give the font a name
 theme: {    extend: {      fontFamily: {       Rampart: ["Rampart One", "cursive"],      },    },  },
Enter fullscreen modeExit fullscreen mode

The rules in the array should be the same as given in Google fonts.

image.png

  • Now you can give any tag the className of font-fontName in this casefont-Rampart.
<h1 className="font-Rampart">This is using a custom font</h1>
Enter fullscreen modeExit fullscreen mode

Now you have got the beautiful font you need 🥳.

image.png

Useful links -

Github repository

TailwindCSS

Google Fonts

All socials

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
kanishkkhurana profile image
Kanishk Khurana
Hi!i am a Computer Science Student . i have a lot of interest in blockchain, react, and i support basically any platform that is helping make the world a better and more secure place !

Great share ! I was trying to figure out where to put the import statement for so long !

CollapseExpand
 
yasuhiron777 profile image
Yasuhiro Nagata
A backend developer.
  • Location
    Tokyo, Japan
  • Joined

Thank you!
Your post helped me save my time!

CollapseExpand
 
ekimcem profile image
Ekim Cem Ülger
Founder & Software Developer @BronixSoftware
  • Email
  • Location
    Ankara, Turkey
  • Education
    Atilim University BSc. / METU MSc.
  • Work
    Software Developer @BronixSoftware
  • Joined
• Edited on• Edited

What if we do not extend and want to change all of fonts so we will not need to type font-[fontname] everytime, how can we do it?

I tried just add the fontFamily in the same level of extend but it didnt work.
@avneesh0612

CollapseExpand
 
jassehomar profile image
Omar Jasseh
A Gambian Based Fullstack Software Developer, who don't know how to stop until it's done.
  • Location
    Banjul, The Gambia
  • Education
    BSC Computer Science, Self Thaught
  • Work
    Fullstack Developer and Code Instructor at Tritech Software
  • Joined

@ekimcem you can do that by adding below snippet to your main CSS file in this case inglobal.css

@layerbase{html{font-family:"Rampart One",cursive;}}
Enter fullscreen modeExit fullscreen mode

So for example your final version of global.css should like below

@importurl("https://fonts.googleapis.com/css2?family=Rampart+One&display=swap");@tailwindbase;@tailwindcomponents;@tailwindutilities;@layerbase{html{font-family:"Rampart One",cursive;}}
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a fullstack web developer. I love to make beautiful websites and also teach others how to make them by writing blogs.
  • Location
    India
  • Joined

More fromAvneesh Agarwal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp