Getting ready for Tailwind v4.0

Written by Oscar Jite-Orimiono✏️ The word "tailwind" literally means the wind blowing in the same direction as a plane or boat's course of movement. It helps an object travel faster and reach its destination quicker, ensuring speed and efficiency. Tailwind CSS is a utility-first framework that lets you “rapidly build modern websites without leaving your HTML.” It’s not every developer’s cup of tea, but Tailwind CSS has gained significant popularity since its release in 2019. Today, you’ll likely find Tailwind CSS listed alongside established names like Bootstrap and Bulma when you search for “Top [insert number] CSS frameworks." This article will provide a preview and in-depth analysis of the next version, Tailwind v4.0. We’ll cover strategies for migrating existing projects and examples demonstrating the new features of Tailwind v4.0. We’ll also compare it with similar CSS frameworks, and explore the benefits and limitations of using this framework. Getting started with Tailwind v4.0 Tailwind v4.0 has been in development for several months, and the first public beta version was released in November 2024. For more detailed information, you can visit the prerelease documentation, but this guide will highlight some of the many new and exciting features developers can look forward to in Tailwind CSS v4.0 New performance engine The Tailwind team announced a new performance engine, Tailwind Oxide, in March 2024. Some benefits include a unified toolchain and simplified configuration to speed up the build process. CSS-first configuration With the current Tailwind version, the tailwind.config.js file allows you to override Tailwind’s default design tokens. It’s a customization hub where you can add custom utility classes and themes, disable plugins, and more. Its most important function is defining the content sources for your project so Tailwind can scan for relevant utility class names and produce the right output. Here’s the default code for the tailwind.config.js file when setting up a new project with Tailwind v3: /** @type {import('tailwindcss').Config} */ export default { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx}", ], theme: { extend: {}, }, plugins: [], } Removed directives After setting up the config file, the next step involved adding the Tailwind directives to the index.css file. These are the directives in Tailwind v3: @tailwind base; @tailwind components; @tailwind utilities; In Tailwind v4, you don’t need a tailwind.config.js file and @tailwind directives. You’ll only need to import "tailwindcss" into your main CSS file, and you’re good to go: @import "tailwindcss"; This reduces the number of steps when setting up a new project and the number of files. You can still use a JS config file, for example, if you already have an existing v3 project, by using the new @config directive to load it in your CSS file: @import "tailwindcss"; @config "../../tailwind.config.js"; However, not every feature, like corePlugins, important, and separator, is likely to be supported in the full v4.0 release. Some options, like safelist may return with changes in behavior. Source detection If there are files you don’t want to include, you can use the source() function when importing Tailwind to limit automatic detection: @import "tailwindcss" source("../src"); For additional sources that Tailwind doesn’t detect by default, like anything in your .gitignore file, you can add them using the @source directive: @import "tailwindcss"; @source "../node_modules/@my-company/ui-lib/src/components"; You can also disable source detection entirely: @import "tailwindcss" source(none); Disabling preflight You can import the specific individual layers you need for your project and disable Tailwind’s base styles: @layer theme, base, components, utilities; @import "tailwindcss/theme" layer(theme); @import "tailwindcss/utilities" layer(utilities); Customizing themes The new CSS-first approach makes adding custom styling to your Tailwind project easier. Any customization will be added directly to the main CSS file instead of a JavaScript configuration file. If, for instance, you want to configure new colors for a custom theme in Tailwind CSS v3, you’ll need to define the new utility classes in the theme section of the tailwind.config.js file. Here’s how you’d do it in the JavaScript configuration file: /** @type {import('tailwindcss').Config} */ export default { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx}", ], theme: { extend: { colors: { background:'#764abc', lilac: '#eabad2', light: '#eae3f5' } }, }, plugins: [], } Here’s how you would add the classes to your HTML file: LogRocket - Oscar Home About Contact

Jan 14, 2025 - 16:18
Getting ready for Tailwind v4.0

Written by Oscar Jite-Orimiono✏️

The word "tailwind" literally means the wind blowing in the same direction as a plane or boat's course of movement. It helps an object travel faster and reach its destination quicker, ensuring speed and efficiency.

Tailwind CSS is a utility-first framework that lets you “rapidly build modern websites without leaving your HTML.” It’s not every developer’s cup of tea, but Tailwind CSS has gained significant popularity since its release in 2019.

Today, you’ll likely find Tailwind CSS listed alongside established names like Bootstrap and Bulma when you search for “Top [insert number] CSS frameworks."

This article will provide a preview and in-depth analysis of the next version, Tailwind v4.0. We’ll cover strategies for migrating existing projects and examples demonstrating the new features of Tailwind v4.0. We’ll also compare it with similar CSS frameworks, and explore the benefits and limitations of using this framework.

Getting started with Tailwind v4.0

Tailwind v4.0 has been in development for several months, and the first public beta version was released in November 2024.

For more detailed information, you can visit the prerelease documentation, but this guide will highlight some of the many new and exciting features developers can look forward to in Tailwind CSS v4.0

New performance engine

The Tailwind team announced a new performance engine, Tailwind Oxide, in March 2024. Some benefits include a unified toolchain and simplified configuration to speed up the build process.

CSS-first configuration

With the current Tailwind version, the tailwind.config.js file allows you to override Tailwind’s default design tokens. It’s a customization hub where you can add custom utility classes and themes, disable plugins, and more.

Its most important function is defining the content sources for your project so Tailwind can scan for relevant utility class names and produce the right output.

Here’s the default code for the tailwind.config.js file when setting up a new project with Tailwind v3:

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

Removed directives

After setting up the config file, the next step involved adding the Tailwind directives to the index.css file.

These are the directives in Tailwind v3:

@tailwind base;
@tailwind components;
@tailwind utilities;

In Tailwind v4, you don’t need a tailwind.config.js file and @tailwind directives. You’ll only need to import "tailwindcss" into your main CSS file, and you’re good to go:

@import "tailwindcss";

This reduces the number of steps when setting up a new project and the number of files.

You can still use a JS config file, for example, if you already have an existing v3 project, by using the new @config directive to load it in your CSS file:

@import "tailwindcss";

@config "../../tailwind.config.js";

However, not every feature, like corePlugins, important, and separator, is likely to be supported in the full v4.0 release. Some options, like safelist may return with changes in behavior.

Source detection

If there are files you don’t want to include, you can use the source() function when importing Tailwind to limit automatic detection:

@import "tailwindcss" source("../src");

For additional sources that Tailwind doesn’t detect by default, like anything in your .gitignore file, you can add them using the @source directive:

@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib/src/components";

You can also disable source detection entirely:

@import "tailwindcss" source(none);

Disabling preflight

You can import the specific individual layers you need for your project and disable Tailwind’s base styles:

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

Customizing themes

The new CSS-first approach makes adding custom styling to your Tailwind project easier. Any customization will be added directly to the main CSS file instead of a JavaScript configuration file.

If, for instance, you want to configure new colors for a custom theme in Tailwind CSS v3, you’ll need to define the new utility classes in the theme section of the tailwind.config.js file.

Here’s how you’d do it in the JavaScript configuration file:

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {
      colors: {
        background:'#764abc',
        lilac: '#eabad2',
        light: '#eae3f5'
      }
    },
  },
  plugins: [],
}

Here’s how you would add the classes to your HTML file:

 className="bg-background">
     className="flex justify-between py-4 px-8">
       href="/" className="text-light">LogRocket - Oscar

       className="text-lilac">
        
  • href="#">Home
  • href="#">About
  • href="#">Contact
  • In this example, the utility classes are bg-background, text-light, and text-lilac.

    In Tailwind CSS v4.0, you configure all your customizations in CSS with the new @theme directive:

    @import "tailwindcss";
    
    @theme {
      --color-background-100: #764abc;
      --color-lilac-100: #eabad2;
      --color-light-100: #eae3f5;
    }
    

    The utility classes are then added to the HTML. You can choose to have different shades of the same color like the default Tailwind colors:

     className="bg-background-100">
         className="flex justify-between py-4 px-8">
           href="/" className="text-light-100">LogRocket - Oscar
    
           className="text-lilac-100">
            
  • href="#">Home
  • href="#">About
  • href="#">Contact
  • If you’re testing it out with VS Code, the @import directive may be highlighted as an error but don’t worry, it’ll work just fine.

    Note that the examples above were created with Tailwind CSS and React, hence why we have className in the HTML and not class. The utilities remain the same no matter the framework you’re working with.

    Theme variables

    From the previous example, you can see that CSS variables drive all theme styling in Tailwind v4.0:

    @theme {
      --font-display: "Poppins", "sans-serif";
    
      --ease-fluid: cubic-bezier(0.3,0,0,1);
    
      --color-background-100: #764abc;
    }
    

    In v4.0, you can override a specific theme namespace — that is, the default utilities for colors, fonts, text, and more, or the entire Tailwind theme and configure your own. You can easily configure custom styling for essentially every Tailwind utility in the main CSS file: List Of Tailwind Utilities To override the entire default theme, use --*: initial. If you wanted to override the default Tailwind font and define your own, you’d use --font-*: initial followed by your custom styling:

    @import "tailwindcss";
    
    @theme {
      --font-*: initial
      --font-display: "Poppins", "sans-serif";
    }
    

    In this case, font-display will be the only font-family utility available in your project.

    You can set default styling for a custom property using double-dashes. Here’s a page with the default Tailwind CSS font and text styling: Default Tailwind CSS Font And Text Styling Here’s the HTML markup for this page:

     className="bg-background h-screen">
         className="flex justify-between py-4 px-8">
           href="/" className="text-lg text-light font-bold">LogRocket - Oscar
    
           className="hidden md:flex flex- items-center align-middle gap-4 font-bold text-lilac">
            
  • href="#" className="py-2 px-4 rounded-md">Home
  • href="#" className="">About
  • href="#" className="">Contact
  • className="container px-32 py-32"> className="flex">
    className="text-5xl text-lilac font-bold">Tailwind CSS /> className="text-3xl text-light font-semibold"> Build websites with utility classes from the comfort of your HTML /> className="text-2xl text-light"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Fu gi at veniet atque unde laudantium. Ipsa nam quisquam quod non fficiis porro? Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eos iure nemo a hic sunt incidunt?