1. Installation
  2. 在 Gatsby 中安裝 Tailwind CSS

Installation

在 Gatsby 中安裝 Tailwind CSS

在 Gatsby 專案中設定 Tailwind CSS。

01

建立你的專案

如果你還沒有設定專案,請先建立一個新的 Gatsby 專案。最常見的方式是使用 Gatsby CLI

Terminal
gatsby new my-projectcd my-project
02

安裝 Tailwind CSS

使用 npm 安裝 @tailwindcss/postcss、其對等相依套件,以及 gatsby-plugin-postcss

Terminal
npm install @tailwindcss/postcss tailwindcss postcss gatsby-plugin-postcss
03

啟用 Gatsby PostCSS 外掛

在你的 gatsby-config.js 檔案中,啟用 gatsby-plugin-postcss。請參閱 外掛文件以取得更多資訊。

gatsby-config.js
module.exports = {  plugins: [    'gatsby-plugin-postcss',    // ...  ],}
04

設定 PostCSS 外掛

在專案根目錄建立 postcss.config.js 檔案,並將 @tailwindcss/postcss 外掛加入你的 PostCSS 設定。

postcss.config.js
module.exports = {  plugins: {    "@tailwindcss/postcss": {},  },};
05

匯入 Tailwind CSS

建立 ./src/styles/global.css 檔案,並加入 @import 來匯入 Tailwind CSS。

global.css
@import "tailwindcss";
06

匯入 CSS 檔案

如果尚未存在,在專案根目錄建立 gatsby-browser.js 檔案,並匯入新建立的 ./src/styles/global.css 檔案。

gatsby-browser.js
import './src/styles/global.css';
07

啟動你的建置流程

使用 gatsby develop 執行建置流程。

Terminal
gatsby develop
08

開始在專案中使用 Tailwind

開始使用 Tailwind 的通用類別來設定內容樣式。

index.js
export default function IndexPage() {  return (    <Layout>      <h1 className="text-3xl font-bold underline">        Hello world!      </h1>    </Layout>  )}
Copyright © 2026 Tailwind Labs Inc.·商標政策(Brand)