tailwind-csstailwind-css-4

TailwindCSS v4 generates bloated CSS with styles not used in the project


In my index.css I have these annotations:

@import "tailwindcss" prefix(tw) source(none);
@source "../content_script";

Generated CSS file has styles with names that are not under content_script and not anywhere in my project

@layer base {
  *, :after, :before, ::backdrop {
    box-sizing: border-box;
    border: 0 solid;
    margin: 0;
    padding: 0;
  }

  ::file-selector-button {
    box-sizing: border-box;
    border: 0 solid;
    margin: 0;
    padding: 0;
  }
  
  /* ... */
}
/* ... */

How to get rid of unnecessary generated styles? Have them not be generated at all?


Solution

  • Those styles are from Tailwind's preflight:

    An opinionated set of base styles for Tailwind projects.

    You can remove them as per the documentation:

    Remove:

    @import "tailwindcss";
    

    And replace with:

    @layer theme, base, components, utilities;
    
    @import "tailwindcss/theme.css" layer(theme);
    @import "tailwindcss/utilities.css" layer(utilities);