Skip to content
全部文档

使用 Angular 安装 Tailwind CSS ​

在 Angular 项目中设置 Tailwind CSS。

创建项目 ​

如果还没有 Angular 项目,先创建一个。最常见的做法是使用 Angular CLI。

Terminal
shell
ng new my-project --style css
cd my-project

安装 Tailwind CSS ​

通过 npm 安装 @tailwindcss/postcss 及其对等依赖。

Terminal
shell
npm install tailwindcss @tailwindcss/postcss postcss --force

配置 PostCSS 插件 ​

在项目根目录创建 .postcssrc.json 文件,并将 @tailwindcss/postcss 插件加入 PostCSS 配置。

.postcssrc.json
js
{
  "plugins": {

    "@tailwindcss/postcss": {}
  }
}

导入 Tailwind CSS ​

在 ./src/styles.css 中添加 @import 以导入 Tailwind CSS。

styles.css
css
@import "tailwindcss";

启动构建 ​

使用 ng serve 运行构建流程。

Terminal
shell
ng serve

在项目中使用 Tailwind ​

app.component.html
html
<h1 class="text-3xl font-bold underline">
  Hello world!
</h1>

文档译文以 MIT 协议授权;原文版权归 Tailwind Labs。湘ICP备2026005453号-2