Installation
在 Symfony 中安装 Tailwind CSS
在 Symfony 项目中设置 Tailwind CSS。

创建你的项目
如果你还没有设置 Symfony 项目,可以先创建一个新的 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@3 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>

