Controlling File Size

Strategies for keeping your generated CSS small and performant.


Using the default configuration, Tailwind CSS comes in at 58.1kb minified and gzipped.

Here are a few other popular frameworks for comparison:

FrameworkOriginal SizeMinifiedGzipBrotli
Tailwind477.6kb350.4kb58.8kb17.1kb
Bootstrap187.8kb152.1kb22.7kb16.7kb
Bulma205.6kb172.4kb23.0kb18.0kb
Foundation154.1kb119.2kb15.9kb12.9kb
Tachyons111.7kb71.8kb13.4kb7.5kb
Semantic UI809.4kb613.8kb100.6kb77.8kb
Materialize175.0kb138.5kb21.1kb17.1kb

By comparison Tailwind definitely seems on the heavy side (although if you use Brotli instead of gzip it’s still very reasonable), but there are a number of strategies you can use to reduce this file size dramatically.


Removing unused CSS

Mozilla’s Firefox Send is built with Tailwind, yet somehow their CSS is only 13.1kb minified, and only 4.7kb gzipped! How?

They’re using Purgecss, a tool for removing CSS that you’re not actually using in your project. Purgecss is particularly effective with Tailwind because Tailwind generates thousands of utility classes for you, most of which you probably won’t actually use.

For example, Tailwind generates margin utilities for every size in your spacing scale, for every side of an element you might want to apply margin to, at every breakpoint you are using in your project. This leads to hundreds of different combinations that are all important to have available, but not all likely to be needed.

When using Purgecss with Tailwind, it’s very hard to end up with more than 10kb of compressed CSS.

Setting up Purgecss

In the future we may incorporate Purgecss directly into Tailwind, but for now the best way to use it in your project is as a PostCSS plugin.

To get started with Purgecss, first install @fullhuman/postcss-purgecss:

  1. # Using npm
  2. npm install @fullhuman/postcss-purgecss
  3. # Using yarn
  4. yarn add @fullhuman/postcss-purgecss

Next, add it as the last plugin in your postcss.config.js file:

  1. // postcss.config.js
  2. const purgecss = require('@fullhuman/postcss-purgecss')({
  3. // Specify the paths to all of the template files in your project
  4. content: [
  5. './src/**/*.html',
  6. './src/**/*.vue',
  7. './src/**/*.jsx',
  8. // etc.
  9. ],
  10. // Include any special characters you're using in this regular expression
  11. defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
  12. })
  13. module.exports = {
  14. plugins: [
  15. require('tailwindcss'),
  16. require('autoprefixer'),
  17. ...process.env.NODE_ENV === 'production'
  18. ? [purgecss]
  19. : []
  20. ]
  21. }

Note that in this example, we’re only enabling Purgecss in production. We recommend configuring Purgecss this way because it can be slow to run, and during development it’s nice to have every class available so you don’t need to wait for a rebuild every time you change some HTML.

Writing purgeable HTML

Purgecss uses "extractors" to determine what strings in your templates are classes. In the example above, we use a custom extractor that will find all of the classes Tailwind generates by default:

  1. const purgecss = require('@fullhuman/postcss-purgecss')({
  2. // ...
  3. defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
  4. })

The way it works is intentionally very "dumb". It doesn’t try to parse your HTML and look for class attributes or dynamically execute your JavaScript — it simply looks for any strings in the entire file that match this regular expression:

  1. /[A-Za-z0-9-_:/]+/g

That means that it is important to avoid dynamically creating class strings in your templates with string concatenation, otherwise Purgecss won’t know to preserve those classes.

Controlling File Size - 图1Don’t use string concatenation to create class names

  1. <div :class="text-{{ error ? 'red' : 'green' }}-600"></div>

Controlling File Size - 图2Do dynamically select a complete class name

  1. <div :class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

As long as a class name appears in your template in its entirety, Purgecss will not remove it.

Customizing the regular expression

In the example above, we use a regular expression that matches all of the non-standard characters Tailwind uses by default, like : and /.

If you are using any other special characters in your class names, make sure to update the regular expression to include those as well.

For example, if you have customized Tailwind to create classes like w-50%, you’ll want to add % to the regular expression:

  1. - /[A-Za-z0-9-_:/]+/g
  2. + /[A-Za-z0-9-_:/%]+/g

Removing unused theme values

If you can’t use Purgecss for one reason or another, you can also reduce Tailwind’s footprint by removing unused values from your configuration file.

The default theme provides a very generous set of colors, breakpoints, sizes, margins, etc. to make sure that when you pull Tailwind down to prototype something, create a CodePen demo, or just try out the workflow, the experience is as enjoyable and fluid as possible.

We don’t want you to have to go and write new CSS because we didn’t provide enough padding helpers out of the box, or because you wanted to use an orange color scheme for your demo and we only gave you blue.

This comes with a trade-off though: the default build is significantly heavier than it would be on a project with a purpose-built configuration file.

Here are a few strategies you can use to keep your generated CSS small and performant.

Limiting your color palette

The default theme includes a whopping 93 colors used for backgrounds, borders, and text, all of which also have hover: and focus variants, as well as responsive variants at the five default screen sizes.

This means that by default, there are 4185 classes generated from this color palette out of 8271 classes total in the entire default build.

Very few projects actually need this many colors, and removing colors you don’t need can have a huge impact on the overall file size.

Here’s how using a smaller color palette affects the final size:

ColorsOriginalMinifiedGzipBrotli
93 (default)477.6kb350.4kb58.8kb17.1kb
50361.3kb260.3kb45.7kb13.9kb
25293.1kb207.2kb38.0kb12.2kb

Removing unused breakpoints

Since almost every Tailwind utility is copied for every screen size, using fewer screen sizes can have a huge impact on the overall file size as well.

Here’s how defining fewer screens affects the output:

BreakpointsOriginalMinifiedGzipBrotli
4 (default)477.6kb350.4kb58.8kb17.1kb
3380.9kb279.7kb47.4kb16.3kb
2284.2kb209.0kb36.0kb15.0kb
1187.5kb138.3kb24.5kb13.7kb

If you only need 3 screen sizes and 35 colors, you’re down to 32.5kb after gzip (11.7kb after Brotli!) without changing anything else.

Disabling unused utilities and variants

If you don’t expect to need a certain utility plugin in your project at all, you can disable it completely by setting it to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. corePlugins: {
  5. float: false
  6. }
  7. }

If you need a utility but don’t need the responsive versions, set its variants to an empty array to generate 80% fewer classes:

  1. module.exports = {
  2. // ...
  3. variants: {
  4. appearance: []
  5. }
  6. }

These are mostly small wins compared to limiting your color palette or using fewer breakpoints, but they can still add up.