在 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 命令,配置文件配置。

相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
三十而立洋3 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
大勇前进3 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
yuzhi_liu3 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile3 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白803 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端