提示:本网站现在为维护调试状态

Tailwind CSS v4 新特性:Zero-config + CSS-native 變數

Choyeon
Choyeon
2026年9月26日 1 分鐘閱讀 513 閱讀
836 字 2,936 字元 更新於 2026年11月8日
Tailwind CSS v4 新特性:Zero-config + CSS-native 變數
目錄

Tailwind CSS v4 新特性:Zero-config + CSS-native 变量

Tailwind CSS v4 是 2024 年底发布的架构级重写,最大变化是"回归 CSS 本身":以前 tailwind.config.js 的所有能力现在都能用原生 CSS 的 @theme 和 CSS 变量实现,配合 oxc 引擎比 v3 快 10-40 倍。

零配置与 @theme 设计令牌

彻底删除 tailwind.config.js。入口文件只需要一行 @import "tailwindcss";所有主题令牌(颜色、字体、阴影、间距、动画)在同个 CSS 文件用 @theme 块声明,和 CSS 自定义属性语法完全一致。Next.js/Vite 无需 postcss.config 即可生效,新手上手门槛降低 50%。

/** @type {import('tailwindcss').Config} 以前 tailwind.config.js 写法现在不要了 **/
/* ====== app.css v4 新写法:零配置 @theme 块 ====== */
@import "tailwindcss";

@theme {
  --color-brand-50: #effaf5;
  --color-brand-100: #d9f2e5;
  --color-brand-500: #11a97a;
  --color-brand-600: #0e8a63;
  --color-surface: #0b0d12;
  --color-muted: color-mix(in oklab, var(--color-surface), white 65%);

  --font-sans: "Inter", "PingFang SC", "Noto Sans JP", ui-sans-serif, system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;

  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-xl: 1.5rem;
  --radius-2xl: 2rem;

  --shadow-card: 0 1px 2px rgba(0,0,0,0.04), 0 12px 32px -16px rgba(0,0,0,0.25);
  --shadow-glow: 0 0 0 1px color-mix(in oklab, var(--color-brand-500), transparent 70%),
                 0 0 32px -8px color-mix(in oklab, var(--color-brand-500), transparent 40%);

  --spacing-18: 4.5rem;
  --spacing-88: 22rem;
  --spacing-128: 32rem;

  --animate-float: float 6s ease-in-out infinite;
  --animate-shimmer: shimmer 2s linear infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}
@keyframes shimmer {
  from { background-position: -200% 0; }
  to   { background-position: 200% 0; }
}

@layer utilities {
  .text-balance { text-wrap: balance; }
  .bg-grid {
    background-image: linear-gradient(currentColor 1px, transparent 1px),
                      linear-gradient(90deg, currentColor 1px, transparent 1px);
    background-size: 24px 24px;
  }
  .mask-fade-b {
    mask-image: linear-gradient(to bottom, black 60%, transparent);
  }
}

/* 媒体查询 & 容器查询 v4 原生支持 */
@container card (min-width: 420px) {
  .card-title { font-size: 1.5rem; }
}
@media (prefers-color-scheme: dark) {
  :root { color-scheme: dark; }
}

/* ====== 业务组件中使用 ====== */
.card {
  @apply bg-white dark:bg-zinc-900/60 backdrop-blur-md rounded-2xl shadow-card p-8 transition-all duration-300;
}
.card:hover {
  @apply shadow-glow -translate-y-1;
}

/* ====== nextjs app router 入口 ====== */
// app/globals.css 首行导入即可,不需要 tailwind.config.js
// tailwindcss v4 依赖:npm i tailwindcss @tailwindcss/next
// next.config.mjs:
import createMDX from "@next/mdx";
import tailwindcss from "@tailwindcss/next";

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: { optimizePackageImports: ["shiki", "@shikijs/rehype"] },
};

export default tailwindcss(createMDX()(nextConfig));

// React 组件里直接用新的工具类
export function PricingCard({ tier }: { tier: "pro" | "enterprise" }) {
  const highlight = tier === "enterprise" ? "ring-2 ring-brand-500/40" : "";
  return (
    <article className={`card ${highlight} @lg:flex-row`}>
      <h3 className="card-title text-2xl font-semibold tracking-tight text-balance">
        {tier === "enterprise" ? (
          <span className="bg-gradient-to-r from-brand-500 to-brand-300 bg-clip-text text-transparent">
            Enterprise
          </span>
        ) : "Pro Plan"}
      </h3>
      <div className="mt-6 space-y-4 text-sm text-zinc-600 dark:text-zinc-300">
        <Feature text="SSO SAML / SCIM provisioning" />
        <Feature text="99.99% uptime SLA" />
        <Feature text="Dedicated CSM" />
      </div>
    </article>
  );
}

内置 oxc 引擎 & 容器查询 & @layer utilities

新编译器 oxc 用 Rust 写,10k 文件项目构建从 8s 降到 300ms。@media 媒体查询、@container 容器查询、@property、color-mix 都是一等公民支持,无需插件。@layer utilities 定义项目专属工具类,和官方工具类一起参与 JIT 排序与 purge。

维度 Tailwind CSS v3 Tailwind CSS v4
配置文件 tailwind.config.js(TypeScript) 零配置,CSS @theme 块
编译器 PostCSS + js oxc (Rust) 10-40x
CSS 原生特性 部分通过插件 @property / container / color-mix 全支持
设计令牌来源 config.theme + preset 合并 纯 CSS 变量,浏览器原生可覆盖
框架集成 postcss.config 必配 @tailwindcss/next / @tailwindcss/vite 零配置

最佳实践

沿用 v3 的 JIT 命名习惯但迁移配置到 @theme。旧项目用官方升级工具 npx @tailwindcss/upgrade 一键迁移。不要混用 @apply 超过 5 个类,复杂样式直接写原生 CSS 配合 theme() 函数取值。

X微博Telegram

本文作者

投票

你最希望 Rosetta 优先加入哪项功能?

社区投票(演示数据,可后台删除)

留言 (7)

0 / 3000
产品汪本汪
产品汪本汪pmdiary.io2026年9月23日

终于有人把 CSS Grid 的各种布局技巧讲明白了,我之前看 MDN 文档看半天没搞懂,看完这篇直接上手了!

奶茶味程序媛
奶茶味程序媛2026年9月23日

建议可以加一下关于无障碍访问(a11y)的部分,现在很多项目都不重视这块,但真的很重要。

AI炼丹学徒
AI炼丹学徒2026年9月22日

示例代码里那个 useEffect 的依赖数组好像少了个参数,严格模式下会触发两次,建议修正一下。

自由撰稿人阿秋
自由撰稿人阿秋qiuwrites.blog2026年9月22日

感谢分享,刚好需要这个!

机器学习萌新
机器学习萌新2026年9月22日

补充:用 CSS container query 实现响应式其实比媒体查询更灵活,组件级别的适配体验会好很多。

后端螺丝钉
后端螺丝钉backend-diary.com2026年9月22日

博主请问一下,Next.js 15 里这个写法在 App Router 下会有 hydration 不匹配的问题吗?我用的时候偶尔报错。

运维老司机
运维老司机ops-notes.cn2026年9月22日

作为一个后端转全栈的,看你的前端文章从来不会觉得难懂,讲得特别接地气,谢谢!

相關文章

CSS Container Queries 完全指南

CSS Container Queries 完全指南

徹底掌握現代響應式 CSS:Container Queries 讓元件基於父容器尺寸自適應(而非視口),配合 :has() 父選擇器實現依賴後代狀態的樣式變化,徹底改變元件化響應式設計範式。

1 分鐘閱讀
Vue 3 組合式 API 實戰

Vue 3 組合式 API 實戰

Vue 3 的組合式 API (Composition API) 徹底改變了 Vue 組件的組織方式。本文通過構建一個複雜的電商篩選器組件,演示 ref/reactive 的選擇、computed/watch 的合理使用、自定義組合函數 (Composable) 的抽離原則、生命週期鉤子的時機以及 provide/inject 進行跨層級狀態傳遞的正確姿勢。

1 分鐘閱讀
PWA 離線應用實戰:Service Worker + IndexedDB + Background Sync

PWA 離線應用實戰:Service Worker + IndexedDB + Background Sync

逐步構建可安裝、可離線運行、網絡恢復自動同步的 Progressive Web App:Service Worker 緩存策略(Cache First / Stale-While-Revalidate)、IndexedDB 封裝結構化本地數據、Background Sync 後台隊列在用戶離線操作後網絡恢復時自動提交變更,附完整 TS 代碼。

1 分鐘閱讀
React 18 Hooks 最佳實踐

React 18 Hooks 最佳實踐

React 18 引入了並發渲染、自動批處理、useTransition、useDeferredValue 等革命性特性。本文系統梳理 useState/useEffect 的正確用法、useMemo/useCallback 的優化邊界、自定義 Hook 的設計模式,以及並發模式下避免陳舊閉包、競態條件等陷阱的完整指南,幫助你寫出更健壯的 React 18 代碼。

1 分鐘閱讀
Astro 靜態站點深度:群島架構 & 內容集合

Astro 靜態站點深度:群島架構 & 內容集合

深入理解 Astro 核心創新:群島架構實現最小化 JS 注入,僅交互元件按需水合;內容集合提供類型安全的 Markdown/MDX 管理,構建零 JS 的高性能靜態站點。

1 分鐘閱讀
前端性能優化:Core Web Vitals 實戰

前端性能優化:Core Web Vitals 實戰

Google 搜索排名核心指標完全實戰:LCP 最大內容繪製(預連接/圖像優先級/CDN緩存)、CLS 累積佈局偏移(圖像尺寸/字體FOIT/動態佔位)、INP 交互響應(長任務拆分/防抖節流)全面落地。

1 分鐘閱讀