Tailwindcss

Configuration

Configure with sensible defaults, or extend it.

Default configuration

When you install the module, the default Tailwind configuration would be equivalent to this:

tailwind.config
{
  "theme": { "extend": {} },
  "content": {
    "files": [
      // all directories and extensions will correspond to your Nuxt config
      "{srcDir}/components/**/*.{vue,js,jsx,mjs,ts,tsx}",
      "{srcDir}/layouts/**/*.{vue,js,jsx,mjs,ts,tsx}",
      "{srcDir}/pages/**/*.{vue,js,jsx,mjs,ts,tsx}",
      "{srcDir}/plugins/**/*.{js,ts,mjs}",
      "{srcDir}/composables/**/*.{js,ts,mjs}",
      "{srcDir}/utils/**/*.{js,ts,mjs}",
      "{srcDir}/{A,a}pp.{vue,js,jsx,mjs,ts,tsx}",
      "{srcDir}/{E,e}rror.{vue,js,jsx,mjs,ts,tsx}",
      "{srcDir}/app.config.{js,ts,mjs}",
      "{srcDir}/app/spa-loading-template.html"
    ]
  },
  "plugins": []
}

You can learn more about the Tailwind configuration and content configuration in Tailwind docs.

Overwriting the configuration

You can extend the default configuration:

The provided configurations will be merged using defu's array function merger.

tailwind.config

If a tailwind.config file is present, it will be imported and used to overwrite the default configuration. You can pass custom configuration filepaths to the config option.

import colors from 'tailwindcss/colors'

export default {
  theme: {
    extend: {
      colors: {
        primary: colors.green
      }
    }
  }
}

Learn more about Tailwind configuration in the official documentation.

config option

You can also use your nuxt.config file to set your Tailwind config with the tailwindcss.config property:

nuxt.config
import colors from 'tailwindcss/colors'

export default defineNuxtConfig({
  // ...
  tailwindcss: {
    config: {
      theme: {
        extend: {
          colors: { primary: colors.green }
        }
      }
    }
  }
})
This config option allows only simple primitive values, and has less priority over the tailwind.config file(s).

Hooks

This is an advanced usage section and intended primarily for Nuxt modules authors.

tailwindcss:loadConfig

Passes any Tailwind configuration read by the module before merging all of them.

tailwindcss:config

Passes the resolved vanilla configuration read from all layers and paths with merging using defu.

tailwindcss:resolvedConfig

Passes the complete resolved configuration with all defaults using resolveConfig.

Usage

You can use a Nuxt hook to manipulate the Tailwind configuration using modules.

// ~/modules/nuxt-tailwind-mod/index.ts
import { defineNuxtModule, addTemplate } from '@nuxt/kit'

export default defineNuxtModule({
  setup (options, nuxt) {
    nuxt.hook('tailwindcss:config', function (tailwindConfig) {
      tailwindConfig.theme.colors.blue = '#fff'
    })

    nuxt.hook('tailwindcss:resolvedConfig', function (resolvedConfig) {
      console.log('This is the resulting config', JSON.stringify(resolvedConfig))
    })
  }
})

Ideally, you should provide an independent configuration template passed to the config module option. Refer to the module authoring example to see how it works.

Referencing in the application

It can often be useful to reference Tailwind configuration values at runtime, e.g. to access some of your theme values when dynamically applying inline styles in a component.

If you need resolved Tailwind config at runtime, you can enable the exposeConfig option:

nuxt.config
export default defineNuxtConfig({
  // ...
  tailwindcss: {
    exposeConfig: true
  }
})

Then, import where needed from #tailwind-config:

// Import fully resolved config
import tailwindConfig from '#tailwind-config'

// Import only part which is required to allow tree-shaking
import { theme } from '#tailwind-config'

Please be aware this adds ~19.5KB (~3.5KB) to the client bundle size. If you want to only import really specific parts of your tailwind config, you can enable imports for each property in the config:

nuxt.config
export default defineNuxtConfig({
  // ...
  tailwindcss: {
    exposeConfig: {
      level: 4,
    }
  }
})
// Import within properties for further tree-shaking
import screens from '#tailwind-config/theme/screens'  // default import
import { _neutral } from '#tailwind-config/theme/colors'  // named (with _ prefix)
import { _800 as slate800 } from '#tailwind-config/theme/colors/slate'  // alias
It is unlikely for level to ever be over 4 - the usual depth of a Tailwind config. A higher value is also likely to increase boot-time and disk space in dev. Refer to the Nuxt Virtual File System to see generated files.
Named exports for properties below root options are prefixed with _ (_colors, _900, _2xl) to ensure safe variable names. You can use default imports to provide any identifier or rename named imports using as. Properties with unsafe variable names (spacing['1.5'], height['1/2'], keyframes.ping['75%, 100%']) do not get exported individually.

Tailwind CSS version

If you wish to use another version of Tailwind CSS (i.e., different from the one that would be installed with this module), you can simply do so using your package manager like so:

npm install -D tailwindcss@3.2.0
Be careful while downgrading Tailwind CSS as the module may also have changes compatible to specific minor versions! In such cases, be sure to go through the release changelog.