Want to slim down your Tailwind CSS for production? Here's how:
- Use PurgeCSS to remove unused styles
- Enable Just-in-Time (JIT) mode
- Minify CSS with cssnano
- Compress with Brotli or Gzip
- Trim unused colors and breakpoints
- Track file size changes
Aim for under 10kB, even for big projects. Here's what you're dealing with out of the box:
Compression | Size |
---|---|
Uncompressed | 3645.2kB |
Minified | 2936.0kB |
Gzip | 294.2kB |
Brotli | 72.8kB |
But don't panic. With the right setup, you can shrink your CSS dramatically. Netflix's Top 10 site? Just 6.5kB of CSS.
In this guide, we'll cover:
- Why file size matters
- Setting up your project
- Techniques to reduce file size
- Handling common issues
- Best practices for keeping files small
Let's dive in and make your Tailwind CSS lean and mean.
Related video from YouTube
Tailwind CSS File Size Basics
Tailwind CSS is great for developers, but it can create big files. Let's look at the numbers and see how this affects your site.
Development File Sizes
Here's what Tailwind CSS looks like out of the box:
Compression | Size |
---|---|
None | 3645.2kB |
Minified | 2936.0kB |
Gzip | 294.2kB |
Brotli | 72.8kB |
Why so big? Tailwind includes TONS of classes for every design possibility. Good for flexibility, not so good for file size.
Big Files, Slow Sites
Large CSS files can hurt your site:
- They take longer to load
- They use more data (bad for mobile users)
- They can lower your SEO rankings
Imagine your browser trying to handle a 2MB CSS file before showing your page. Not fun, right?
But don't worry. Tailwind has ways to shrink these files for your live site. With the right setup, even big projects can end up with tiny CSS files.
Take Netflix's Top 10 site. They use Tailwind but only send 6.5kB of CSS. That's tiny!
Want your CSS that small? Here's how:
- Use Tailwind's
purge
to cut unused styles - Minify your CSS
- Compress with Brotli
Do this, and you'll have a lean, mean Tailwind machine.
Getting Ready to Reduce File Size
Let's set up our project and tools before we start trimming Tailwind CSS. This prep work will help us track our progress.
Starting a Tailwind CSS Project
Here's a quick setup for a new Tailwind CSS project:
- Make a new project folder
- Run
npm init -y
- Install Tailwind:
npm install -D tailwindcss
- Create config:
npx tailwindcss init
Create input.css
:
@tailwind base;
@tailwind components;
@tailwind utilities;
Add to package.json
:
"scripts": {
"dev": "tailwindcss -i ./input.css -o ./css/style.css --watch",
"build": "tailwindcss -i ./input.css -o ./css/style.css"
}
Use npm run dev
for development and npm run build
for production.
Tools to Check CSS File Size
To track progress, we'll measure CSS file size. Here are some tools:
Tool | Use |
---|---|
ls -lh |
Check file size in terminal |
Browser DevTools | See file size and load time |
GTmetrix | Check website performance online |
Let's check our starting point. Run npm run build
and use ls -lh
:
$ ls -lh css/style.css
-rw-r--r-- 1 user group 3.6M Apr 15 10:00 css/style.css
That's our baseline: 3.6MB uncompressed. Our goal? Shrink it to under 10KB for production.
Next, we'll dive into size-cutting techniques. Get ready for some big improvements!
Ways to Make Files Smaller
Let's cut down your Tailwind CSS file size. Here's how:
Remove Unused CSS
PurgeCSS is your friend here. Set it up:
1. Install it:
yarn add @fullhuman/postcss-purgecss
2. Configure postcss.config.js
:
let environment = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
]
}
if (process.env.RAILS_ENV === "production") {
environment.plugins.push(
require('@fullhuman/postcss-purgecss')({
content: [
'./app/**/*.html.erb',
'./app/helpers/**/*.rb',
'./app/javascript/**/*.js',
'./app/javascript/**/*.vue',
'./app/javascript/**/*.jsx',
],
defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
})
)
}
module.exports = environment
This runs PurgeCSS only in production. Smart, right?
Use Fewer Colors
Tailwind's default palette is huge. Trim it:
module.exports = {
theme: {
colors: {
blue: '#1fb6ff',
purple: '#7e5bef',
pink: '#ff49db',
orange: '#ff7849',
green: '#13ce66',
yellow: '#ffc82c',
'gray-dark': '#273444',
gray: '#8492a6',
'gray-light': '#d3dce6',
},
},
}
Cut Down Screen Sizes
Fewer breakpoints = less CSS. Try this:
module.exports = {
theme: {
screens: {
sm: '640px',
lg: '1024px',
},
},
}
This cuts breakpoints from 5 to 2. That's a 51% file size reduction!
Turn Off Unused Features
Don't need it? Disable it:
module.exports = {
corePlugins: {
float: false,
objectFit: false,
objectPosition: false,
},
}
Compress CSS Files
After purging, minify. With Tailwind CLI:
npx tailwindcss -o build.css --minify
For PostCSS, add cssnano
:
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
...(process.env.NODE_ENV === 'production' ? { cssnano: {} } : {})
}
}
These tricks can slash your Tailwind CSS file size. Netflix's Top 10 feature? Just 6.5kB of CSS. That's tiny!
sbb-itb-55aadfa
More Ways to Shrink Files
Managing Custom Add-ons
When using Tailwind CSS, custom add-ons can bulk up your file size. Here's how to keep things slim:
- Make utility classes just for your project
- Don't copy existing Tailwind classes
- Use
@apply
to combine multiple utility classes
Instead of making a new class for every button type, create a base button class:
.btn {
@apply py-2 px-4 rounded;
}
Then build on it:
.btn-primary {
@apply btn bg-blue-500 text-white;
}
This cuts down on repetition and keeps your custom CSS lean.
Loading CSS in Parts
To speed up initial load times, try loading CSS in chunks:
- Critical CSS: Put must-have styles in your HTML's
<head>
- Non-critical CSS: Load the rest asynchronously
Here's how:
<head>
<style>
/* Critical CSS here */
</style>
<link rel="preload" href="full-styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="full-styles.css"></noscript>
</head>
This gets the important styles loaded first, making your site feel faster.
Keeping Files Small Over Time
As your project grows, you need to keep your CSS lean. Try these:
- Use PurgeCSS to ditch unused styles regularly
- Use Tailwind's Just-in-Time mode to make CSS on-demand
- Track CSS file size changes with each commit
Irina Nazarova, CEO at Evil Martians, says:
"Follow these rules and you'll use Tailwind for ages—happily and hassle-free."
Even big projects can have small CSS files. Netflix's Top 10 feature, built with Tailwind, only sends 6.5kB of CSS over the network.
Fixing Common Problems
Let's tackle two issues you might face when optimizing Tailwind CSS for production.
Fixing Removal Errors
PurgeCSS can be overzealous, especially with dynamic class names. Here's how to fix it:
1. Ditch string concatenation for class names. Instead of this:
<div :class="text-{{ error ? 'red' : 'green' }}-600"></div>
Do this:
<div :class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
2. Update your tailwind.config.js
:
module.exports = {
content: ['./src/**/*.html', './src/**/*.vue', './src/**/*.jsx'],
theme: {},
variants: {},
plugins: [],
}
3. For CSS-defined classes, keep them outside @layer
directives.
Unexpected File Size Increases
If your Tailwind CSS file suddenly balloons, try these:
-
Check your config: Use the
content
option intailwind.config.js
. -
Verify your build: Run Tailwind in production mode:
NODE_ENV=production npx tailwindcss-cli@latest build tailwind.css -o tailwind-styles.css
-
Scan your HTML: Look for new, unused classes.
-
Review custom plugins: They might be adding extra CSS.
-
Use Tailwind's size tracking:
module.exports = {
theme: {
// ...
},
plugins: [
require('@tailwindcss/typography'),
require('@tailwindcss/forms'),
],
experimental: {
optimizeUniversalDefaults: true,
},
}
This helps you keep tabs on size changes with each build.
Wrap-up
Want to slim down your Tailwind CSS for production? Here's how:
1. PurgeCSS: Zaps unused classes. Netflix's Top 10 site? Just 6.5kB of CSS.
2. JIT mode: On-demand CSS. Update tailwind.config.js
:
module.exports = {
content: [
'./resources/**/*.html',
'./resources/**/*.js',
],
jit: true,
};
3. Minify: Use cssnano in PostCSS:
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
require('cssnano')({ preset: 'default' }),
],
};
4. Compress: Brotli or Gzip. With Tailwind CLI:
npx tailwindcss -o build.css --minify
5. Trim extras: Cut unused colors and breakpoints.
6. Track size: In tailwind.config.js
:
module.exports = {
experimental: {
optimizeUniversalDefaults: true,
},
}
Aim for under 10kB, even for big projects. Always build in production:
NODE_ENV=production npx tailwindcss -o flowbite.css
Watch out for plugins and dynamic classes - they can bulk up your CSS.
FAQs
How to reduce Tailwind CSS file size?
Want to slim down your Tailwind CSS? Here's how:
- Use PurgeCSS to zap unused classes
- Minify with cssnano
- Compress using Brotli or Gzip
PurgeCSS can work wonders. One dev said:
"PurgeCSS cut my blog's CSS from ~900KB to ~9KB. Page load time in Chrome's Network tab? Halved!"
What is the build size of Tailwind CSS?
Tailwind CSS sizes vary:
Compression | Size |
---|---|
Uncompressed | 2413.4kB |
Minified | 1967.4kB |
Gzip | 190.2kB |
Brotli | 46.2kB |
How big is Tailwind minified?
Minified Tailwind CSS? 1967.4kB. But wait, there's more:
- Gzip squeezes it to 190.2kB
- Brotli shrinks it to a tiny 46.2kB
How large is tailwind in CSS?
Tailwind's default CSS file is hefty:
- Uncompressed: 2413.4kB
- Minified: 1967.4kB
Don't sweat it, though. With smart optimization, you can slash this size. Many devs end up with production builds under 10kB. Now that's lean!