905 字
5 分钟
TailwindCSS常用样式
Tailwind CSS 常用样式速查手册 ✅
目录 📚
- 基础布局 (Layout)
- 间距 (Spacing)
- 排版 (Typography)
- 背景与边框 (Background & Border)
- Flex 与 Grid 布局
- 尺寸与定位 (Sizing & Positioning)
- 交互状态与伪类 (States & Pseudo)
- 响应式与变体 (Responsive & Variants)
- 动画、过渡与变换 (Animation/Transition/Transform)
- 表单、可访问性与实用工具 (Forms & Accessibility)
- 配置、插件与常用模式 (Config & Plugins)
基础布局 (Layout)
- 容器:
container(配合max-width自动响应) - 显示类型:
block、inline-block、inline、flex、grid、hidden - 可见性/显示控制:
sr-only(屏幕阅读器专用)、visible/invisible
示例:
<div class="container mx-auto px-4"> <div class="block md:flex gap-4">...</div></div>间距 (Spacing)
- 外边距(margin):
m-{n}、mx-{n}、my-{n}、mt-2、-mt-1(支持负值) - 内边距(padding):
p-{n}、px-{n}、py-{n}、pt-4 - 常用尺寸:
0, 0.5, 1, 1.5, 2, 3, 4, 6, 8, 10, 12, 16, 20, 24(以配置为准)
示例:
<button class="px-4 py-2 bg-blue-600 text-white rounded-md">按钮</button>排版 (Typography)
- 字体大小:
text-xs、text-sm、text-base、text-lg、text-xl… - 行高:
leading-tight、leading-normal、leading-loose - 字重:
font-thin、font-light、font-normal、font-medium、font-semibold、font-bold - 字体颜色:
text-gray-700、text-red-500等 - 文本对齐:
text-left、text-center、text-right、text-justify - 溢出处理:
truncate、overflow-ellipsis、whitespace-nowrap
示例:
<h1 class="text-3xl font-bold leading-tight">标题文字</h1><p class="text-base text-gray-600">段落文字示例,支持响应式字号。</p>背景与边框 (Background & Border)
- 背景色:
bg-white、bg-gray-100、bg-gradient-to-r from-blue-400 to-pink-500 - 背景图与定位:
bg-cover、bg-center、bg-no-repeat - 边框:
border、border-2、border-dashed、border-gray-300 - 圆角:
rounded、rounded-sm、rounded-md、rounded-full - 阴影:
shadow、shadow-md、shadow-lg、shadow-none
示例:
<div class="bg-white border rounded-md shadow p-6">卡片内容</div>Flex 与 Grid 布局
- Flex:
flex、flex-row、flex-col、items-center、justify-center、gap-4、flex-wrap - Grid:
grid、grid-cols-2、grid-cols-3、gap-x-4、gap-y-6、col-span-2
示例:
<div class="grid grid-cols-3 gap-6"> <div class="col-span-2">主要内容</div> <div>侧边</div></div>尺寸与定位 (Sizing & Positioning)
- 宽高:
w-4、w-1/2、w-full、h-8、h-screen - 最小/最大:
min-w-0、max-w-lg、max-h-screen - 定位:
relative、absolute、fixed、top-0、right-4、inset-0 - 对象适应:
object-cover、object-contain
示例:
<img class="w-full h-64 object-cover rounded" src="..." alt="示例" />交互状态与伪类 (States & Pseudo)
- 悬停:
hover:bg-blue-700、hover:scale-105 - 激活/按下:
active:opacity-75 - 焦点:
focus:outline-none、focus:ring-2、focus:ring-blue-300 - 已禁用:
disabled:opacity-50(需要配合变体启用)
示例:
<a class="px-4 py-2 bg-blue-500 hover:bg-blue-600 focus:ring-4 ring-blue-200 rounded text-white" >链接</a>响应式与变体 (Responsive & Variants)
- 响应式前缀:
sm:、md:、lg:、xl:、2xl:(优先级从小到大) - 复合变体:
md:hover:bg-red-500、focus-visible:
示例:
<div class="p-4 sm:p-6 md:p-8 lg:p-10">响应式内边距</div><button class="text-sm md:text-base">响应式字号</button>动画、过渡与变换 (Animation/Transition/Transform)
- 变换:
transform、scale-105、translate-x-2、rotate-3 - 过渡:
transition、transition-colors、duration-200、ease-in-out - 动画:
animate-spin、animate-pulse、animate-bounce或自定义@keyframes
示例:
<div class="transform hover:scale-105 transition duration-200">卡片</div>表单、可访问性与实用工具 (Forms & Accessibility)
- 表单插件:安装
@tailwindcss/forms可得到更一致的表单样式 - 辅助工具:
sr-only、aria-*(无障碍属性需和 HTML 一起用) - 文本溢出与裁剪:
truncate、line-clamp-{n}(需启用 plugin)
示例:
<input class="form-input mt-1 block w-full rounded-md border-gray-300" />小贴士:始终为交互元素提供可见的
:focus指示(focus:ring)以提高键盘可访问性。
配置、插件与常用模式 (Config & Plugins)
Tailwind 配置要点(tailwind.config.js)
module.exports = { content: ["./src/**/*.{html,js,jsx,ts,tsx,astro}"], theme: { extend: { colors: { brand: "#1e40af", }, }, }, plugins: [ require("@tailwindcss/forms"), require("@tailwindcss/typography"), require("@tailwindcss/line-clamp"), ],};content(或旧名purge)用于移除未使用的样式(生产构建体积关键)extend用于扩展颜色、间距、字体等- 常用插件:
forms、typography、line-clamp、aspect-ratio等
使用 @apply 提取公用类(在 CSS/SCSS 中)
.btn-primary { @apply px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700;}注意:
@apply不能用于复合变体(如hover:的嵌套复杂场景需谨慎)。
常见问题与最佳实践 💡
- 使用 JIT 模式(Tailwind v2.1+ 为可选,v3 默认)可以得到即时类生成和更小的 CSS 体积。
- 在
content中包含所有模板类型(如.astro,.vue,.jsx等),以免被错误地 tree-shaking。 - 优先使用语义化类组合(如
prose、container、sr-only)来保持可读性。 - 对于复杂组件,使用
@apply或抽象成组件(React/Vue/Astro)会更易维护。
快速示例集(常用场景)
- 导航栏:
<nav class="bg-white border-b"> <div class="container mx-auto flex items-center justify-between px-4 py-3"> <a class="text-xl font-semibold">Logo</a> <div class="hidden md:flex gap-4"> <a class="text-gray-700 hover:text-black">产品</a> <a class="text-gray-700 hover:text-black">定价</a> </div> </div></nav>- 响应式卡片:
<div class="grid gap-6 sm:grid-cols-2 lg:grid-cols-3"> <div class="bg-white rounded-lg shadow p-6">卡片内容</div></div>扩展阅读与参考 📖
- 官方文档: https://tailwindcss.com
- 插件文档:
@tailwindcss/forms、@tailwindcss/typography、@tailwindcss/line-clamp
- 使用框架集成:React / Vue / Svelte / Astro
分享
如果这篇文章对你有帮助,欢迎分享给更多人!
部分信息可能已经过时









