技能标识:tailwind-v4
Tailwind CSS v4 with CSS-first configuration and design tokens. Use when setting up Tailwind v4, defining theme variables, using OKLCH colors, or configuring dark mode. Triggers on @theme, @tailwindcss/vite, oklch, CSS variables, --color-, tailwind v4.
Vite 插件设置:
ts
// vite.config.ts
import tailwindcss from @tailwindcss/vite;
import { defineConfig } from vite;
export default defineConfig({
plugins: [tailwindcss()],
});
CSS 入口文件:
css
/ src/index.css /
@import tailwindcss;
@theme 内联指令:
css
@theme inline {
--color-primary: oklch(60% 0.24 262);
--color-surface: oklch(98% 0.002 247);
}
| 特性 | v3 | v4 |
|---|---|---|
| 配置 | tailwind.config.js | CSS 中的 @theme |
| 构建工具 |
/ 生成::root { --color-brand: oklch(...); } /
/ 用法:text-brand → color: var(--color-brand) /
注意:你也可以显式使用 @theme default 来标记可被非默认 @theme 声明覆盖的主题值。
/ 用法:text-brand → color: oklch(60% 0.24 262) /
/ 没有 :root 变量,但工具类使用回退值 /
/ 用法:bg-internal → background-color: var(--color-internal, oklch(50% 0.1 180)) /
OKLCH 提供感知均匀的颜色,在不同色相间具有更好的一致性:
css
oklch(L% C H)
示例:
css
--color-sky-500: oklch(68.5% 0.169 237.323); / 亮蓝色 /
--color-red-600: oklch(57.7% 0.245 27.325); / 鲜艳红色 /
--color-zinc-900: oklch(21% 0.006 285.885); / 近黑色灰色 /
Tailwind v4 使用双破折号 CSS 变量命名约定:
css
@theme {
/ 颜色:--color-{名称}-{色阶} /
--color-primary-500: oklch(60% 0.24 262);
/ 间距:--spacing 乘数 /
--spacing: 0.25rem; / 间距比例的基础单位 /
/ 字体:--font-{字体系列} /
--font-display: Inter Variable, system-ui, sans-serif;
/ 断点:--breakpoint-{尺寸} /
--breakpoint-lg: 64rem;
/ 自定义动画:--animate-{名称} /
--animate-fade-in: fade-in 0.3s ease-out;
}
Tailwind v4 消除了配置文件:
json
{
devDependencies: {
@tailwindcss/vite: ^4.0.0,
@types/node: ^22.0.0,
tailwindcss: ^4.0.0,
vite: ^6.0.0
}
}
使用 @theme inline:
使用 @theme(默认):
使用 @theme reference:
映射到设计令牌的语义变量:
css
@theme {
/ 设计令牌(OKLCH 颜色) /
--color-blue-600: oklch(54.6% 0.245 262.881);
--color-slate-800: oklch(27.9% 0.041 260.031);
/ 语义映射 /
--color-primary: var(--color-blue-600);
--color-surface: var(--color-slate-800);
}
/ 用法:bg-primary, bg-surface /
css
@theme {
--font-display: Inter Variable, system-ui, sans-serif;
--font-mono: JetBrains Mono, ui-monospace, monospace;
--font-display--font-variation-settings: wght 400;
--font-display--font-feature-settings: cv02, cv03, cv04;
}
/ 用法:font-display, font-mono /
css
@theme inline {
--animate-beacon: beacon 2s ease-in-out infinite;
@keyframes beacon {
0%, 100% {
opacity: 1;
transform: scale(1);
}
50% {
opacity: 0.5;
transform: scale(1.05);
}
}
}
/ 用法:animate-beacon /
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 tailwind-v4-1775975402 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 tailwind-v4-1775975402 技能
skillhub install tailwind-v4-1775975402
文件大小: 12.46 KB | 发布时间: 2026-4-13 12:15