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

Installation

在 Symfony 中安裝 Tailwind CSS

在 Symfony 專案中設定 Tailwind CSS。

01

建立你的專案

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

Terminal
symfony new --webapp my-projectcd my-project
02

安裝 Webpack Encore

安裝 Webpack Encore,它負責建置你的資源。請參閱 文件以取得更多資訊。

Terminal
composer remove symfony/ux-turbo symfony/asset-mapper symfony/stimulus-bundlecomposer require symfony/webpack-encore-bundle symfony/ux-turbo symfony/stimulus-bundle
03

安裝 Tailwind CSS

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

Terminal
npm install tailwindcss @tailwindcss/postcss postcss postcss-loader
04

啟用 PostCSS 支援

在你的 webpack.config.js 檔案中,啟用 PostCSS Loader。請參閱 文件以取得更多資訊。

webpack.config.js
Encore  .enablePostCssLoader();
05

設定 PostCSS 外掛

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

postcss.config.mjs
export default {  plugins: {    "@tailwindcss/postcss": {},  },};
06

匯入 Tailwind CSS

./assets/styles/app.css 中加入 @import 來匯入 Tailwind CSS,並加入 @source 來忽略 public 目錄,以防止在監視模式下重複編譯。

app.css
@import "tailwindcss";@source not "../../public";
07

啟動你的建置流程

使用 npm run watch 執行建置流程。

Terminal
npm run watch
08

開始在專案中使用 Tailwind

確保你編譯後的 CSS 已包含在 <head> 中,然後開始使用 Tailwind 的通用類別來設定內容樣式。

base.html.twig
<!doctype html><html>  <head>    <meta charset="utf-8" />    <meta      name="viewport"      content="width=device-width, initial-scale=1.0"    />    {% block stylesheets %}      {{ encore_entry_link_tags('app') }}    {% endblock %}  </head>  <body>    <h1 class="text-3xl font-bold underline">      Hello world!    </h1>  </body></html>
Copyright © 2026 Tailwind Labs Inc.·商標政策(Brand)