Adding New Utilities

Extending Tailwind with your own custom utility classes.


Although Tailwind provides a pretty comprehensive set of utility classes out of the box, you may 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 to help you add your own utilities in the most idiomatic way possible.


Using CSS

The easiest way to add your own utilities to Tailwind is to simply add them to your CSS.

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer utilities {
  5. .scroll-snap-none {
  6. scroll-snap-type: none;
  7. }
  8. .scroll-snap-x {
  9. scroll-snap-type: x;
  10. }
  11. .scroll-snap-y {
  12. scroll-snap-type: y;
  13. }
  14. }

By using the @layer directive, Tailwind will automatically move those styles to the same place as @tailwind utilities to avoid unintended specificity issues.

Using the @layer directive will also instruct Tailwind to consider those styles for purging when purging the utilities layer. Read our Controlling File Size documentation for more details.

Generating responsive variants

If you’d like to create responsive variants of your own utilities based on the breakpoints defined in your tailwind.config.js file, wrap your utilities in the @responsive directive:

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer utilities {
  5. @responsive {
  6. .scroll-snap-none {
  7. scroll-snap-type: none;
  8. }
  9. .scroll-snap-x {
  10. scroll-snap-type: x;
  11. }
  12. .scroll-snap-y {
  13. scroll-snap-type: y;
  14. }
  15. }
  16. }

Tailwind will automatically generate prefixed versions of each custom utility that you can use to conditionally apply those styles at different breakpoints:

  1. <!-- Use `scroll-snap-type: none` by default, then use `scroll-snap-type: x` on medium screens and up -->
  2. <div class="scroll-snap-none md:scroll-snap-x"></div>

Learn more about responsive utilities in the responsive design documentation.

Generating pseudo-class variants

If you’d like to create pseudo-class variants of your own utilities, wrap your utilities in the @variants directive:

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer utilities {
  5. @variants hover, focus {
  6. .filter-none {
  7. filter: none;
  8. }
  9. .filter-grayscale {
  10. filter: grayscale(100%);
  11. }
  12. }
  13. }

Tailwind will automatically generate prefixed versions of each custom utility that you can use to conditionally apply those styles at different states:

  1. <div class="filter-grayscale hover:filter-none"></div>

Pseudo-class variants are generated in the same order you list them in the @variants directive, so if you’d like one pseudo-class to take precedence over another, make sure you list that one last:

  1. /* Focus will take precedence over hover */
  2. @variants hover, focus {
  3. .filter-grayscale {
  4. filter: grayscale(100%);
  5. }
  6. /* ... */
  7. }
  8. /* Hover will take precedence over focus */
  9. @variants focus, hover {
  10. .filter-grayscale {
  11. filter: grayscale(100%);
  12. }
  13. /* ... */
  14. }

If you’d like to generate both responsive and pseudo-class variants of your custom utilities, wrap @variants with @responsive:

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer utilities {
  5. @responsive {
  6. @variants hover, focus {
  7. .filter-none {
  8. filter: none;
  9. }
  10. .filter-grayscale {
  11. filter: grayscale(100%);
  12. }
  13. }
  14. }
  15. }

Learn more about pseudo-class variants utilities in the pseudo-class variant documentation.


Using a plugin

In addition to adding new utility classes directly in your CSS files, you can also add utilities to Tailwind by writing your own plugin:

  1. // tailwind.config.js
  2. const plugin = require('tailwindcss/plugin')
  3. module.exports = {
  4. plugins: [
  5. plugin(function({ addUtilities }) {
  6. const newUtilities = {
  7. '.filter-none': {
  8. filter: 'none',
  9. },
  10. '.filter-grayscale': {
  11. filter: 'grayscale(100%)',
  12. },
  13. }
  14. addUtilities(newUtilities, ['responsive', 'hover'])
  15. })
  16. ]
  17. }

This can be a good choice if you want to publish your custom utilities as a library or make it easier to share them across multiple projects.

Learn more in the utility plugin documentation.


← Extracting Components   Functions & Directives →