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

Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数

Choyeon
Choyeon
2026年9月26日 1 分で読了 511 閲覧
679 字 3,516 文字 更新日 2026年11月8日
Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数
目次

Tailwind CSS v4: Zero-config + CSS-native Variables

Tailwind CSS v4 is the late-2024 architectural rewrite - its biggest shift is "going back to CSS itself". Everything previously in tailwind.config.js is now expressed via native CSS @theme blocks and CSS variables, and the new oxc engine delivers 10-40x speedup over v3.

Zero-config & @theme Design Tokens

Delete tailwind.config.js entirely. Entry CSS needs only one line: @import "tailwindcss". All theme tokens (colors, font stacks, shadows, spacing, animations) live inside the same CSS file in @theme block using real CSS custom-property syntax. Works with Next.js/Vite with no postcss.config - onboarding friction cut in half.

/** @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>
  );
}

Bundled oxc Engine, Container Queries, @layer utilities

New Rust-based oxc compiler cuts 10k-file project build from 8s to 300ms. @media queries, @container queries, @property, color-mix are first-class citizens - no plugins required. @layer utilities defines project-specific utility classes that join official utilities in JIT sorting and purge.

Dimension Tailwind CSS v3 Tailwind CSS v4
Config file tailwind.config.js (TS) Zero-config, CSS @theme block
Compiler PostCSS + JS oxc (Rust) 10-40x
Native CSS features Partial via plugins @property/container/color-mix full support
Design token source config.theme + preset merge Pure CSS vars, browser-overridable
Framework integration postcss.config required @tailwindcss/next / @tailwindcss/vite zero-config

Best Practices

Keep v3 JIT naming habits but migrate configs to @theme. For legacy projects run npx @tailwindcss/upgrade one-shot migration. Don't stack @apply beyond 5 utilities; for complex compositions write native CSS pulling tokens via theme() function.

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の2大レスポンシブ機能:親コンテナ寸法に応じるContainer Queries、子孫要素の状態で親をスタイルする:has()セレクタ。

1 分で読了
Vue 3 コンポジション API 実践

Vue 3 コンポジション API 実践

Vue 3 のコンポジション API は、Vue コンポーネントの設計手法を根本的に変えました。複雑な EC サイトのフィルターコンポーネントの構築を通じて、ref/reactive の使い分け、computed/watch の適切な使用方法、カスタム Composable の抽出原則、ライフサイクルフックのタイミング、provide/inject による階層を超えた状態受け渡しの正しい方法を解説します。

1 分で読了
PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

インストール可能・オフラインファーストなPWAを段階的構築:Service Worker キャッシュ戦略(Cache First / Stale-While-Revalidate)、構造化ローカルデータ用IndexedDBラッパー、ネットワーク復旧時にオフライン操作を自動リプレイするBackground Syncキュー。完全TypeScriptコード付き。

1 分で読了
React 18 Hooks ベストプラクティス

React 18 Hooks ベストプラクティス

React 18 は、コンカレントレンダリング、自動バッチング、useTransition、useDeferredValue などの革新的な機能を導入しました。この記事では、useState/useEffect の正しい使い方、useMemo/useCallback の最適化境界、カスタム Hook の設計パターン、コンカレントモードでの古いクロージャや競合状態を回避するための完全なガイドを提供します。

1 分で読了
Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション

Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション

Astroの革新:最小JSのアイランドアーキテクチャ、型安全なMarkdown/MDXのコンテンツコレクション、ゼロJSの高速静的サイト構築。

1 分で読了
フロントエンド性能:Core Web Vitals 実践

フロントエンド性能:Core Web Vitals 実践

Google検索順位のCore Web Vitals完全実践:LCP最適化(事前接続・画像優先度・CDN)、CLS除去(画像寸法・フォント・プレースホルダ)、INP改善(長タスク分割・デバウンス)。

1 分で読了