Functions & Directives

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

@tailwind

Use the @tailwind directive to insert Tailwind’s base, components, utilities and screens 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 responsive
  18. * variations of each utility.
  19. *
  20. * If omitted, Tailwind will append these classes to the very end of
  21. * your stylesheet by default.
  22. */
  23. @tailwind screens;

@apply

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

This is useful when you find a common utility pattern in your HTML that you’d like to extract to a new component.

  1. .btn {
  2. @apply font-bold py-2 px-4 rounded;
  3. }
  4. .btn-blue {
  5. @apply bg-blue-500 hover:bg-blue-700 text-white;
  6. }

Note that classes are applied based on their location in your original CSS, not based on the order you list them after the @apply directive. This is to ensure that the behavior you get when extracting a list of classes with @apply matches how those classes behave when listed directly in your HTML.

  1. /* Input */
  2. .btn {
  3. @apply py-2 p-4;
  4. }
  5. /* Output */
  6. .btn {
  7. padding: 1rem;
  8. padding-top: 0.5rem;
  9. padding-bottom: 0.5rem;
  10. }

If you want fine-grained control over the order in which classes are applied, use multiple @apply statements:

  1. /* Input */
  2. .btn {
  3. @apply py-2;
  4. @apply p-4;
  5. }
  6. /* Output */
  7. .btn {
  8. padding-top: 0.5rem;
  9. padding-bottom: 0.5rem;
  10. padding: 1rem;
  11. }

You can also mix @apply declarations with normal CSS declarations:

  1. /* Input */
  2. .btn {
  3. transform: translateY(-1px);
  4. @apply bg-black;
  5. }
  6. /* Output */
  7. .btn {
  8. background-color: #000;
  9. transform: translateY(-1px);
  10. }

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. }

@layer

Use the @layer directive to tell Tailwind which “bucket” a set of custom styles belong to. Valid layers are a 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. @variants hover, focus {
  19. .filter-none {
  20. filter: none;
  21. }
  22. .filter-grayscale {
  23. filter: grayscale(100%);
  24. }
  25. }
  26. }

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 as much about authoring your CSS in a specific order to avoid specificity issues.

Wrapping any custom CSS in a @layer directive also tells Tailwind to consider those styles for purging when purging that layer. Read our documentation on optimizing for production for more details.


@variants

You can generate responsive, hover, focus, active, and other variants of your own utilities by wrapping their definitions in the @variants directive.

  1. @variants focus, hover {
  2. .rotate-0 {
  3. transform: rotate(0deg);
  4. }
  5. .rotate-90 {
  6. transform: rotate(90deg);
  7. }
  8. }

This will generate the following CSS:

  1. .rotate-0 {
  2. transform: rotate(0deg);
  3. }
  4. .rotate-90 {
  5. transform: rotate(90deg);
  6. }
  7. .focus\:rotate-0:focus {
  8. transform: rotate(0deg);
  9. }
  10. .focus\:rotate-90:focus {
  11. transform: rotate(90deg);
  12. }
  13. .hover\:rotate-0:hover {
  14. transform: rotate(0deg);
  15. }
  16. .hover\:rotate-90:hover {
  17. transform: rotate(90deg);
  18. }

It’s important to note that variants are generated in the order you specify them.

So if you want focus utilities to take priority over hover utilities for example, make sure focus comes after hover in the list:

  1. /* Input */
  2. @variants hover, focus {
  3. .banana {
  4. color: yellow;
  5. }
  6. }
  7. /* Output */
  8. .banana {
  9. color: yellow;
  10. }
  11. .hover\:banana:hover {
  12. color: yellow;
  13. }
  14. .focus\:banana:focus {
  15. color: yellow;
  16. }

The @variants at-rule supports all of the values that are supported in the variants section of your config file, as well as any custom variants added through plugins.


@responsive

You can generate responsive variants of your own classes by wrapping their definitions in the @responsive directive:

  1. @responsive {
  2. .bg-gradient-brand {
  3. background-image: linear-gradient(blue, green);
  4. }
  5. }

This is a shortcut for writing out @variants responsive { ... } which works as well.

Using the default breakpoints, this would generate these classes:

  1. .bg-gradient-brand {
  2. background-image: linear-gradient(blue, green);
  3. }
  4. /* ... */
  5. @media (min-width: 640px) {
  6. .sm\:bg-gradient-brand {
  7. background-image: linear-gradient(blue, green);
  8. }
  9. /* ... */
  10. }
  11. @media (min-width: 768px) {
  12. .md\:bg-gradient-brand {
  13. background-image: linear-gradient(blue, green);
  14. }
  15. /* ... */
  16. }
  17. @media (min-width: 1024px) {
  18. .lg\:bg-gradient-brand {
  19. background-image: linear-gradient(blue, green);
  20. }
  21. /* ... */
  22. }
  23. @media (min-width: 1280px) {
  24. .xl\:bg-gradient-brand {
  25. background-image: linear-gradient(blue, green);
  26. }
  27. /* ... */
  28. }

The responsive variants will be added to Tailwind’s existing media queries at the end of your stylesheet. This makes sure that classes with a responsive prefix always defeat non-responsive classes that are targeting the same CSS property.


@screen

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

For example, say you have a sm breakpoint at 640px and you need to write some custom CSS that references this breakpoint.

Instead of writing a raw media query that duplicates that value like this:

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

…you can use the @screen directive and reference the breakpoint by name:

  1. @screen sm {
  2. /* ... */
  3. }

screen()

The screen function accepts a screen name like md and generates the corresponding media feature expression:

  1. /* Input */
  2. @media screen(sm) {
  3. /* ... */
  4. }
  5. /* Output */
  6. @media (min-width: 640px) {
  7. /* ... */
  8. }

This can be useful when you using Tailwind with other CSS tooling that handles the @screen directive poorly. For example postcss-nesting doesn’t understand @screen but understands @media, so using @media alongside the screen() function behaves more correctly.


theme()

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

This can be a useful alternative to @apply when you want to reference a value from your theme configuration for only part of a declaration:

  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 - 图1Don’t use the dash syntax when accessing nested color values

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

Functions & Directives - 图2Use dot notation to access nested color values

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

←Adding New Utilities Configuration→

Edit this page on GitHub