安裝
使用 Symfony 安裝 Tailwind CSS
在 Symfony 專案中設定 Tailwind CSS。

建立專案
如果你還沒有設定好,請先建立一個新的 Symfony 專案。最常見的方法是使用 Symfony 安裝程式。
終端機symfony new --webapp my-projectcd my-project
安裝 Webpack Encore
安裝 Webpack Encore,它負責建立你的資產。請參閱 文件 以取得更多資訊。
終端機composer require symfony/webpack-encore-bundle
安裝 Tailwind CSS
使用 npm 安裝
tailwindcss
及其對等相依項,以及postcss-loader
,然後執行 init 指令來產生tailwind.config.js
和postcss.config.js
。終端機npm install -D tailwindcss postcss postcss-loader autoprefixernpx tailwindcss init -p
啟用 PostCSS 支援
在您的
webpack.config.js
檔案中,啟用 PostCSS Loader。請參閱 文件 以取得更多資訊。webpack.config.jsEncore // ... .enablePostCssLoader() ;
設定您的範本路徑
在您的
tailwind.config.js
檔案中加入所有範本檔案的路徑。tailwind.config.js/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./assets/**/*.js", "./templates/**/*.html.twig", ], theme: { extend: {}, }, plugins: [], }
將 Tailwind 指令加入您的 CSS
將每個 Tailwind 層的
@tailwind
指令加入您的./assets/styles/app.css
檔案。app.css@tailwind base; @tailwind components; @tailwind utilities;
開始您的建置程序
使用
npm run watch
執行您的建置程序。終端機npm run watch
開始在您的專案中使用 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>