DEV Community

Maulik Paghdal for Script Binary

Posted on • Originally published at scriptbinary.com

Quick Guide: Reducing Tailwind CSS Build Size

If you've ever shipped a Tailwind project and noticed your CSS file is surprisingly large, you're not alone. By default, Tailwind can generate a hefty stylesheet, but with proper configuration, you can dramatically reduce it often to under 10KB.

The Main Problem

Tailwind generates thousands of utility classes. Without proper setup, all of them end up in your production build, even the ones you never use. The solution? Content purging and smart configuration.

Essential Steps to Reduce File Size

1. Configure Content Purging Correctly

This is the most critical step. Tell Tailwind where your template files are:

module.exports = {
  content: [
    './src/**/*.{html,js,jsx,ts,tsx,vue,svelte}',
    './public/index.html',
  ],
  theme: {
    extend: {},
  },
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Important: Avoid dynamic class names like bg-${color}-500. Tailwind can't detect these during the build process. Use complete class names instead.

2. Use Tailwind v3 (JIT Mode)

If you're still on v2, upgrade. Tailwind v3 uses Just-In-Time mode by default, which generates only the utilities you actually use. This alone can cut your CSS size in half.

3. Disable Unused Core Plugins

Not using floats or object-fit? Turn them off:

module.exports = {
  corePlugins: {
    float: false,
    objectFit: false,
    objectPosition: false,
  },
}
Enter fullscreen mode Exit fullscreen mode

4. Limit Your Color Palette

Tailwind's default color palette is massive. If you only use a few brand colors, define just those:

module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      white: '#ffffff',
      black: '#000000',
      blue: {
        500: '#3b82f6',
        600: '#2563eb',
      },
      gray: {
        500: '#6b7280',
        900: '#111827',
      },
    },
  },
}
Enter fullscreen mode Exit fullscreen mode

5. Minimize Use of @apply

While @apply is convenient, it can bloat your CSS. Prefer using Tailwind classes directly in your markup or create framework components:

// Better approach
function Button({ children }) {
  return (
    <button className="px-4 py-2 bg-blue-500 text-white rounded">
      {children}
    </button>
  );
}
Enter fullscreen mode Exit fullscreen mode

Don't Forget Production Optimization

Enable CSS minification in your build tool and make sure gzip/Brotli compression is active on your server. Most modern hosting platforms handle this automatically.

Check your build output:

npm run build
Enter fullscreen mode Exit fullscreen mode

You should see something like:

dist/assets/index.css  8.42 KB โ”‚ gzip: 2.15 KB
Enter fullscreen mode Exit fullscreen mode

Quick Wins Summary

โœ… Set up content paths correctly in tailwind.config.js

โœ… Upgrade to Tailwind v3 for JIT mode

โœ… Disable unused core plugins

โœ… Reduce your color palette

โœ… Avoid @apply where possible

โœ… Enable minification and compression

Want More Details?

This is just a quick overview. For a comprehensive guide with code examples, common mistakes, advanced techniques, and a complete optimization checklist, check out the full article on reducing Tailwind CSS build size.

With these optimizations in place, you'll ship leaner stylesheets and improve your site's performance significantly.


What's your biggest challenge with Tailwind CSS optimization? Drop a comment below!

Top comments (0)