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

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

Choyeon
Choyeon
Sep 26, 2026 1 min read 512 Views
679 words 3,516 chars Updated Nov 8, 2026
Tailwind CSS v4: Zero-config + CSS-native Variables
Table of contents

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

About the author

Poll

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

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

Comments (7)

0 / 3000
产品汪本汪
产品汪本汪pmdiary.ioSep 23, 2026

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

奶茶味程序媛
奶茶味程序媛Sep 23, 2026

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

AI炼丹学徒
AI炼丹学徒Sep 22, 2026

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

自由撰稿人阿秋
自由撰稿人阿秋qiuwrites.blogSep 22, 2026

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

机器学习萌新
机器学习萌新Sep 22, 2026

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

后端螺丝钉
后端螺丝钉backend-diary.comSep 22, 2026

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

运维老司机
运维老司机ops-notes.cnSep 22, 2026

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

Related posts

Complete Guide to CSS Container Queries

Complete Guide to CSS Container Queries

Modern responsive CSS pillars: Container Queries for component-level responsiveness based on parent size (not viewport), plus :has() parent selector for ancestor styling based on descendants.

1 min read
Vue 3 Composition API in Practice

Vue 3 Composition API in Practice

Vue 3's Composition API fundamentally changed how Vue components are organized. Through building a complex e-commerce filter component, this article demonstrates ref vs reactive choices, proper computed/watch usage, custom Composable extraction principles, lifecycle hook timing, and the correct approach to cross-layer state via provide/inject.

1 min read
PWA Offline App in Practice: Service Worker + IndexedDB + Background Sync

PWA Offline App in Practice: Service Worker + IndexedDB + Background Sync

Build installable, offline-first Progressive Web Apps: Service Worker caching strategies (Cache First, Stale-While-Revalidate), IndexedDB wrapper for structured local data, Background Sync queue that replays offline operations when network returns. Full TypeScript code included.

1 min read
React 18 Hooks Best Practices

React 18 Hooks Best Practices

React 18 introduces revolutionary features like concurrent rendering, automatic batching, useTransition, and useDeferredValue. This article systematically covers correct useState/useEffect usage, useMemo/useCallback optimization boundaries, custom Hook design patterns, and a complete guide to avoiding stale closures, race conditions, and other pitfalls in concurrent mode.

1 min read
Astro Deep Dive: Islands Architecture & Content Collections

Astro Deep Dive: Islands Architecture & Content Collections

Astro innovations: Islands Architecture for minimal JS with partial hydration, Content Collections for type-safe Markdown/MDX management, ultra-fast zero-JS static sites.

1 min read
Frontend Performance: Core Web Vitals in Practice

Frontend Performance: Core Web Vitals in Practice

Practical Google Core Web Vitals playbook: LCP via preconnects/image-priority/CDN, CLS elimination via image dimensions/font-loading/placeholders, INP via long-task splitting + debouncing.

1 min read