3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Next.js】Tailwind CSS を使用する

Last updated at Posted at 2022-05-08

Next.jsでTailwind CSSを使用する方法

Next.js のプロジェクトを作成する

ディレクトリを作成し、ターミナルでnpx create-next-app app_nameを実行します。

ターミナル
$ npx create-next-app app_name

すると以下のようにファイルなどが作成されます。
image220509_033202.png

Tailwind CSSの設定をする

Tailwind CSSをインストールする

まずTailwind CSSをインストールします。
ターミナルで以下のコマンドを実行します。

ターミナル
$ npm install -D tailwindcss postcss autoprefixer

設定ファイルを生成する

次にtailwind.config.jspostcss.config.jsを生成するためにnpx tailwindcss init -pを実行します。

ターミナル
$ npx tailwindcss init -p

image220509_034800.png
tailwind.config.jspostcss.config.jsが生成されました。

tailwind.config.jsを編集する

tailwind.config.js内容を変更します。
以下のように追記します。

tailwind.config.js
module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

global.cssを編集する

次に./styles/globals.cssを編集します。

./styles/globals.css
@tailwind base;
@tailwind components;
@tailwind utilities;

以上で設定が完了しました。

Tailwind CSSを使用する

以下のチートシートを確認ながらindex.jsを以下のように編集します。

index.js
import Head from 'next/head'

const Home = () => {
  return (
    // チートシートを見ながらclassNameに記述する
    <div className="min-h-screen py-0 px-2 flex flex-col justify-center items-center">
      <Head>
        <title>Sample App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
        <h1 className="text-8xl text-purple-600">
          Hello sample-app!!
        </h1>
    </div>
  )
}

export default Home

image220509_043010.png

Tailwind CSSが使用できていることが確認できました。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?