在 html 中使用 tailwindcss

一、为什么学习?

  • 简单
  • 高效
  • 易学

二、环境准备

  • tailwindcss vscode 插件
  • live server 插件

  • windows wsl2 环境

三、初始化项目

sh 复制代码
cd your_dir
pnpm init

根据自己熟悉的 npm 包管理工具初始化项目,此处使用 pnpm。

四、安装依赖

sh 复制代码
pnpm add tailwindcss -D

tailwindcss 在开发阶段开始使用,所以使用 -D

五、创建 tailwindcss 配置文件

sh 复制代码
npx tailwind init --ts

生成 tailwindcss.config.ts 文件,并初始化配置, 初始化之后在 content 字段中添加 ./index.html:

ts 复制代码
import type { Config } from 'tailwindcss'

export default {
  content: ["./index.html"],
  theme: {
    extend: {},
  },
  plugins: [],
} satisfies Config

content 配置了文件之后,每次构建都会读取 index.html 文件中,tailwindcss 的原子类。

六、添加 tailwindcss 指令

css 复制代码
@tailwind base;
@tailwind components;
@tailwind utilities;

配置 css 文件,

七、编译 tailwindcss

  • 添加脚本
ts 复制代码
{
  "scripts": {
    "build": "npx tailwind -w ./src/styles/tailwind.css -o ./dist/tailwind.css"
  }
}
sh 复制代码
pnpm run build

在 dist 目录下,生成 ./dist/tailwind.css 文件。

八、在 html 中使用

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>tailwindcss</title>
  <link rel="stylesheet" href="./dist/tailwind.css">
</head>
<body>
  <div class="flex">This is tailwind css</div>
</body>
</html>

九、启动服务

使用 vs code 中使用 live server 启动 html 服务。

十、快速示例:flex 的水平垂直居中

html 复制代码
<div class="bg-slate-700 h-[200px] text-cyan-400 flex justify-center items-center">
    This is tailwind css
</div>

十一、小结

本文主要介绍基于 html + pnpm 的 tailwindcss 的基础用法,tailwindcss 生态中的 vscode 插件,cli 命令,配置文件配置。

相关推荐
平头哥AI7 分钟前
Day 01 | go run 跑通第一个 Go 程序,go build 留下一个能拷走的 exe
开发语言·后端·golang
小小尚@42 分钟前
WebGIS/ECharts 地图开发|MapLand 在线行政区划边界一键下载 GeoJSON 工具
前端·javascript·echarts
默_笙1 小时前
🚍 一条 Todo 的奇幻漂流:TypeScript 全栈类型安全的"护照检查"
前端·javascript
ZGG0031 小时前
线程池详解:从 7 大参数到生产避坑
java·数据库·后端
计算机魔术师2 小时前
NVIDIA 以 129.3 亿美元收购 Hugging Face
前端
平头哥技术团队2 小时前
Day 01 | 用 HTML 写第一个网页:双击就能在浏览器打开
前端
geovindu3 小时前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
计算机魔术师3 小时前
两年翻45倍!英伟达靠买买买建成千亿投资帝国
前端
paopaokaka_luck3 小时前
基于springboot3+vue3的精准扶贫管理系统(AI 问答、ECharts 图形化分析)
前端·人工智能·echarts
l1m0_3 小时前
智能电动自行车管理后台实战:AI生成页面与React组件化技巧
前端·react.js·ui·ai·设计