Tailwind CSS 4.0: New Features & Upgrade Guide

Tailwind CSSCSS frameworkweb developmentfrontend tools
Lafu Code
Lafu Code
-- views

Tailwind CSS 4.0 is Here!

A few days ago, Tailwind CSS 4.0 Alpha was released. As a heavy Tailwind user, I immediately tried out the new version.

I have to say, this update is truly impressive. It's not just simple feature additions, but a complete overhaul from the underlying architecture to the user experience.

Today, let's dive deep into Tailwind CSS 4.0's new features and how to upgrade.

The Biggest Change: A Brand New Engine

From JavaScript to Rust

The biggest change in Tailwind CSS 4.0 is that the underlying engine has been rewritten from JavaScript to Rust. What does this change bring?

Performance Improvements:

  • Build speed improved by 10x or more
  • Significantly reduced memory usage
  • Support for larger scale projects

I tested on a medium-sized project, and build time dropped from 3 seconds to 0.3 seconds. This improvement is a qualitative leap.

Better Development Experience:

  • Faster hot reloading
  • More stable file watching
  • Clearer error messages

New Package Name: @tailwindcss/cli

Version 4.0 no longer uses the tailwindcss package, but instead uses the new @tailwindcss/cli:

# Install new version
npm install @tailwindcss/cli@next

# Or use pnpm
pnpm add @tailwindcss/cli@next

The benefits of this change:

  • Smaller package size
  • Fewer dependencies
  • Faster startup

Revolutionary Changes in Configuration

Goodbye tailwind.config.js

In Tailwind CSS 3.x, we needed a tailwind.config.js file to configure various options. Version 4.0 simplifies this process.

New Configuration Method:

/* Configure directly in CSS file */
@import "tailwindcss";

@config {
  theme: {
    extend: {
      colors: {
        primary: '#3b82f6',
        secondary: '#64748b'
      }
    }
  }
}

Advantages of this approach:

  • Configuration and styles in the same file
  • Support for CSS variables
  • Better type hints
  • Fewer files

Native CSS Variables Support

Version 4.0 natively supports CSS variables, making theme switching much simpler:

@import "tailwindcss";

@config {
  theme: {
    extend: {
      colors: {
        primary: 'var(--color-primary)',
        secondary: 'var(--color-secondary)'
      }
    }
  }
}

:root {
  --color-primary: #3b82f6;
  --color-secondary: #64748b;
}

[data-theme="dark"] {
  --color-primary: #60a5fa;
  --color-secondary: #94a3b8;
}

This way you can easily implement theme switching without additional JavaScript code.

Zero-Config Content Detection

Automatic File Discovery

Version 3.x required specifying files to scan in the config file:

// tailwind.config.js (3.x)
module.exports = {
  content: ["./src/**/*.{js,ts,jsx,tsx}", "./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
};

Version 4.0 automatically detects files in your project without manual configuration:

/* 4.0 version, no need to configure content */
@import "tailwindcss";

The system automatically scans common file types and directories, including:

  • .js, .jsx, .ts, .tsx
  • .vue, .svelte
  • .html, .php
  • Common directories like src/, pages/, components/

Smarter Class Name Detection

The new version's class name detection is more intelligent and can recognize:

Dynamic Class Names:

// These can all be correctly detected
const buttonClass = `bg-${color}-500`;
const textSize = `text-${size}`;
const spacing = `p-${padding}`;

Conditional Class Names:

const className = isActive ? "bg-blue-500" : "bg-gray-500";

Template Strings:

const styles = `
  ${isLarge ? "text-xl" : "text-base"}
  ${isPrimary ? "bg-blue-500" : "bg-gray-500"}
`;

New Selectors and Variants

More Powerful group and peer Variants

Version 4.0 enhances the functionality of group and peer variants:

Nested Groups:

<div class="group">
  <div class="group">
    <button class="group-hover:bg-blue-500 group-hover/parent:text-white">Button</button>
  </div>
</div>

Named Groups:

<div class="group/card">
  <div class="group/button">
    <button class="group-hover/card:shadow-lg group-hover/button:bg-blue-500">Button</button>
  </div>
</div>

New Container Query Support

Version 4.0 natively supports container queries:

<div class="@container">
  <div class="@sm:text-lg @md:text-xl @lg:text-2xl">Responsive text</div>
</div>

This is more flexible than media queries because it's based on container size rather than screen size.

New State Variants

Added more useful state variants:

<!-- Form validation states -->
<input class="valid:border-green-500 invalid:border-red-500" />

<!-- User preferences -->
<div class="motion-safe:animate-bounce motion-reduce:animate-none">Animated content</div>

<!-- Print styles -->
<div class="print:hidden screen:block">Only visible on screen</div>

Improved Development Tools

Better Error Messages

Version 4.0's error messages are more friendly and detailed:

❌ 3.x version error message:
Error: Cannot resolve class 'bg-blue-550'

✅ 4.0 version error message:
Unknown utility class: 'bg-blue-550'
Did you mean: 'bg-blue-500' or 'bg-blue-600'?
File: src/components/Button.jsx:15:23

Built-in Debug Tools

The new version includes built-in debug tools that can be viewed directly in the browser:

@import "tailwindcss";
@import "tailwindcss/debug";

This displays in development mode:

  • Current breakpoint information
  • Element's Tailwind class names
  • Generated CSS rules

How to Upgrade to 4.0

1. Install New Version

# Uninstall old version
npm uninstall tailwindcss

# Install new version
npm install @tailwindcss/cli@next

2. Update Configuration Files

Delete old config file:

rm tailwind.config.js

Create new CSS configuration:

/* styles/tailwind.css */
@import "tailwindcss";

@config {
  theme: {
    extend: {
      colors: {
        primary: "#3b82f6";
      }
    }
  }
}

3. Update Build Scripts

package.json:

{
  "scripts": {
    "build-css": "tailwindcss -i ./styles/tailwind.css -o ./dist/output.css",
    "watch-css": "tailwindcss -i ./styles/tailwind.css -o ./dist/output.css --watch"
  }
}

4. Update PostCSS Configuration (if using)

// postcss.config.js
module.exports = {
  plugins: {
    "@tailwindcss/postcss": {},
  },
};

5. Check Compatibility

Most 3.x class names work normally in 4.0, but there are some changes:

Removed Features:

  • @apply directive (recommend using component classes)
  • Some outdated variants

New Features:

  • Container queries
  • More state variants
  • CSS variables support

Real Project Migration Experience

I upgraded a medium-sized React project from 3.4 to 4.0, here are some insights:

Migration Time

  • Small projects (< 10 components): 1-2 hours
  • Medium projects (10-50 components): Half a day
  • Large projects (> 50 components): 1-2 days

Main Tasks

  1. Update dependencies: 5 minutes
  2. Migrate configuration: 30 minutes
  3. Test functionality: Most of the time
  4. Optimize new features: As needed

Issues Encountered

Issue 1: Custom CSS not working

/* Solution: Use @layer */
@import "tailwindcss";

@layer utilities {
  .custom-scroll {
    scrollbar-width: thin;
  }
}

Issue 2: Dynamic class names missing

// Solution: Use safelist
@config {
  safelist: [
    'bg-red-500',
    'bg-green-500',
    'bg-blue-500'
  ]
}

Performance Comparison

I tested 3.x and 4.0 performance on several projects:

Project Size 3.x Build Time 4.0 Build Time Improvement
Small 0.8s 0.1s 8x
Medium 3.2s 0.3s 10.7x
Large 12.5s 1.2s 10.4x

Performance improvements are very significant, especially in large projects.

Is It Worth Upgrading?

  • New projects: Use 4.0 directly
  • Slow build speeds: 4.0 can significantly improve performance
  • Need new features: Container queries, CSS variables, etc.
  • Team has time: Migration cost is not high

Hold Off for Now Scenarios

  • Project about to launch: Avoid introducing risks
  • Lots of custom configuration: High migration cost
  • Dependent on old version plugins: Wait for plugin updates

Final Thoughts

Tailwind CSS 4.0 is a milestone update. Although it's still an Alpha version, it has already shown tremendous potential.

Main Advantages:

  • Significant performance improvements
  • Simpler configuration
  • More powerful features
  • Better development experience

Things to Note:

  • Currently still Alpha version
  • Some plugins may not be compatible
  • Documentation is still being refined

If you're starting a new project, I recommend using 4.0 directly. For existing projects, you can wait until Beta or stable release to upgrade.

Overall, Tailwind CSS 4.0 makes me more excited about the future of frontend development. These performance and experience improvements will make our development work more efficient and enjoyable.

Are you ready to embrace Tailwind CSS 4.0?

Follow WeChat Official Account

WeChat Official Account QR Code

Scan to get:

  • • Latest tech articles
  • • Exclusive dev insights
  • • Useful tools & resources

💬 评论讨论

欢迎对《Tailwind CSS 4.0: New Features & Upgrade Guide》发表评论,分享你的想法和经验