Skip to main content
← ブログに戻る

Modern CSS Development with Tailwind CSS v4

4 min read著者: pycabbage
TutorialFrontend

Modern CSS Development with Tailwind CSS v4

Tailwind CSS v4 brings significant improvements to the utility-first CSS framework. Let's explore the new features and how they enhance modern web development.

What's New in Tailwind CSS v4

The latest version introduces groundbreaking changes:

  • New Engine: Completely rewritten for better performance
  • Lightning CSS: Faster builds and smaller output
  • Native cascade layers: Better CSS organization
  • Container queries: Responsive design based on container size
  • Improved configuration: More intuitive setup

Getting Started with v4

Install Tailwind CSS v4 in your project:

npm install tailwindcss@next @tailwindcss/postcss@next

Create a CSS file with Tailwind directives:

@import "tailwindcss";

Configure PostCSS:

// postcss.config.js
export default {
  plugins: {
    '@tailwindcss/postcss': {}
  }
}

New Configuration Approach

Tailwind v4 simplifies configuration with CSS-based customization:

@import "tailwindcss";

@theme {
  --color-primary: #3b82f6;
  --color-secondary: #8b5cf6;
  
  --font-sans: 'Inter', system-ui, sans-serif;
  --font-mono: 'Fira Code', monospace;
  
  --spacing-unit: 0.25rem;
}

Container Queries

One of the most exciting features is native container query support:

<div class="@container">
  <div class="@lg:grid-cols-3 @md:grid-cols-2 grid gap-4">
    <!-- Responsive based on container size, not viewport -->
  </div>
</div>

Use container query variants:

<article class="@container/article">
  <h2 class="@lg/article:text-3xl @md/article:text-2xl text-xl">
    Responsive heading based on article container
  </h2>
</article>

Advanced Styling Techniques

Dynamic Values

Use arbitrary values with improved syntax:

<div class="w-[calc(100%-2rem)] h-[clamp(200px,50vh,400px)]">
  <!-- Dynamic sizing with CSS functions -->
</div>

Logical Properties

Better support for internationalization:

<div class="pis-4 mie-2 border-bs-2">
  <!-- padding-inline-start, margin-inline-end, border-block-start -->
</div>

Advanced Selectors

Target complex states and relationships:

<ul class="[&>li:nth-child(odd)]:bg-gray-100">
  <li>Odd rows have gray background</li>
  <li>Even rows stay default</li>
</ul>

Component Patterns

Build reusable components with Tailwind v4:

@layer components {
  .btn {
    @apply inline-flex items-center justify-center;
    @apply px-4 py-2 rounded-lg font-medium;
    @apply transition-colors duration-200;
  }
  
  .btn-primary {
    @apply btn;
    @apply bg-primary text-white;
    @apply hover:bg-primary/90;
  }
}

Performance Optimizations

Tailwind v4 delivers significant performance improvements:

Faster Builds

  • Lightning CSS integration reduces build times by 10x
  • Smaller CSS output with better compression
  • Smarter purging of unused styles

Runtime Performance

<!-- Automatic CSS containment -->
<div class="contain-paint contain-layout">
  <!-- Better rendering performance -->
</div>

Dark Mode Enhancements

Improved dark mode with automatic color adjustments:

<div class="bg-white dark:bg-gray-900">
  <!-- Automatic color scheme detection -->
  <p class="text-gray-900 dark:text-gray-100">
    Content adapts to color scheme
  </p>
</div>

Custom dark mode colors:

@theme {
  --color-background: light-dark(#ffffff, #0a0a0a);
  --color-text: light-dark(#000000, #ffffff);
}

Responsive Design Evolution

Beyond breakpoints with modern responsive techniques:

<!-- Fluid typography -->
<h1 class="text-[clamp(2rem,5vw,4rem)]">
  Responsive without breakpoints
</h1>

<!-- Aspect ratio containers -->
<div class="aspect-video @container">
  <img class="@sm:object-cover object-contain" />
</div>

Animation and Transitions

Enhanced animation utilities:

<div class="animate-in slide-in-from-bottom duration-500">
  <!-- Enter animations -->
</div>

<button class="transition-[transform,box-shadow] hover:scale-105 hover:shadow-lg">
  Smooth interactions
</button>

Best Practices for v4

  1. Use CSS custom properties: Leverage the new theme system
  2. Embrace container queries: Design component-first
  3. Optimize for performance: Use contain utilities
  4. Think in layers: Organize styles with cascade layers
  5. Progressive enhancement: Use modern CSS features responsibly

Migration Tips

Upgrading from v3 to v4:

# Install the upgrade tool
npm install -D @tailwindcss/upgrade

# Run the migration
npx @tailwindcss/upgrade

Key changes to address:

  • Update configuration format
  • Replace deprecated utilities
  • Adjust custom components
  • Test container queries

Conclusion

Tailwind CSS v4 represents a major leap forward in utility-first CSS development. With its new engine, container queries, and improved developer experience, it's easier than ever to build modern, responsive web applications.

The future of CSS is here, and it's more powerful and flexible than ever. Start exploring Tailwind CSS v4 today and experience the next generation of utility-first styling!