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

相关推荐
CDwenhuohuo5 分钟前
用spark-md5实现切片上传前端起node模拟上传文件大小,消耗时间
前端
阿桂有点桂11 分钟前
React使用笔记(持续更新中)
前端·javascript·react.js·react
自由日记24 分钟前
实例:跳动的心,火柴人
前端·css·css3
柯腾啊32 分钟前
一文简单入门 Axios
前端·axios·apifox
im_AMBER1 小时前
React 15
前端·javascript·笔记·学习·react.js·前端框架
ZZHHWW1 小时前
RocketMQ vs Kafka01 - 存储架构深度对比
后端
How_doyou_do1 小时前
模态框的两种管理思路
java·服务器·前端
snow@li1 小时前
前端:前端/浏览器 可以录屏吗 / 实践 / 录制 Microsoft Edge 标签页、应用窗口、整个屏幕
前端·浏览器录屏·前端录屏·web录屏
依_旧2 小时前
MySQL下载安装配置(超级超级入门级)
java·后端
李贺梖梖2 小时前
CSS学习
前端·css