Core Concepts

Functions & Directives

A reference for the custom functions and directives Tailwind exposes to your CSS.

Directives

Directives are custom Tailwind-specific at-rules you can use in your CSS that offer special functionality for Tailwind CSS projects.

@tailwind

Use the @tailwind directive to insert Tailwind’s base, components, utilities and variants styles into your CSS.

  1. /**
  2. * This injects Tailwind's base styles and any base styles registered by
  3. * plugins.
  4. */
  5. @tailwind base;
  6. /**
  7. * This injects Tailwind's component classes and any component classes
  8. * registered by plugins.
  9. */
  10. @tailwind components;
  11. /**
  12. * This injects Tailwind's utility classes and any utility classes registered
  13. * by plugins.
  14. */
  15. @tailwind utilities;
  16. /**
  17. * Use this directive to control where Tailwind injects the hover, focus,
  18. * responsive, dark mode, and other variants of each class.
  19. *
  20. * If omitted, Tailwind will append these classes to the very end of
  21. * your stylesheet by default.
  22. */
  23. @tailwind variants;

@layer

Use the @layer directive to tell Tailwind which “bucket” a set of custom styles belong to. Valid layers are base, components, and utilities.

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer base {
  5. h1 {
  6. @apply text-2xl;
  7. }
  8. h2 {
  9. @apply text-xl;
  10. }
  11. }
  12. @layer components {
  13. .btn-blue {
  14. @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
  15. }
  16. }
  17. @layer utilities {
  18. .filter-none {
  19. filter: none;
  20. }
  21. .filter-grayscale {
  22. filter: grayscale(100%);
  23. }
  24. }

Tailwind will automatically move any CSS within a @layer directive to the same place as the corresponding @tailwind rule, so you don’t have to worry about authoring your CSS in a specific order to avoid specificity issues.

Any custom CSS added to a layer will only be included in the final build if that CSS is actually used in your HTML, just like all of the classes built in to Tailwind by default.

Wrapping any custom CSS in a @layer directive also makes it possible to use modifiers with those rules, like hover: and focus: or responsive modifiers like md: and lg:.

@apply

Use @apply to inline any existing utility classes into your own custom CSS.

This is useful when you need to write custom CSS (like to override the styles in a third-party library) but still want to work with your design tokens and use the same syntax you’re used to using in your HTML.

  1. .select2-dropdown {
  2. @apply rounded-b-lg shadow-md;
  3. }
  4. .select2-search {
  5. @apply border border-gray-300 rounded;
  6. }
  7. .select2-results__group {
  8. @apply text-lg font-bold text-gray-900;
  9. }

Any rules inlined with @apply will have !important removed by default to avoid specificity issues:

  1. /* Input */
  2. .foo {
  3. color: blue !important;
  4. }
  5. .bar {
  6. @apply foo;
  7. }
  8. /* Output */
  9. .foo {
  10. color: blue !important;
  11. }
  12. .bar {
  13. color: blue;
  14. }

If you’d like to @apply an existing class and make it !important, simply add !important to the end of the declaration:

  1. /* Input */
  2. .btn {
  3. @apply font-bold py-2 px-4 rounded !important;
  4. }
  5. /* Output */
  6. .btn {
  7. font-weight: 700 !important;
  8. padding-top: .5rem !important;
  9. padding-bottom: .5rem !important;
  10. padding-right: 1rem !important;
  11. padding-left: 1rem !important;
  12. border-radius: .25rem !important;
  13. }

Note that if you’re using Sass/SCSS, you’ll need to use Sass’ interpolation feature to get this to work:

  1. .btn {
  2. @apply font-bold py-2 px-4 rounded #{!important};
  3. }

Using @apply with per-component CSS

Component frameworks like Vue and Svelte support adding per-component styles within a <style> block that lives in each component file.

If you try to @apply a custom class you’ve defined in your global CSS in one of these per-component <style> blocks, you’ll get an error about the class not existing:

main.css

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer components {
  5. .card {
  6. background-color: theme(colors.white);
  7. border-radius: theme(borderRadius.lg);
  8. padding: theme(spacing.6);
  9. box-shadow: theme(boxShadow.xl);
  10. }
  11. }

Card.svelte

  1. <div>
  2. <slot></slot>
  3. </div>
  4. <style>
  5. div {
  6. /* Won't work because this file and main.css are processed separately */
  7. @apply card;
  8. }
  9. </style>

This is because under-the-hood, frameworks like Vue and Svelte are processing every single <style> block independently, and running your PostCSS plugin chain against each one in isolation.

That means if you have 10 components that each have a <style> block, Tailwind is being run 10 separate times, and each run has zero knowledge about the other runs. Because of this, when you try to @apply card in Card.svelte it fails, because Tailwind has no idea that the card class exists since Svelte processed Card.svelte and main.css in total isolation from each other.

The solution to this problem is to define any custom styles you want to @apply in your components using the plugin system instead:

tailwind.config.js

  1. const plugin = require('tailwindcss/plugin')
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. plugin(function ({ addComponents, theme }) {
  6. addComponents({
  7. '.card': {
  8. backgroundColor: theme(colors.white),
  9. borderRadius: theme(borderRadius.lg),
  10. padding: theme(spacing.6),
  11. boxShadow: theme(boxShadow.xl),
  12. }
  13. })
  14. })
  15. ]
  16. }

This way any file processed by Tailwind that uses this config file will have access to those styles.

Honestly though the best solution is to just not do weird stuff like this at all. Use Tailwind’s utilities directly in your markup the way they are intended to be used, and don’t abuse the @apply feature to do things like this and you will have a much better experience.


Functions

Tailwind adds a few custom functions you can use in your CSS to access Tailwind-specific values. These functions are evaluated at build-time, and are replaced by static values in your final CSS.

theme()

Use the theme() function to access your Tailwind config values using dot notation.

  1. .content-area {
  2. height: calc(100vh - theme(spacing.12));
  3. }

If you need to access a value that contains a dot (like the 2.5 value in the spacing scale), you can use square bracket notation:

  1. .content-area {
  2. height: calc(100vh - theme(spacing[2.5]));
  3. }

Since Tailwind uses a nested object syntax to define its default color palette, make sure to use dot notation to access the nested colors.

Functions & Directives - 图1

Don’t use the dash syntax when accessing nested color values

  1. .btn-blue {
  2. background-color: theme(colors.blue-500);
  3. }

Functions & Directives - 图2

Use dot notation to access nested color values

  1. .btn-blue {
  2. background-color: theme(colors.blue.500);
  3. }

To adjust the opacity of a color retrieved with theme, use a slash followed by the opacity value you want to use:

  1. .btn-blue {
  2. background-color: theme(colors.blue.500 / 75%);
  3. }

screen()

The screen function allows you to create media queries that reference your breakpoints by name instead of duplicating their values in your own CSS.

  1. @media screen(sm) {
  2. /* ... */
  3. }

This will resolve to the underlying screen value at build-time, generating a regular media query that matches specified breakpoint:

  1. @media (min-width: 640px) {
  2. /* ... */
  3. }