tailwindcss-fundamentals-v4

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Tailwind CSS v4 Fundamentals (2025/2026)

Tailwind CSS v4 基础知识(2025/2026)

Overview

概述

Tailwind CSS v4.0 was released January 22, 2025, featuring a complete rewrite with a Rust-based engine, CSS-first configuration, and significant performance improvements.
Tailwind CSS v4.0 于2025年1月22日发布,采用基于Rust的引擎进行完全重写,支持CSS优先配置,性能大幅提升。

Key Changes from v3

与v3版本的主要变化

Featurev3v4
ConfigurationJavaScript (tailwind.config.js)CSS-first (@theme directive)
EngineNode.jsRust (10x faster)
Color formathex/rgbOKLCH (perceptually uniform)
PluginsJS files@plugin directive
Custom utilitiesJS config@utility directive
PostCSS importspostcss-importBuilt-in
AutoprefixerRequiredBuilt-in
CSS nestingpostcss-nestedBuilt-in
Content detectionExplicit configAutomatic
特性v3v4
配置方式JavaScript(tailwind.config.js)CSS优先(@theme 指令)
底层引擎Node.jsRust(速度提升10倍)
颜色格式十六进制/RGBOKLCH(感知均匀色彩)
插件机制JS文件@plugin 指令
自定义工具类JS配置@utility 指令
PostCSS 导入依赖postcss-import内置支持
浏览器前缀需要Autoprefixer内置支持
CSS嵌套依赖postcss-nested内置支持
内容检测需显式配置自动检测

Installation

安装

Vite Projects (Recommended)

Vite 项目(推荐)

bash
npm install -D tailwindcss @tailwindcss/vite
javascript
// vite.config.js
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [tailwindcss()]
})
bash
npm install -D tailwindcss @tailwindcss/vite
javascript
// vite.config.js
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [tailwindcss()]
})

PostCSS Projects

PostCSS 项目

bash
npm install -D tailwindcss @tailwindcss/postcss
javascript
// postcss.config.mjs
export default {
  plugins: {
    '@tailwindcss/postcss': {}
  }
}
bash
npm install -D tailwindcss @tailwindcss/postcss
javascript
// postcss.config.mjs
export default {
  plugins: {
    '@tailwindcss/postcss': {}
  }
}

CSS Entry Point

CSS 入口文件

css
/* app.css - The only required CSS file */
@import "tailwindcss";
css
/* app.css - 唯一需要的CSS文件 */
@import "tailwindcss";

CSS-First Configuration

CSS优先配置

The @theme Directive

@theme 指令

Replace tailwind.config.js with CSS-based configuration:
css
@import "tailwindcss";

@theme {
  /* Colors using modern oklch */
  --color-primary: oklch(0.6 0.2 250);
  --color-secondary: oklch(0.7 0.15 180);
  --color-accent: oklch(0.8 0.2 30);

  /* Typography */
  --font-display: "Satoshi", sans-serif;
  --font-body: "Inter", sans-serif;

  /* Custom spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 2rem;
  --spacing-xl: 4rem;

  /* Custom breakpoints */
  --breakpoint-xs: 475px;
  --breakpoint-3xl: 1920px;
}
使用基于CSS的配置替代tailwind.config.js:
css
@import "tailwindcss";

@theme {
  /* 使用现代OKLCH格式定义颜色 */
  --color-primary: oklch(0.6 0.2 250);
  --color-secondary: oklch(0.7 0.15 180);
  --color-accent: oklch(0.8 0.2 30);

  /* 排版配置 */
  --font-display: "Satoshi", sans-serif;
  --font-body: "Inter", sans-serif;

  /* 自定义间距 */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 2rem;
  --spacing-xl: 4rem;

  /* 自定义断点 */
  --breakpoint-xs: 475px;
  --breakpoint-3xl: 1920px;
}

Theme Variables Reference

主题变量参考

CategoryVariable PatternExample
Colors
--color-*
--color-brand-500
Fonts
--font-*
--font-heading
Spacing
--spacing-*
--spacing-4
Breakpoints
--breakpoint-*
--breakpoint-3xl
Radius
--radius-*
--radius-lg
Shadows
--shadow-*
--shadow-xl
Animations
--animate-*
--animate-fade-in
Easing
--ease-*
--ease-bounce
分类变量模式示例
颜色
--color-*
--color-brand-500
字体
--font-*
--font-heading
间距
--spacing-*
--spacing-4
断点
--breakpoint-*
--breakpoint-3xl
圆角
--radius-*
--radius-lg
阴影
--shadow-*
--shadow-xl
动画
--animate-*
--animate-fade-in
缓动函数
--ease-*
--ease-bounce

Disabling Defaults

禁用默认主题

css
@theme {
  /* Start fresh - disable all default theme values */
  --*: initial;

  /* Define only what you need */
  --spacing: 4px;
  --font-body: Inter, sans-serif;
  --color-primary: oklch(0.72 0.11 221.19);
  --color-secondary: oklch(0.74 0.17 40.24);
}
css
@theme {
  /* 从零开始 - 禁用所有默认主题值 */
  --*: initial;

  /* 仅定义所需内容 */
  --spacing: 4px;
  --font-body: Inter, sans-serif;
  --color-primary: oklch(0.72 0.11 221.19);
  --color-secondary: oklch(0.74 0.17 40.24);
}

Custom Utilities

自定义工具类

Static Utilities

静态工具类

css
/* Define custom utility in CSS */
@utility content-auto {
  content-visibility: auto;
}

@utility text-balance {
  text-wrap: balance;
}

@utility scrollbar-hide {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

@utility scrollbar-hide::-webkit-scrollbar {
  display: none;
}
Usage:
html
<div class="content-auto scrollbar-hide">
  <h1 class="text-balance">Long headline that should balance nicely</h1>
</div>
css
/* 在CSS中定义自定义工具类 */
@utility content-auto {
  content-visibility: auto;
}

@utility text-balance {
  text-wrap: balance;
}

@utility scrollbar-hide {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

@utility scrollbar-hide::-webkit-scrollbar {
  display: none;
}
使用示例:
html
<div class="content-auto scrollbar-hide">
  <h1 class="text-balance">需要自动平衡的长标题</h1>
</div>

Functional Utilities

功能性工具类

css
/* Utility that accepts values */
@utility tab-* {
  tab-size: --value(integer);
}

@utility text-shadow-* {
  text-shadow: 0 0 --value([length]) currentColor;
}

/* With theme reference */
@utility gap-safe-* {
  gap: max(--value(--spacing-*), env(safe-area-inset-bottom));
}
Usage:
html
<pre class="tab-4">Code with 4-space tabs</pre>
<h1 class="text-shadow-[2px]">Glowing text</h1>
<div class="gap-safe-4">Safe area aware gap</div>
css
/* 支持传值的工具类 */
@utility tab-* {
  tab-size: --value(integer);
}

@utility text-shadow-* {
  text-shadow: 0 0 --value([length]) currentColor;
}

/* 关联主题变量的工具类 */
@utility gap-safe-* {
  gap: max(--value(--spacing-*), env(safe-area-inset-bottom));
}
使用示例:
html
<pre class="tab-4">使用4空格缩进的代码</pre>
<h1 class="text-shadow-[2px]">带发光效果的文字</h1>
<div class="gap-safe-4">适配安全区域的间距</div>

Custom Variants

自定义变体

The @custom-variant Directive

@custom-variant 指令

css
/* Dark mode with selector */
@custom-variant dark (&:where(.dark, .dark *));

/* Custom state variants */
@custom-variant hocus (&:hover, &:focus);
@custom-variant group-hocus (:merge(.group):hover &, :merge(.group):focus &);

/* Data attribute variants */
@custom-variant data-loading (&[data-loading="true"]);
@custom-variant data-active (&[data-state="active"]);

/* Child selectors */
@custom-variant children (& > *);
@custom-variant not-first (& > *:not(:first-child));
Usage:
html
<button class="hocus:bg-blue-600">Hover or focus</button>
<div class="data-loading:opacity-50" data-loading="true">Loading...</div>
<ul class="children:border-b not-first:pt-2">
  <li>Item 1</li>
  <li>Item 2</li>
</ul>
css
/* 带选择器的暗色模式 */
@custom-variant dark (&:where(.dark, .dark *));

/* 自定义状态变体 */
@custom-variant hocus (&:hover, &:focus);
@custom-variant group-hocus (:merge(.group):hover &, :merge(.group):focus &);

/* 数据属性变体 */
@custom-variant data-loading (&[data-loading="true"]);
@custom-variant data-active (&[data-state="active"]);

/* 子元素选择器变体 */
@custom-variant children (& > *);
@custom-variant not-first (& > *:not(:first-child));
使用示例:
html
<button class="hocus:bg-blue-600">悬停或聚焦时变色</button>
<div class="data-loading:opacity-50" data-loading="true">加载中...</div>
<ul class="children:border-b not-first:pt-2">
  <li>项目1</li>
  <li>项目2</li>
</ul>

Loading Plugins

加载插件

The @plugin Directive

@plugin 指令

css
@import "tailwindcss";

/* Load official plugins */
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
@plugin "@tailwindcss/container-queries";

/* Load local plugin */
@plugin "./my-plugin.js";
css
@import "tailwindcss";

/* 加载官方插件 */
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
@plugin "@tailwindcss/container-queries";

/* 加载本地插件 */
@plugin "./my-plugin.js";

Plugin with Options

带配置项的插件

css
@plugin "@tailwindcss/typography" {
  className: wysiwyg;
}

@plugin "@tailwindcss/forms" {
  strategy: class;
}
css
@plugin "@tailwindcss/typography" {
  className: wysiwyg;
}

@plugin "@tailwindcss/forms" {
  strategy: class;
}

Using Prefixes

使用前缀

css
@import "tailwindcss" prefix(tw);

@theme {
  /* Define without prefix */
  --font-display: "Satoshi", sans-serif;
}
html
<!-- Use with prefix -->
<div class="tw:flex tw:bg-red-500 tw:hover:bg-red-600">
  Content
</div>
Generated CSS variables include prefix:
css
:root {
  --tw-font-display: "Satoshi", sans-serif;
}
css
@import "tailwindcss" prefix(tw);

@theme {
  /* 定义时无需添加前缀 */
  --font-display: "Satoshi", sans-serif;
}
html
/* 使用时添加前缀 */
<div class="tw:flex tw:bg-red-500 tw:hover:bg-red-600">
  内容
</div>
生成的CSS变量会自动包含前缀:
css
:root {
  --tw-font-display: "Satoshi", sans-serif;
}

CSS Variables in Code

代码中使用CSS变量

Replace theme() with var()

用var()替代theme()

css
/* v3 approach (deprecated) */
.old-way {
  background-color: theme(colors.red.500);
}

/* v4 approach */
.new-way {
  background-color: var(--color-red-500);
}

/* In media queries, use CSS variable names */
@media (width >= theme(--breakpoint-xl)) {
  /* styles */
}
css
/* v3 写法(已废弃) */
.old-way {
  background-color: theme(colors.red.500);
}

/* v4 写法 */
.new-way {
  background-color: var(--color-red-500);
}

/* 媒体查询中使用CSS变量名 */
@media (width >= theme(--breakpoint-xl)) {
  /* 样式代码 */
}

Core Utility Categories

核心工具类分类

Layout

布局

html
<!-- Flexbox -->
<div class="flex flex-col md:flex-row items-center justify-between gap-4">

<!-- Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">

<!-- Container -->
<div class="container mx-auto px-4">
html
<!-- Flexbox 布局 -->
<div class="flex flex-col md:flex-row items-center justify-between gap-4">

<!-- Grid 布局 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">

<!-- 容器 -->
<div class="container mx-auto px-4">

Spacing

间距

html
<!-- Padding -->
<div class="p-4 px-6 py-2">

<!-- Margin -->
<div class="m-4 mx-auto my-8">

<!-- Gap -->
<div class="gap-4 gap-x-6 gap-y-2">
html
<!-- 内边距 -->
<div class="p-4 px-6 py-2">

<!-- 外边距 -->
<div class="m-4 mx-auto my-8">

<!-- 间隙 -->
<div class="gap-4 gap-x-6 gap-y-2">

Typography

排版

html
<!-- Font size -->
<p class="text-sm md:text-base lg:text-lg">

<!-- Font weight -->
<h1 class="font-bold">

<!-- Text color -->
<p class="text-gray-600 dark:text-gray-300">

<!-- Line height -->
<p class="leading-relaxed">
html
<!-- 字体大小 -->
<p class="text-sm md:text-base lg:text-lg">

<!-- 字体粗细 -->
<h1 class="font-bold">

<!-- 文字颜色 -->
<p class="text-gray-600 dark:text-gray-300">

<!-- 行高 -->
<p class="leading-relaxed">

Colors

颜色

html
<!-- Background -->
<div class="bg-white dark:bg-gray-900">

<!-- Text -->
<p class="text-blue-600">

<!-- Border -->
<div class="border border-gray-200">

<!-- Ring -->
<button class="focus:ring-2 focus:ring-blue-500">
html
<!-- 背景色 -->
<div class="bg-white dark:bg-gray-900">

<!-- 文字颜色 -->
<p class="text-blue-600">

<!-- 边框颜色 -->
<div class="border border-gray-200">

<!-- 焦点环 -->
<button class="focus:ring-2 focus:ring-blue-500">

Sizing

尺寸

html
<!-- Width -->
<div class="w-full md:w-1/2 lg:w-1/3">

<!-- Height -->
<div class="h-screen min-h-[500px]">

<!-- Max width -->
<div class="max-w-xl mx-auto">
html
<!-- 宽度 -->
<div class="w-full md:w-1/2 lg:w-1/3">

<!-- 高度 -->
<div class="h-screen min-h-[500px]">

<!-- 最大宽度 -->
<div class="max-w-xl mx-auto">

Arbitrary Values

任意值

html
<!-- Use any CSS value -->
<div class="top-[117px] left-[calc(50%-4rem)]">

<!-- With CSS variables -->
<div class="bg-[var(--my-color)]">

<!-- Complex values -->
<div class="grid-cols-[1fr_500px_2fr]">
html
<!-- 使用任意CSS值 -->
<div class="top-[117px] left-[calc(50%-4rem)]">

<!-- 结合CSS变量使用 -->
<div class="bg-[var(--my-color)]">

<!-- 复杂值 -->
<div class="grid-cols-[1fr_500px_2fr]">

Important Modifier

!important 修饰符

html
<!-- Force important -->
<div class="!mt-0">

<!-- With variants -->
<div class="hover:!bg-red-500">
html
<!-- 强制使用!important -->
<div class="!mt-0">

<!-- 结合变体使用 -->
<div class="hover:!bg-red-500">

Built-in Features (No Config Needed)

内置特性(无需配置)

Featurev3 Requirementv4
@import handlingpostcss-importBuilt-in
Vendor prefixingautoprefixerBuilt-in
CSS nestingpostcss-nestedBuilt-in
Content detectioncontent configAutomatic
特性v3 版本要求v4 版本
@import 处理依赖postcss-import内置支持
浏览器前缀依赖autoprefixer内置支持
CSS嵌套依赖postcss-nested内置支持
内容检测需配置content字段自动检测

Layers

图层

css
/* Use native CSS layers */
@layer base {
  h1 {
    @apply text-2xl font-bold;
  }
}

@layer components {
  .btn {
    @apply px-4 py-2 rounded font-medium;
  }
}

@layer utilities {
  /* Custom utilities via @utility directive instead */
}
css
/* 使用原生CSS图层 */
@layer base {
  h1 {
    @apply text-2xl font-bold;
  }
}

@layer components {
  .btn {
    @apply px-4 py-2 rounded font-medium;
  }
}

@layer utilities {
  /* 建议使用@utility指令定义自定义工具类 */
}

Best Practices (2025/2026)

2025/2026 最佳实践

1. Use OKLCH Colors for Design Systems

1. 为设计系统使用OKLCH颜色

OKLCH provides perceptually uniform colors, better gradients, and wide gamut support:
css
@theme {
  /* OKLCH format: oklch(lightness chroma hue) */
  /* Lightness: 0-1, Chroma: 0-0.4, Hue: 0-360 */

  /* Primary palette - adjust L for shades */
  --color-primary-50: oklch(0.97 0.02 250);
  --color-primary-100: oklch(0.93 0.04 250);
  --color-primary-500: oklch(0.55 0.2 250);  /* Base */
  --color-primary-600: oklch(0.48 0.2 250);
  --color-primary-900: oklch(0.27 0.12 250);

  /* Semantic colors */
  --color-success: oklch(0.6 0.15 145);
  --color-warning: oklch(0.75 0.15 65);
  --color-error: oklch(0.55 0.2 25);
}
OKLCH提供感知均匀的色彩、更自然的渐变和广色域支持:
css
@theme {
  /* OKLCH格式:oklch(亮度 色度 色相) */
  /* 亮度:0-1,色度:0-0.4,色相:0-360 */

  /* 主色调板 - 调整亮度生成不同深浅 */
  --color-primary-50: oklch(0.97 0.02 250);
  --color-primary-100: oklch(0.93 0.04 250);
  --color-primary-500: oklch(0.55 0.2 250);  /* 基础色 */
  --color-primary-600: oklch(0.48 0.2 250);
  --color-primary-900: oklch(0.27 0.12 250);

  /* 语义化颜色 */
  --color-success: oklch(0.6 0.15 145);
  --color-warning: oklch(0.75 0.15 65);
  --color-error: oklch(0.55 0.2 25);
}

2. Implement Fluid Typography

2. 实现流体排版

Smooth scaling without breakpoint jumps:
css
@theme {
  /* Fluid type scale using clamp() */
  /* clamp(min, preferred, max) */
  --text-fluid-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-fluid-sm: clamp(0.875rem, 0.8rem + 0.375vw, 1rem);
  --text-fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
  --text-fluid-lg: clamp(1.125rem, 1rem + 0.625vw, 1.25rem);
  --text-fluid-xl: clamp(1.25rem, 1rem + 1.25vw, 1.5rem);
  --text-fluid-2xl: clamp(1.5rem, 1.1rem + 2vw, 2rem);
  --text-fluid-3xl: clamp(1.875rem, 1.2rem + 3.375vw, 2.5rem);
  --text-fluid-4xl: clamp(2.25rem, 1rem + 6.25vw, 3.5rem);
}
Important: Always combine
vw
with
rem
for accessibility (respects zoom).
无需断点即可实现平滑的文字缩放:
css
@theme {
  /* 使用clamp()实现流体字体缩放 */
  /* clamp(最小值, 首选值, 最大值) */
  --text-fluid-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-fluid-sm: clamp(0.875rem, 0.8rem + 0.375vw, 1rem);
  --text-fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
  --text-fluid-lg: clamp(1.125rem, 1rem + 0.625vw, 1.25rem);
  --text-fluid-xl: clamp(1.25rem, 1rem + 1.25vw, 1.5rem);
  --text-fluid-2xl: clamp(1.5rem, 1.1rem + 2vw, 2rem);
  --text-fluid-3xl: clamp(1.875rem, 1.2rem + 3.375vw, 2.5rem);
  --text-fluid-4xl: clamp(2.25rem, 1rem + 6.25vw, 3.5rem);
}
重要提示:始终将
vw
rem
结合使用以保证可访问性(尊重用户缩放设置)。

3. Fluid Spacing System

3. 流体间距系统

css
@theme {
  /* Fluid spacing that scales with viewport */
  --spacing-fluid-sm: clamp(0.5rem, 0.4rem + 0.5vw, 1rem);
  --spacing-fluid-md: clamp(1rem, 0.75rem + 1.25vw, 2rem);
  --spacing-fluid-lg: clamp(2rem, 1rem + 3vw, 4rem);
  --spacing-fluid-section: clamp(4rem, 2rem + 8vw, 8rem);
}
css
@theme {
  /* 随视窗尺寸缩放的流体间距 */
  --spacing-fluid-sm: clamp(0.5rem, 0.4rem + 0.5vw, 1rem);
  --spacing-fluid-md: clamp(1rem, 0.75rem + 1.25vw, 2rem);
  --spacing-fluid-lg: clamp(2rem, 1rem + 3vw, 4rem);
  --spacing-fluid-section: clamp(4rem, 2rem + 8vw, 8rem);
}

4. Organize Theme by Category

4. 按分类组织主题

css
@theme {
  /* === Colors === */
  --color-primary: oklch(0.6 0.2 250);
  --color-secondary: oklch(0.7 0.15 180);
  --color-success: oklch(0.6 0.15 145);
  --color-error: oklch(0.55 0.2 25);

  /* === Typography === */
  --font-sans: 'Inter Variable', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;

  /* === Fluid Typography === */
  --text-fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
  --text-fluid-lg: clamp(1.25rem, 1rem + 1.25vw, 2rem);

  /* === Spacing === */
  --spacing-page: 2rem;
  --spacing-fluid-section: clamp(4rem, 2rem + 8vw, 8rem);

  /* === Border Radius === */
  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;

  /* === Shadows === */
  --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px oklch(0 0 0 / 0.07);

  /* === Easing === */
  --ease-fluid: cubic-bezier(0.3, 0, 0, 1);
  --ease-snappy: cubic-bezier(0.2, 0, 0, 1);
}
css
@theme {
  /* === 颜色 === */
  --color-primary: oklch(0.6 0.2 250);
  --color-secondary: oklch(0.7 0.15 180);
  --color-success: oklch(0.6 0.15 145);
  --color-error: oklch(0.55 0.2 25);

  /* === 排版 === */
  --font-sans: 'Inter Variable', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;

  /* === 流体排版 === */
  --text-fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
  --text-fluid-lg: clamp(1.25rem, 1rem + 1.25vw, 2rem);

  /* === 间距 === */
  --spacing-page: 2rem;
  --spacing-fluid-section: clamp(4rem, 2rem + 8vw, 8rem);

  /* === 圆角 === */
  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;

  /* === 阴影 === */
  --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px oklch(0 0 0 / 0.07);

  /* === 缓动函数 === */
  --ease-fluid: cubic-bezier(0.3, 0, 0, 1);
  --ease-snappy: cubic-bezier(0.2, 0, 0, 1);
}

5. Keep @utility Definitions Simple

5. 保持@utility 定义简洁

css
/* Good - single purpose utilities */
@utility truncate-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

@utility text-balance {
  text-wrap: balance;
}

@utility content-auto {
  content-visibility: auto;
  contain-intrinsic-size: auto 500px;
}

/* Safe area utilities for notched devices */
@utility safe-area-pb {
  padding-bottom: env(safe-area-inset-bottom);
}

@utility safe-area-pt {
  padding-top: env(safe-area-inset-top);
}

/* Avoid - too complex, use components instead */
@utility card-fancy {
  /* Too many properties - use @layer components */
}
css
/* 推荐 - 单一职责的工具类 */
@utility truncate-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

@utility text-balance {
  text-wrap: balance;
}

@utility content-auto {
  content-visibility: auto;
  contain-intrinsic-size: auto 500px;
}

/* 适配刘海屏设备的安全区域工具类 */
@utility safe-area-pb {
  padding-bottom: env(safe-area-inset-bottom);
}

@utility safe-area-pt {
  padding-top: env(safe-area-inset-top);
}

/* 不推荐 - 过于复杂,建议使用组件 */
@utility card-fancy {
  /* 过多属性 - 请使用@layer components */
}

6. Mobile-First Class Ordering

6. 移动端优先的类顺序

Always structure responsive classes progressively:
html
<!-- Base (mobile) -> sm -> md -> lg -> xl -> 2xl -->
<div class="
  text-sm md:text-base lg:text-lg
  p-4 md:p-6 lg:p-8
  grid-cols-1 md:grid-cols-2 lg:grid-cols-3
">
  Content
</div>
始终按渐进式结构组织响应式类:
html
<!-- 基础(移动端)-> sm -> md -> lg -> xl -> 2xl -->
<div class="
  text-sm md:text-base lg:text-lg
  p-4 md:p-6 lg:p-8
  grid-cols-1 md:grid-cols-2 lg:grid-cols-3
">
  内容
</div>

7. Accessible Interactive Elements

7. 可访问的交互元素

html
<!-- Touch-friendly button (44px minimum - WCAG 2.2) -->
<button class="
  min-h-11 min-w-11 px-4 py-2.5
  bg-primary-600 hover:bg-primary-700 text-white
  rounded-lg font-medium
  focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2
  transition-colors motion-reduce:transition-none
">
  Button Text
</button>
html
<!-- 符合WCAG 2.2标准的触控友好按钮(最小44px) -->
<button class="
  min-h-11 min-w-11 px-4 py-2.5
  bg-primary-600 hover:bg-primary-700 text-white
  rounded-lg font-medium
  focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2
  transition-colors motion-reduce:transition-none
">
  按钮文字
</button>

Common Migration Issues

常见迁移问题

Border Color Default

边框颜色默认值

css
/* v3: border used gray-200 by default */
/* v4: border uses currentColor */

/* Fix: explicitly set color */
@theme {
  --default-border-color: var(--color-gray-200);
}
css
/* v3: border默认使用gray-200 */
/* v4: border默认使用currentColor */

/* 修复:显式设置颜色 */
@theme {
  --default-border-color: var(--color-gray-200);
}

Ring Default

焦点环默认值

css
/* v3: ring was 3px blue-500 */
/* v4: ring is 1px currentColor */

/* Fix: restore v3 behavior */
@theme {
  --default-ring-width: 3px;
  --default-ring-color: var(--color-blue-500);
}
css
/* v3: 焦点环默认是3px blue-500 */
/* v4: 焦点环默认是1px currentColor */

/* 修复:恢复v3的行为 */
@theme {
  --default-ring-width: 3px;
  --default-ring-color: var(--color-blue-500);
}

Button Cursor

按钮光标

css
/* v4: buttons use cursor: default */
/* Fix: add pointer globally if needed */
button {
  cursor: pointer;
}
css
/* v4: 按钮默认使用cursor: default */
/* 修复:全局设置为pointer(如果需要) */
button {
  cursor: pointer;
}