Unocss / Tailwind CSS 完整速查指南
本指南基于 Tailwind CSS 编写,适用于 Unocss。旨在提供全面的常用及生僻 Class 速查与实战范例。
- 布局 (Layout)
- Flexbox & Grid
- 间距 (Spacing)
- 尺寸 (Sizing)
- 排版 (Typography)
- 背景 (Backgrounds)
- 边框 (Borders)
- 效果 (Effects) & 滤镜 (Filters)
- 过渡与动画 (Transitions & Animation)
- 实战组件范例 (Examples)
| Class | CSS | 说明 |
|---|
| Display | | |
block | display: block; | 块级元素 |
inline-block | display: inline-block; | 行内块级 |
inline | display: inline; | 行内元素 |
flex | display: flex; | 弹性容器 |
grid | display: grid; | 网格容器 |
hidden | display: none; | 隐藏元素 |
| Position | | |
static | position: static; | 默认定位 |
relative | position: relative; | 相对定位 |
absolute | position: absolute; | 绝对定位 |
fixed | position: fixed; | 固定定位 (相对于视口) |
sticky | position: sticky; | 粘性定位 (吸顶/吸底) |
| Class | CSS | 说明 |
|---|
inset-0 | top:0; right:0; bottom:0; left:0; | 占满容器 |
top-0, left-4 | top: 0px;, left: 1rem; | 距离定位 |
z-0 ~ z-50 | z-index: 0/10/20/30/40/50; | 层级 (可自定义如 z-100) |
z-auto | z-index: auto; | 自动层级 |
| Class | CSS | 说明 |
|---|
overflow-auto | overflow: auto; | 自动滚动条 |
overflow-hidden | overflow: hidden; | 隐藏溢出 |
overflow-x-scroll | overflow-x: scroll; | 强制横向滚动 |
visible / invisible | visibility: visible / hidden; | 可见性 (保留占位) |
| Class | CSS | 说明 |
|---|
flex-row | flex-direction: row; | 水平排列 (默认) |
flex-col | flex-direction: column; | 垂直排列 |
flex-row-reverse | flex-direction: row-reverse; | 水平反向 |
flex-wrap | flex-wrap: wrap; | 允许换行 |
flex-nowrap | flex-wrap: nowrap; | 禁止换行 |
| Class | CSS | 说明 |
|---|
flex-1 | flex: 1 1 0%; | 自动填满剩余空间 |
flex-auto | flex: 1 1 auto; | 根据内容自动伸缩 |
flex-none | flex: none; | 禁止伸缩 (常用于头部/侧边栏) |
grow | flex-grow: 1; | 允许放大 |
shrink-0 | flex-shrink: 0; | 禁止缩小 (防止图片/图标被挤压) |
| Class | CSS | 说明 |
|---|
| Justify Content (主轴) | justify-content: ... | |
justify-start | flex-start | 起始对齐 |
justify-center | center | 居中 |
justify-end | flex-end | 末尾对齐 |
justify-between | space-between | 两端对齐 |
justify-around | space-around | 环绕对齐 |
| Align Items (交叉轴) | align-items: ... | |
items-start | flex-start | 顶部对齐 |
items-center | center | 垂直居中 |
items-end | flex-end | 底部对齐 |
items-stretch | stretch | 拉伸填满 (默认) |
| Class | CSS | 说明 |
|---|
grid-cols-1 ~ 12 | grid-template-columns: repeat(n, minmax(0, 1fr)); | 定义列数 |
col-span-2 | grid-column: span 2 / span 2; | 跨列合并 |
col-start-1 | grid-column-start: 1; | 起始列位置 |
gap-4 | gap: 1rem; | 网格间距 |
gap-x-2 / gap-y-4 | column-gap / row-gap | 横向/纵向间距 |
p (padding), m (margin)
后缀: t(top), r(right), b(bottom), l(left), x(left+right), y(top+bottom)
| Class | 值 (rem / px) | 说明 |
|---|
p-0 | 0px | 无内边距 |
p-1 | 0.25rem / 4px | 微小间距 |
p-2 | 0.5rem / 8px | 小间距 |
p-4 | 1rem / 16px | 标准间距 |
px-6 | 1.5rem / 24px | 水平间距 |
py-10 | 2.5rem / 40px | 垂直间距 |
-m-2 | -0.5rem | 负边距 (常用) |
mx-auto | auto | 水平居中 (需配合定宽) |
space-x-4 | | 子元素之间的水平间距 (加在父级) |
space-y-2 | | 子元素之间的垂直间距 (加在父级) |
| Class | CSS | 说明 |
|---|
w-0 ~ w-96 | | 固定宽度 (类似 Spacing 比例) |
w-1/2, w-1/3 | width: 50%, 33.333% | 百分比宽度 |
w-full | width: 100%; | 占满父容器 |
w-screen | width: 100vw; | 占满视口宽度 |
w-min / w-max | min-content / max-content | 内容适应宽度 |
w-[100px] | width: 100px; | 自定义数值 (JIT) |
h-full | height: 100%; | 占满父容器高度 (父容器需有高度) |
h-screen | height: 100vh; | 占满视口高度 |
| Class | CSS | 说明 |
|---|
max-w-xs ~ 7xl | max-width: ... | 限制最大宽 (rem) |
max-w-full | max-width: 100%; | 响应式图片常用 |
min-h-screen | min-height: 100vh; | 最小高度为屏幕高 |
min-w-0 | min-width: 0; | 修复 Flex 子项溢出问题 |
| Class | CSS | 说明 |
|---|
text-xs | 12px | 极小 |
text-sm | 14px | 小号 (常用正文) |
text-base | 16px | 基准 (浏览器默认) |
text-lg | 18px | 大号 |
text-xl ~ 9xl | | 标题大小 |
font-thin | 100 | 极细 |
font-normal | 400 | 正常 |
font-medium | 500 | 中等 |
font-bold | 700 | 粗体 |
font-black | 900 | 极粗 |
| Class | 说明 |
|---|
text-gray-500 | 灰色文字 |
text-primary | 主题色 (若配置) |
text-left / center / right / justify | 对齐方式 |
underline | 下划线 |
line-through | 删除线 |
no-underline | 无修饰 |
uppercase / lowercase / capitalize | 大写 / 小写 / 首字母大写 |
truncate | 单行溢出省略 (...) |
line-clamp-2 | 限制显示2行并省略 (需插件或原生支持) |
| Class | CSS | 说明 |
|---|
leading-none | 1 | 无行高 |
leading-tight | 1.25 | 紧凑 |
leading-normal | 1.5 | 正常 |
leading-loose | 2 | 宽松 |
tracking-tighter | -0.05em | 紧缩字距 |
tracking-wide | 0.025em | 宽字距 |
| Class | CSS | 说明 |
|---|
bg-white | background-color: #fff; | 背景色 |
bg-opacity-50 | | 背景透明度 |
bg-cover | background-size: cover; | 覆盖填充 |
bg-contain | background-size: contain; | 包含 |
bg-center | background-position: center; | 居中 |
bg-no-repeat | background-repeat: no-repeat; | 不重复 |
| Gradients | | |
bg-gradient-to-r | linear-gradient(to right, ...) | 向右渐变 |
from-blue-500 | | 起始色 |
via-purple-500 | | 中间色 (可选) |
to-pink-500 | | 结束色 |
| Class | CSS | 说明 |
|---|
rounded-none | 0px | 直角 |
rounded-sm | 2px | 小圆角 |
rounded | 4px | 标准圆角 |
rounded-md | 6px | 中圆角 |
rounded-lg | 8px | 大圆角 |
rounded-xl ~ 3xl | | 超大圆角 |
rounded-full | 9999px | 胶囊/圆形 |
| Class | CSS | 说明 |
|---|
border | border-width: 1px; | 1px 边框 (默认) |
border-0 / 2 / 4 / 8 | | 指定宽度 |
border-t-2 | | 仅顶部边框 |
border-red-500 | | 红色边框 |
border-dashed | border-style: dashed; | 虚线 |
divide-y | | 子元素之间添加分隔线 (父级用) |
divide-gray-200 | | 分隔线颜色 |
| Class | 说明 |
|---|
ring-2 | 添加 2px 外环 (不占空间) |
ring-offset-2 | 外环偏移 2px |
ring-blue-500 | 外环颜色 |
| Class | CSS | 说明 |
|---|
| Shadow | | |
shadow-sm | | 小阴影 |
shadow | | 默认阴影 |
shadow-lg | | 大阴影 |
shadow-inner | | 内阴影 |
shadow-none | | 无阴影 |
| Opacity | | |
opacity-0 | 0 | 全透明 |
opacity-100 | 1 | 不透明 |
| Filters | | |
blur-sm | filter: blur(4px); | 模糊 |
grayscale | filter: grayscale(100%); | 灰度 |
backdrop-blur-md | | 背景毛玻璃 (需背景半透明) |
| Mix Blend | | |
mix-blend-multiply | | 正片叠底 |
mix-blend-screen | | 滤色 |
| Class | CSS | 说明 |
|---|
transition | transition-property: ... | 开启默认属性过渡 |
transition-all | | 所有属性过渡 |
transition-colors | | 仅颜色过渡 |
duration-300 | 300ms | 持续时间 |
delay-100 | 100ms | 延迟 |
ease-in-out | | 缓动函数 |
| Class | 说明 |
|---|
animate-spin | 旋转 (Loading常用) |
animate-ping | 扩散 (消息提醒点) |
animate-pulse | 脉冲/呼吸 (骨架屏) |
animate-bounce | 上下弹跳 |
包含图片、标题、描述、标签和悬停效果。
<div class="max-w-sm rounded-lg overflow-hidden shadow-lg bg-white hover:shadow-2xl transition-shadow duration-300">
<!-- 图片区域 -->
<img class="w-full h-48 object-cover" src="/img/card-top.jpg" alt="Sunset">
<!-- 内容区域 -->
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2 text-gray-800">The Coldest Sunset</div>
<p class="text-gray-700 text-base line-clamp-3">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quia, nulla!
Maiores et perferendis eaque, exercitationem praesentium nihil.
</p>
</div>
<!-- 标签区域 -->
<div class="px-6 pt-4 pb-2">
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span>
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span>
</div>
</div>
Flex 布局,两端对齐,移动端适应。
<nav class="flex items-center justify-between flex-wrap bg-teal-500 p-6">
<!-- Logo -->
<div class="flex items-center flex-shrink-0 text-white mr-6">
<span class="font-semibold text-xl tracking-tight">Tailwind CSS</span>
</div>
<!-- Menu Links -->
<div class="w-full block flex-grow lg:flex lg:items-center lg:w-auto">
<div class="text-sm lg:flex-grow">
<a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">
Docs
</a>
<a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">
Examples
</a>
</div>
<!-- Button -->
<div>
<a href="#" class="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white transition mt-4 lg:mt-0">
Download
</a>
</div>
</div>
</nav>
带有 Ring 聚焦效果和标签。
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="username">
Username
</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition"
id="username"
type="text"
placeholder="Enter your username"
>
</div>
最常用的水平垂直居中技巧。
<!-- Grid 方式 -->
<div class="grid place-items-center h-screen bg-gray-100">
<div class="bg-white p-10 rounded shadow">Centered Content</div>
</div>
<!-- Flex 方式 -->
<div class="flex items-center justify-center h-screen bg-gray-100">
<div class="bg-white p-10 rounded shadow">Centered Content</div>
</div>
<!-- 主按钮 -->
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition">
Button
</button>
<!-- 幽灵按钮 -->
<button class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded transition">
Button
</button>
<!-- 禁用状态 -->
<button class="bg-blue-500 text-white font-bold py-2 px-4 rounded opacity-50 cursor-not-allowed">
Disabled
</button>