间距:padding、margin 和 gap
间距类用后面的数字乘以 --spacing(默认 0.25rem),所以 p-4 就是 1rem,任何整数都可以用。
| 作用 | 类名 |
|---|
| 四周内边距 | p-4 |
| 水平和垂直内边距 | px-6 py-2 |
单侧内边距(pt、pr、pb、pl) | pt-8 |
| 行首和行尾的内边距(在从右到左的文字中会翻转) | ps-4 pe-2 |
| 四周外边距 | m-4 |
| 块级元素水平居中 | mx-auto |
| 负外边距 | -mt-2 |
| 子元素之间的垂直间距 | space-y-4 |
| flex 或 grid 项目之间的间隙 | gap-4 |
| 列间隙和行间隙不同 | gap-x-8 gap-y-4 |
| 1 像素,或者 0 | p-px m-0 |
| 精确的值 | mt-[13px] |
尺寸:宽度和高度
| 作用 | 类名 |
|---|
| 间距刻度中的宽度 | w-64 |
| 父元素宽度的几分之几 | w-1/2 |
| 占满父元素的宽度 | w-full |
| 同时设置宽度和高度 | size-10 |
| 高度 | h-12 |
| 占满视口高度,在移动端也可靠 | h-dvh |
| 至少与视口一样高 | min-h-screen |
| 最小宽度和最大宽度 | min-w-0 max-w-md |
| 适合阅读的文本行宽 | max-w-prose |
| 宽度与内容一致 | w-fit |
| 宽高比 | aspect-video |
| 精确的宽度 | w-[350px] |
文字排版
| 作用 | 类名 |
|---|
字号(text-xs 到 text-9xl) | text-lg |
| 字号加行高 | text-lg/7 |
| 字重 | font-semibold |
| 字体 | font-mono |
| 行高 | leading-relaxed |
| 字间距 | tracking-tight |
| 文本对齐 | text-center |
| 对齐到行首(跟随文字方向) | text-start |
| 全部大写 | uppercase |
| 斜体加下划线 | italic underline |
| 下划线偏移和粗细 | underline-offset-4 decoration-2 |
| 截断成一行并显示省略号 | truncate |
| 文本最多显示三行 | line-clamp-3 |
| 让标题各行长度均衡 | text-balance |
| 长单词换行,避免溢出 | wrap-break-word |
| 文本不换行 | whitespace-nowrap |
| 圆点列表 | list-disc list-inside |
颜色
调色板中的每种颜色都有从 50 到 950 的色阶,用 OKLCH 定义。
| 作用 | 类名 |
|---|
| 文字颜色 | text-gray-700 |
| 背景颜色 | bg-sky-500 |
| 边框颜色 | border-red-300 |
| 50% 不透明度的颜色 | bg-black/50 |
| 精确的颜色 | bg-[#1da1f2] |
| 来自 CSS 变量的颜色 | bg-(--brand) |
| 使用当前文字颜色 | border-current |
| 透明背景 | bg-transparent |
| 黑底白字 | text-white bg-black |
| 占位符文字颜色 | placeholder:text-gray-400 |
| 选中文本的颜色 | selection:bg-yellow-200 |
| 光标和表单强调色 | caret-pink-500 accent-pink-500 |
| SVG 填充和描边 | fill-blue-500 stroke-white |
背景与渐变
| 作用 | 类名 |
|---|
| 从左到右的线性渐变 | bg-linear-to-r from-cyan-500 to-blue-500 |
| 带中间色的渐变 | from-indigo-500 via-purple-500 to-pink-500 |
| 带角度的渐变 | bg-linear-45 |
| 渐变的色标位置 | from-10% to-90% |
| 径向渐变 | bg-radial |
| 锥形渐变 | bg-conic |
| 背景图片 | bg-[url(/hero.jpg)] |
| 图片铺满元素并居中 | bg-cover bg-center |
| 图片不重复 | bg-no-repeat |
Flexbox
| 作用 | 类名 |
|---|
| 子元素横向排列 | flex |
| 子元素纵向排列 | flex flex-col |
| 子元素水平垂直居中 | flex items-center justify-center |
| 子元素推到两端 | justify-between |
| 放不下时换行 | flex-wrap |
| 伸展以填满剩余空间 | flex-1 |
| 永不收缩 | shrink-0 |
| 单独对齐某个子元素 | self-end |
| 反转顺序 | flex-row-reverse |
| 把某个子元素移到最前 | order-first |
| flex 项目的初始尺寸 | basis-1/3 |
| 行内 flex 容器 | inline-flex |
Grid
| 作用 | 类名 |
|---|
| 三列等宽 | grid grid-cols-3 |
移动端一列,从 md 起三列 | grid-cols-1 md:grid-cols-3 |
| 跨两列 | col-span-2 |
| 跨满整行 | col-span-full |
| 起止网格线 | col-start-2 col-end-4 |
| 自定义列宽 | grid-cols-[200px_1fr] |
| 页头、内容和页脚三行 | grid-rows-[auto_1fr_auto] |
| 放得下多少列卡片就放多少列 | grid-cols-[repeat(auto-fill,minmax(12rem,1fr))] |
| 项目在单元格中居中 | place-items-center |
| 先填满列再填行 | grid-flow-col |
| 继承父元素的列 | grid-cols-subgrid |
布局与定位
| 作用 | 类名 |
|---|
| 隐藏元素 | hidden |
| 行内块元素 | inline-block |
| 子元素相对于此元素定位 | relative |
| 固定在右上角 | absolute top-0 right-0 |
| 铺满已定位的父元素 | absolute inset-0 |
| 固定在窗口顶部的栏 | fixed inset-x-0 top-0 |
| 滚动时吸附在顶部 | sticky top-0 |
| 叠放在其他元素之上 | z-10 |
| 裁剪溢出的内容 | overflow-hidden |
| 内容溢出时垂直滚动 | overflow-y-auto |
| 裁剪图片以填满容器 | object-cover |
| 居中的页面容器 | container mx-auto px-4 |
| 视觉上隐藏,但保留给屏幕阅读器 | sr-only |
| 文本分栏 | columns-3 |
边框、外环和轮廓
| 作用 | 类名 |
|---|
| 1px 边框 | border |
| 边框宽度 | border-2 |
| 单侧边框 | border-b |
| 虚线边框 | border-dashed |
| 圆角 | rounded-lg |
| 圆形或胶囊形 | rounded-full |
| 只给顶部两个角加圆角 | rounded-t-xl |
| 子元素之间的分隔线 | divide-y divide-gray-200 |
| 元素周围的外环 | ring-2 ring-blue-500 |
| 元素与外环之间的间隙 | ring-offset-2 |
| 元素内部的内环 | inset-ring inset-ring-gray-300 |
| 带偏移的轮廓 | outline-2 outline-offset-2 outline-sky-500 |
| 隐藏轮廓,但在强制颜色模式下保留 | outline-hidden |
阴影与效果
| 作用 | 类名 |
|---|
| 盒子阴影 | shadow-md |
| 更大的阴影 | shadow-xl |
| 彩色阴影 | shadow-lg shadow-cyan-500/50 |
| 内阴影 | inset-shadow-sm |
| 文字阴影 | text-shadow-md |
| 贴合形状的投影 | drop-shadow-lg |
| 不透明度 | opacity-50 |
| 模糊 | blur-sm |
| 毛玻璃 | backdrop-blur-md bg-white/30 |
| 灰度 | grayscale |
| 与背景混合 | mix-blend-multiply |
| 下半部分渐隐 | mask-b-from-50% |
| 手形光标 | cursor-pointer |
| 忽略点击 | pointer-events-none |
| 禁止选中文本 | select-none |
过渡、变换与动画
| 作用 | 类名 |
|---|
| 为颜色、不透明度、阴影和变换的变化添加动画 | transition |
| 只为颜色变化添加动画 | transition-colors |
| 时长和缓动 | duration-300 ease-in-out |
| 延迟 | delay-150 |
| 悬停时放大 | hover:scale-105 |
| 旋转 | rotate-45 |
| 上移自身高度的一半 | -translate-y-1/2 |
| 绝对定位元素居中 | absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 |
| 变换原点 | origin-top-left |
| 3D 旋转(由父元素设置透视) | rotate-x-45 perspective-distant |
| 加载旋转图标 | animate-spin |
| 加载占位效果 | animate-pulse |
| 仅在未开启减弱动态效果时播放动画 | motion-safe:animate-bounce |
| 开启减弱动态效果时关闭过渡 | motion-reduce:transition-none |
| 元素首次出现时淡入 | transition-opacity starting:opacity-0 |
响应式断点
断点采用移动优先:不带前缀的类在所有宽度下都生效,md: 则从该宽度起生效。
| 作用 | 类名 |
|---|
| 40rem(640px)及以上 | sm:flex |
| 48rem(768px)及以上 | md:grid-cols-2 |
| 64rem(1024px)及以上 | lg:px-8 |
| 80rem(1280px)及以上 | xl:text-6xl |
| 96rem(1536px)及以上 | 2xl:max-w-7xl |
| 仅在某个断点以下 | max-md:hidden |
| 两个断点之间 | md:max-lg:flex |
| 一次性的自定义断点 | min-[900px]:grid-cols-4 |
移动端隐藏,从 md 起显示 | hidden md:block |
移动端纵向堆叠,从 md 起横向排列 | flex flex-col md:flex-row |
| 横屏时 | landscape:flex-row |
| 打印时 | print:hidden |
悬停、聚焦和其他状态
| 作用 | 类名 |
|---|
| 悬停时 | hover:bg-sky-700 |
| 键盘聚焦时 | focus-visible:outline-2 |
| 聚焦时 | focus:border-blue-500 |
| 按下时 | active:scale-95 |
| 禁用时 | disabled:opacity-50 |
| 用户编辑过字段后无效时 | user-invalid:border-red-500 |
| 选中的复选框或单选按钮 | checked:bg-blue-600 |
| 第一个和最后一个子元素 | first:pt-0 last:pb-0 |
| 斑马纹行 | odd:bg-white even:bg-gray-50 |
| 除第一个以外的所有子元素 | not-first:mt-4 |
| 在父元素上为所有直接子元素设置样式 | *:rounded-md |
标记了 group 的父元素被悬停时 | group-hover:underline |
| 命名分组 | group-hover/item:visible |
标记了 peer 的兄弟元素无效时 | peer-invalid:visible |
| 元素包含选中的 input 时 | has-checked:bg-indigo-50 |
| ARIA 状态 | aria-expanded:rotate-180 |
| data 属性 | data-[state=open]:bg-gray-100 |
| 元素前后的内容 | after:content-['*'] after:text-red-500 |
打开的 details 或 dialog | open:bg-white |
| 把类标记为 important | bg-red-500! |
深色模式
dark: 默认跟随操作系统的设置。想用类名切换的话,加上下方指令表中的 @custom-variant dark 规则。
| 作用 | 类名 |
|---|
| 深色模式下的背景 | dark:bg-gray-900 |
| 深色模式下的文字 | dark:text-gray-100 |
| 深色模式下的悬停 | dark:hover:bg-gray-800 |
| 原生控件和滚动条使用深色 | scheme-light dark:scheme-dark |
容器查询
先用 @container 标记父元素,再让子元素根据容器的宽度而不是窗口的宽度调整尺寸。
| 作用 | 类名 |
|---|
| 把元素设为容器 | @container |
| 容器宽度 28rem 及以上 | @md:flex-row |
| 容器宽度低于某个值 | @max-md:hidden |
| 两个容器宽度之间 | @sm:@max-lg:grid-cols-2 |
| 给容器命名 | @container/sidebar |
| 查询命名的容器 | @lg/sidebar:block |
| 精确的容器宽度 | @min-[475px]:flex |
| 容器宽度的一半 | w-[50cqw] |
任意值与任意属性
| 作用 | 类名 |
|---|
| 任意值 | top-[117px] |
| 值中的空格写成下划线 | grid-cols-[1fr_500px_2fr] |
| 计算表达式 | w-[calc(100%-2rem)] |
| CSS 变量 | fill-(--brand-color) |
| 带类型提示的 CSS 变量 | text-(length:--title-size) |
| 任意 CSS 属性 | [mask-type:luminance] |
| 为匹配的子元素设置样式 | [&>p]:mt-2 |
| 元素带有某个类时的样式 | [&.is-active]:font-bold |
主题变量与指令
Tailwind CSS 4 在 CSS 中配置,而不是在 tailwind.config.js 中。
| 作用 | CSS |
|---|
| 在样式表中引入 Tailwind CSS | @import "tailwindcss"; |
添加颜色(生成 bg-brand、text-brand 等) | @theme { --color-brand: oklch(0.62 0.19 259); } |
添加字体(生成 font-display) | @theme { --font-display: "Satoshi", sans-serif; } |
添加断点(生成 3xl:) | @theme { --breakpoint-3xl: 120rem; } |
| 修改间距刻度 | @theme { --spacing: 0.3rem; } |
| 替换整套调色板 | @theme { --color-*: initial; --color-ink: #111827; --color-paper: #ffffff; } |
添加动画(生成 animate-wiggle) | @theme { --animate-wiggle: wiggle 1s ease-in-out infinite; @keyframes wiggle { 0%, 100% { rotate: -3deg; } 50% { rotate: 3deg; } } } |
用 dark 类切换深色模式 | @custom-variant dark (&:where(.dark, .dark *)); |
| 用 data 属性切换深色模式 | @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); |
| 自定义工具类 | @utility content-auto { content-visibility: auto; } |
| 在自己的 CSS 中使用工具类 | .btn { @apply rounded-lg px-4 py-2 font-semibold; } |
| 在自己的 CSS 中使用变体 | .card { @variant dark { background: black; } } |
| 在 CSS 中读取主题值 | .note { color: var(--color-gray-500); } |
给所有类加前缀(tw:flex) | @import "tailwindcss" prefix(tw); |
| 扫描 Tailwind 默认会跳过的文件夹 | @source "../node_modules/@acme/ui"; |
| 永不扫描某个文件夹 | @source not "./legacy"; |
| 始终生成某个类 | @source inline("underline"); |
安装与工具
| 作用 | 命令或代码 |
|---|
| 为 Vite 安装 | npm install tailwindcss @tailwindcss/vite |
在 vite.config.ts 中添加 Vite 插件 | plugins: [tailwindcss()] |
| 为 PostCSS 安装 | npm install tailwindcss @tailwindcss/postcss postcss |
postcss.config.mjs 中的 PostCSS 配置 | export default { plugins: { "@tailwindcss/postcss": {} } }; |
| 安装命令行工具 | npm install tailwindcss @tailwindcss/cli |
| 用命令行工具构建并监听变化 | npx @tailwindcss/cli -i ./src/input.css -o ./src/output.css --watch |
| 为生产环境构建压缩后的 CSS | npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --minify |
| 升级 Tailwind CSS 3 项目 | npx @tailwindcss/upgrade |
Tailwind CSS 4 中改名的类
版本 3 的一些名称仍然可用,但现在代表不同的尺寸,比如 shadow-sm;有些只是为了兼容而保留,还有一些已被移除,比如 bg-opacity-50。npx @tailwindcss/upgrade 会帮你完成这些修改。
| Tailwind CSS 3 | Tailwind CSS 4 |
|---|
shadow-sm | shadow-xs |
shadow | shadow-sm |
rounded-sm | rounded-xs |
rounded | rounded-sm |
blur-sm | blur-xs |
drop-shadow-sm | drop-shadow-xs |
outline-none | outline-hidden |
ring | ring-3 |
bg-opacity-50 | bg-black/50 |
flex-shrink-0 | shrink-0 |
flex-grow | grow |
bg-gradient-to-r | bg-linear-to-r |
overflow-ellipsis | text-ellipsis |
decoration-clone | box-decoration-clone |
!font-bold | font-bold! |
bg-[--brand] | bg-(--brand) |
最常用的 Tailwind CSS 类名,都整理在可直接复制的表格里:先是间距、尺寸、文字排版、颜色、flexbox、grid、边框和效果,然后是断点、悬停和聚焦状态、深色模式、容器查询,以及 @theme 和其他 CSS 指令。
这里全部是 Tailwind CSS 4 的语法,表格中的每个 Tailwind CSS 4 类名和 CSS 代码片段都用 Tailwind CSS 4.1 编译器构建过,确认确实存在。从版本 3 迁移过来?最后一张表列出了改名的工具类。想查这些类背后的 CSS 属性,请看 CSS 速查表。