9
6

More than 3 years have passed since last update.

Next.js+TailwindにGoogleフォントを追加する

Last updated at Posted at 2021-06-29

完成コード

styles/globals.css
@import url('https://fonts.googleapis.com/css2?family=Reggae+One&display=swap');

@tailwind base;
@tailwind components;
@tailwind utilities;
tailwind.config.js
module.exports = {
  purge: [],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {
      // フォントを追加
      fontFamily: {
        reggae: ['Reggae One'],
      },
    },
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
about.jsx
import React from 'react'

const About = () => {
  return (
    <>
      <div className="font-reggae">
        About Page.
      </div>
    </>
  )
}

export default About

解説

Tailwind CSSのフォント設定をカスタマイズします。

Googleフォントにアクセスし、任意のフォントの@importを取得します。

cc.png

Next.jsのglobals.cssに@importを追加します。

styles/globals.css
@import url('https://fonts.googleapis.com/css2?family=Reggae+One&display=swap');

tailwindにフォント設定を追加します。

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      // フォントを追加
      fontFamily: {
        reggae: ['Reggae One'],
      },
  // ...

tailwindの書式でフォントを適用します。

<div className="font-reggae">
  About Page.
</div>

フォントが無事適用されました。

a.png

参考

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6