Tailwind exposes a few custom CSS functions and directives that can be used in your actual CSS files.

@tailwind

Use the @tailwind directive to insert Tailwind’s preflight, utilities and screens styles into your CSS. Here’s a full example of how you might do this:

  1. /**
  2. * This injects Tailwind's base styles, which is a combination of
  3. * Normalize.css and some additional base styles.
  4. *
  5. * You can see the styles here:
  6. * https://github.com/tailwindcss/tailwindcss/blob/master/css/preflight.css
  7. */
  8. @tailwind preflight;
  9. /**
  10. * This injects any component classes registered by plugins.
  11. */
  12. @tailwind components;
  13. /**
  14. * This injects all of Tailwind's utility classes, generated based on your
  15. * config file. It also injects any utility classes registered by plugins.
  16. */
  17. @tailwind utilities;
  18. /**
  19. * Use this directive to control where Tailwind injects the responsive
  20. * variations of each utility.
  21. *
  22. * If omitted, Tailwind will append these classes to the very end of
  23. * your stylesheet by default.
  24. */
  25. @tailwind screens;

@apply

Use @apply to mix-in the contents of existing classes into your custom CSS.

This is extremely 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 text-white;
  6. }
  7. .btn-blue:hover {
  8. @apply bg-blue-dark;
  9. }

Rules can be listed on a single line or with multiple calls to @apply:

  1. .btn {
  2. @apply font-bold;
  3. @apply py-2;
  4. @apply px-4;
  5. @apply rounded;
  6. }

You can mix @apply declarations with normal CSS declarations too of course:

  1. .btn:hover {
  2. @apply bg-blue-dark;
  3. transform: translateY(-1px);
  4. }

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

  1. // Input
  2. .foo {
  3. @apply bar;
  4. }
  5. .bar {
  6. color: blue !important;
  7. }
  8. // Output
  9. .foo {
  10. color: blue;
  11. }
  12. .bar {
  13. color: blue !important;
  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 @apply will not work for mixing in hover, focus, or responsive variants of another utility. Instead, mix in the plain version of that utility into the appropriate pseudo-selector or a new media query:

  1. // Won't work:
  2. .btn {
  3. @apply block bg-red;
  4. @apply md:inline-block;
  5. @apply hover:bg-blue;
  6. }
  7. // Do this instead:
  8. .btn {
  9. @apply block bg-red;
  10. }
  11. .btn:hover {
  12. @apply bg-blue;
  13. }
  14. @screen md {
  15. .btn {
  16. @apply inline-block;
  17. }
  18. }

If you’ve configured a prefix for your utilities, you can optionally omit the prefix when using @apply if you prefer a terser syntax:

  1. // Both of these will work
  2. .btn {
  3. @apply tw-font-bold tw-py-2 tw-px-4 tw-rounded;
  4. }
  5. .btn {
  6. @apply font-bold py-2 px-4 rounded;
  7. }

@variants

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

  1. @variants focus, hover {
  2. .banana {
  3. color: yellow;
  4. }
  5. .chocolate {
  6. color: brown;
  7. }
  8. }

This will generate the following CSS:

  1. .banana {
  2. color: yellow;
  3. }
  4. .chocolate {
  5. color: brown;
  6. }
  7. .focus\:banana:focus {
  8. color: yellow;
  9. }
  10. .focus\:chocolate:focus {
  11. color: brown;
  12. }
  13. .hover\:banana:hover {
  14. color: yellow;
  15. }
  16. .hover\:chocolate:hover {
  17. color: brown;
  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 modules section of your config file:

  • responsive
  • hover
  • focus
  • active
  • group-hover
  • focus-within

…as well as any custom variants added through plugins.

@responsive

You can generate responsive versions 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. }

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: 576px) {
  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: 992px) {
  18. .lg\:bg-gradient-brand {
  19. background-image: linear-gradient(blue, green);
  20. }
  21. // ...
  22. }
  23. @media(min-width: 1200px) {
  24. .xl\:bg-gradient-brand {
  25. background-image: linear-gradient(blue, green);
  26. }
  27. // ...
  28. }

The responsive versions 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 576px 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: 576px) {
  2. /* ... */
  3. }

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

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

config()

While it’s recommended to use the @apply directive to compose custom CSS out of existing utility classes whenever possible, sometimes you need direct access to your Tailwind config values.

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

  1. // Source
  2. .error {
  3. font-size: config('textSizes.xs');
  4. color: config('colors.red-darker');
  5. }
  6. // Output
  7. .error {
  8. font-size: .75rem;
  9. color: #a61611;
  10. }