Tailwind_快速参考.md 8.0 KB

Tailwind CSS 快速参考手册

🚀 常用类名速查表

布局

// Flexbox
flex items-center justify-center       // 水平垂直居中
flex-col                               // 垂直排列
flex-1                                 // flex: 1 (填充剩余空间)

// 定位
absolute top-0 left-0                  // 绝对定位
relative                               // 相对定位
fixed bottom-0                         // 固定定位
z-10                                   // z-index: 10

// 尺寸
w-full h-screen                        // 100% 宽度,100vh 高度
w-[450px] h-[80vh]                     // 自定义值
max-w-mobile                           // 最大宽度 450px (自定义)

间距

// 内边距
p-4                                    // padding: 1rem
px-6 py-3                              // 水平 1.5rem,垂直 0.75rem
pt-[18vh]                              // 自定义视口单位

// 外边距
m-4                                    // margin: 1rem
mx-auto                                // 水平居中
mb-8                                   // margin-bottom: 2rem
-mt-4                                  // 负边距

// 间隙
gap-4                                  // grid/flex gap: 1rem

颜色

// 背景
bg-white                               // 白色
bg-[#F5F3F0]                           // 自定义颜色
bg-gradient-to-br from-purple-500 to-indigo-600  // 渐变

// 文字
text-black text-white                  // 黑白色
text-[#007AFF]                         // 自定义颜色

// 透明度
bg-white/10                            // 10% 透明度
text-black/50                          // 50% 透明度

文字

// 字体大小
text-sm text-base text-lg text-xl      // 14px, 16px, 18px, 20px
text-fluid-2xl                         // clamp(24px, 6vw, 32px)
text-[28px]                            // 自定义大小

// 字重
font-normal font-medium font-semibold font-bold

// 对齐
text-center text-left text-right

// 行高
leading-tight leading-normal leading-loose

边框

// 边框
border border-2 border-white           // 边框宽度和颜色
border-t-4                             // 只加顶部边框

// 圆角
rounded rounded-xl rounded-full        // 4px, 12px, 9999px
rounded-t-xl                           // 只加顶部圆角

阴影

shadow shadow-lg shadow-xl             // 阴影大小
shadow-[0_4px_16px_rgba(0,0,0,0.3)]   // 自定义阴影

过渡动画

transition-all duration-300            // 所有属性 0.3s 过渡
transition-colors                      // 只过渡颜色
ease-in-out                            // 缓动函数

// 变换
hover:scale-105                        // 悬停放大
active:scale-95                        // 点击缩小
hover:-translate-y-0.5                 // 悬停上移
rotate-45                              // 旋转 45°

📱 响应式断点

内置断点

// 移动端优先
<div className="
  text-base          /* 默认(移动端): 16px */
  sm:text-lg         /* ≥640px: 18px */
  md:text-xl         /* ≥768px: 20px */
  lg:text-2xl        /* ≥1024px: 24px */
  xl:text-3xl        /* ≥1280px: 30px */
">

自定义断点

// 最大宽度(向下断点)
<div className="max-[375px]:text-sm">
  {/* 屏幕 ≤ 375px 时应用 */}
</div>

// 范围断点
<div className="min-[640px]:max-[768px]:text-lg">
  {/* 仅在 640px-768px 之间应用 */}
</div>

横屏模式

<div className="landscape:h-[60vh]">
  {/* 横屏时应用 */}
</div>

🎨 项目自定义配置

自定义颜色

bg-bg-main          // #E8E8E8
bg-bg-card          // #F5F3F0
bg-accent           // #007AFF

自定义字体大小

text-fluid-xs       // clamp(12px, 3vw, 14px)
text-fluid-sm       // clamp(14px, 3.5vw, 16px)
text-fluid-base     // clamp(16px, 4vw, 18px)
text-fluid-2xl      // clamp(24px, 6vw, 32px)

自定义间距

top-18vh            // top: 18vh
left-5vw            // left: 5vw
pt-32vh             // padding-top: 32vh

自定义尺寸

max-w-mobile        // max-width: 450px
h-input             // height: clamp(48px, 7vh, 56px)
h-button            // height: clamp(44px, 6vh, 52px)

💡 实战示例

1. 页面容器

<div className="mobile-container">
  <div className="content-card">
    {/* 内容 */}
  </div>
</div>

2. 标题区域

<div className="absolute top-[max(164px,18vh)] left-[max(32px,5vw)] right-[max(32px,5vw)]">
  <h1 className="text-fluid-2xl font-medium text-black mb-1">
    What is your name?
  </h1>
  <p className="text-fluid-sm font-medium text-black">
    我应该怎么称呼你呢?
  </p>
</div>

3. 输入框

<input 
  className="w-full h-input px-4 text-fluid-sm 
             bg-white rounded-t-xl border-none outline-none
             focus:ring-2 focus:ring-accent
             placeholder:text-gray-400"
  placeholder="输入名字"
/>

4. 按钮

<button className="
  px-8 py-[18px] bg-gradient-to-br from-[#667eea] to-[#764ba2]
  text-white text-lg font-semibold rounded-[28px]
  shadow-[0_4px_16px_rgba(102,126,234,0.3)]
  transition-all duration-300
  hover:-translate-y-0.5 hover:shadow-[0_6px_20px_rgba(102,126,234,0.4)]
  active:translate-y-0 active:scale-[0.98]
">
  点击按钮
</button>

5. 卡片

<div className="
  p-6 bg-white rounded-xl shadow-lg
  hover:shadow-2xl transition-shadow duration-300
">
  {/* 卡片内容 */}
</div>

6. 渐变背景

<div className="
  w-screen h-screen 
  bg-gradient-to-br from-[#667eea] to-[#764ba2]
  relative overflow-hidden
">
  {/* 内容 */}
</div>

7. 加载动画

<div className="
  w-20 h-20 border-4 border-transparent border-t-white
  rounded-full animate-spin
" />

8. 图片容器

<img 
  src={image} 
  alt="描述"
  className="
    w-full h-full max-h-[45vh] 
    object-contain object-[center_bottom]
    sm:max-h-[50vh]
  " 
/>

🔧 高级技巧

1. 组合伪类

<button className="
  hover:bg-blue-600 
  active:bg-blue-700 
  disabled:opacity-50 
  disabled:cursor-not-allowed
">
  按钮
</button>

2. 分组悬停

<div className="group">
  <div className="group-hover:scale-110 transition-transform">
    {/* 父元素悬停时子元素放大 */}
  </div>
</div>

3. 对等元素

<input type="checkbox" className="peer" />
<label className="peer-checked:text-blue-600">
  {/* 复选框选中时标签变蓝 */}
</label>

4. 任意选择器

<div className="[&>p]:text-red-500">
  {/* 所有直接子 <p> 元素为红色 */}
  <p>红色文本</p>
</div>

5. 条件类名

import clsx from 'clsx';

<div className={clsx(
  'base-class',
  isActive && 'text-blue-600',
  isDisabled && 'opacity-50'
)}>

⚠️ 常见问题

Q: 如何使用 max() 函数?

<div className="top-[max(164px,18vh)]">
  {/* 方括号语法支持任意 CSS 值 */}
</div>

Q: 如何添加文本阴影?

<h1 className="text-shadow-[0_2px_10px_rgba(0,0,0,0.3)]">
  {/* Tailwind v4 需用方括号 */}
</h1>

Q: 如何创建复杂动画?

// 方法1: 内联 <style>
<style>{`
  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }
`}</style>
<div className="animate-[fadeIn_0.5s_ease-out]" />

// 方法2: tailwind.config.js
animation: {
  'fade-in': 'fadeIn 0.5s ease-out',
}

Q: 如何处理 iOS 安全区域?

<div className="safe-top safe-bottom">
  {/* 使用自定义类 */}
</div>

{/* 或内联 */}
<div className="pt-[env(safe-area-inset-top)]" />

🔗 参考链接


提示: 使用 VS Code + Tailwind CSS IntelliSense 插件可获得自动补全 🚀