Although Tailwind provides a pretty comprehensive set of utility classes out of the box, you’re inevitably going to run into situations where you need to add a few of your own.

Deciding on the best way to extend a framework can be paralyzing, so here are some best practices and tools to help you add your own utilities “the Tailwind way.”

CSS Structure

A bare-bones Tailwind setup is a single CSS file that looks like this:

  1. @tailwind preflight;
  2. @tailwind components;
  3. @tailwind utilities;

In CSS, the order of your rule definitions is extremely important.

If two rules have the same specificity, the rule defined last is the rule that is applied.

For example, given the following CSS:

  1. .bg-red {
  2. background: #ff0000;
  3. }
  4. .bg-green {
  5. background-color: #00ff00;
  6. }

…and the following HTML:

  1. <div class="bg-green bg-red"></div>

…the div would be green, because .bg-green is defined after .bg-red in the CSS file.

For this reason, we recommend defining any custom utility classes at the end of your stylesheet, after you inject Tailwind’s utility classes:

  1. @tailwind preflight;
  2. @tailwind components;
  3. @tailwind utilities;
  4. .bg-cover-image {
  5. background-image: url('/path/to/image.jpg');
  6. }

This way your custom utilities can override Tailwind utilities if needed, although you should strive to avoid applying two utility classes to an element that target the same CSS property if at all possible.

If you’re using postcss-import or a preprocessor like Less, Sass, or Stylus, consider keeping your utilities in a separate file and importing them:

  1. @tailwind preflight;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @import "custom-utilities";

Responsive Variants

If you’d like to create responsive versions of your own utilities based on the breakpoints defined in your Tailwind config file, wrap your utilities in the @responsive { ... } directive:

  1. @tailwind preflight;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @responsive {
  5. .bg-cover-image {
  6. background-image: url('/path/to/image.jpg');
  7. }
  8. }

Tailwind will intelligently group the responsive versions into its existing media queries which are output at the very end of the stylesheet. This ensures that any responsive utilities will always take precedence over unprefixed utilities.

The above code would generate CSS that looks something like this:

  1. /* Preflight styles rendered here... */
  2. html { ... }
  3. /* ... */
  4. /* Tailwind utilities rendered here... */
  5. .bg-red { ... }
  6. /* ... */
  7. .bg-cover-image {
  8. background-image: url('/path/to/image.jpg');
  9. }
  10. @media (min-width: 576px) {
  11. /* Tailwind utilities rendered here... */
  12. .sm\:bg-red { ... }
  13. /* ... */
  14. .sm\:bg-cover-image {
  15. background-image: url('/path/to/image.jpg');
  16. }
  17. }
  18. @media (min-width: 768px) {
  19. /* Tailwind utilities rendered here... */
  20. .md\:bg-red { ... }
  21. /* ... */
  22. .md\:bg-cover-image {
  23. background-image: url('/path/to/image.jpg');
  24. }
  25. }
  26. @media (min-width: 992px) {
  27. /* Tailwind utilities rendered here... */
  28. .lg\:bg-red { ... }
  29. /* ... */
  30. .lg\:bg-cover-image {
  31. background-image: url('/path/to/image.jpg');
  32. }
  33. }
  34. @media (min-width: 1200px) {
  35. /* Tailwind utilities rendered here... */
  36. .xl\:bg-red { ... }
  37. /* ... */
  38. .xl\:bg-cover-image {
  39. background-image: url('/path/to/image.jpg');
  40. }
  41. }