Core Concepts

Handling Hover, Focus, and Other States

Using utilities to style elements on hover, focus, and more.

Every utility class in Tailwind can be applied conditionally by adding a modifier to the beginning of the class name that describes the condition you want to target.

For example, to apply the bg-sky-700 class on hover, use the hover:bg-sky-700 class:

Hover, Focus, and Other States - 图1

Hover over this button to see the background color change

Hover, Focus, and Other States - 图2

  1. <button class="bg-sky-500 hover:bg-sky-700 ...">
  2. Save changes
  3. </button>

How does this compare to traditional CSS?

When writing CSS the traditional way, a single class name would do different things based on the current state.

Hover, Focus, and Other States - 图3

Traditionally the same class name applies different styles on hover

  1. .btn-primary {
  2. background-color: #0ea5e9;
  3. }
  4. .btn-primary:hover {
  5. background-color: #0369a1;
  6. }

In Tailwind, rather than adding the styles for a hover state to an existing class, you add another class to the element that only does something on hover.

Hover, Focus, and Other States - 图4

In Tailwind, separate classes are used for the default state and the hover state

  1. .bg-sky-500 {
  2. background-color: #0ea5e9;
  3. }
  4. .hover\:bg-sky-700:hover {
  5. background-color: #0369a1;
  6. }

Notice how hover:bg-sky-700 only defines styles for the :hover state? It does nothing by default, but as soon as you hover over an element with that class, the background color will change to sky-700.

This is what we mean when we say a utility class can be applied conditionally — by using modifiers you can control exactly how your design behaves in different states, without ever leaving your HTML.

Tailwind includes modifiers for just about everything you’ll ever need, including:

These modifiers can even be stacked to target more specific situations, for example changing the background color in dark mode, at the medium breakpoint, on hover:

  1. <button class="dark:md:hover:bg-fuchsia-600 ...">
  2. Save changes
  3. </button>

In this guide you’ll learn about every modifier available in the framework, how to use them with your own custom classes, and even how to create your own.


Pseudo-classes

Hover, focus, and active

Style elements on hover, focus, and active using the hover, focus, and active modifiers:

Hover, Focus, and Other States - 图5

Try interacting with this button to see the hover, focus, and active states

Hover, Focus, and Other States - 图6

  1. <button class="bg-violet-500 hover:bg-violet-600 active:bg-violet-700 focus:outline-none focus:ring focus:ring-violet-300 ...">
  2. Save changes
  3. </button>

Tailwind also includes modifiers for other interactive states like :visited, :focus-within, :focus-visible, and more.

See the pseudo-class reference for a complete list of available pseudo-class modifiers.

First, last, odd, and even

Style an element when it is the first-child or last-child using the first and last modifiers:

Hover, Focus, and Other States - 图7

  1. <ul role="list" class="p-6 divide-y divide-slate-200">
  2. {#each people as person}
  3. <!-- Remove top/bottom padding when first/last child -->
  4. <li class="flex py-4 first:pt-0 last:pb-0">
  5. <img class="h-10 w-10 rounded-full" src="{person.imageUrl}" alt="" />
  6. <div class="ml-3 overflow-hidden">
  7. <p class="text-sm font-medium text-slate-900">{person.name}</p>
  8. <p class="text-sm text-slate-500 truncate">{person.email}</p>
  9. </div>
  10. </li>
  11. {/each}
  12. </ul>

You can also style an element when it’s an odd or even child using the odd and even modifiers:

Hover, Focus, and Other States - 图8

  1. <table>
  2. <!-- ... -->
  3. <tbody>
  4. {#each people as person}
  5. <!-- Use a white background for odd rows, and slate-50 for even rows -->
  6. <tr class="odd:bg-white even:bg-slate-50">
  7. <td>{person.name}</td>
  8. <td>{person.title}</td>
  9. <td>{person.email}</td>
  10. </tr>
  11. {/each}
  12. </tbody>
  13. </table>

Tailwind also includes modifiers for other structural pseudo-classes like :only-child, :first-of-type, :empty, and more.

See the pseudo-class reference for a complete list of available pseudo-class modifiers.

Form states

Style form elements in different states using modifiers like required, invalid, and disabled:

Hover, Focus, and Other States - 图9

Try making the email address valid to see the styles change

Hover, Focus, and Other States - 图10

  1. <form>
  2. <label class="block">
  3. <span class="block text-sm font-medium text-slate-700">Username</span>
  4. <!-- Using form state modifers, the classes can be identical for every input -->
  5. <input type="text" value="tbone" disabled class="mt-1 block w-full px-3 py-2 bg-white border border-slate-300 rounded-md text-sm shadow-sm placeholder-slate-400
  6. focus:outline-none focus:border-sky-500 focus:ring-1 focus:ring-sky-500
  7. disabled:bg-slate-50 disabled:text-slate-500 disabled:border-slate-200 disabled:shadow-none
  8. invalid:border-pink-500 invalid:text-pink-600
  9. focus:invalid:border-pink-500 focus:invalid:ring-pink-500
  10. "/>
  11. </label>
  12. <!-- ... -->
  13. </form>

Using modifiers for this sort of thing can reduce the amount of conditional logic in your templates, letting you use the same set of classes regardless of what state an input is in and letting the browser apply the right styles for you.

Tailwind also includes modifiers for other form states like :read-only, :indeterminate, :checked, and more.

See the pseudo-class reference for a complete list of available pseudo-class modifiers.

Styling based on parent state (group-{modifier})

When you need to style an element based on the state of some parent element, mark the parent with the group class, and use group-* modifiers like group-hover to style the target element:

Hover, Focus, and Other States - 图11

Hover over the card to see both text elements change color

Hover, Focus, and Other States - 图12

  1. <a href="#" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
  2. <div class="flex items-center space-x-3">
  3. <svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
  4. <h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">New project</h3>
  5. </div>
  6. <p class="text-slate-500 group-hover:text-white text-sm">Create a new project from a variety of starting templates.</p>
  7. </a>

This pattern works with every pseudo-class modifier, for example group-focus, group-active, or even group-odd.

Styling based on sibling state (peer-{modifier})

When you need to style an element based on the state of a sibling element, mark the sibling with the peer class, and use peer-* modifiers like peer-invalid to style the target element:

Hover, Focus, and Other States - 图13

Try making the email address valid to see the warning disappear

Hover, Focus, and Other States - 图14

  1. <form>
  2. <label class="block">
  3. <span class="block text-sm font-medium text-slate-700">Email</span>
  4. <input type="email" class="peer ..."/>
  5. <p class="mt-2 invisible peer-invalid:visible text-pink-600 text-sm">
  6. Please provide a valid email address.
  7. </p>
  8. </label>
  9. </form>

This makes it possible to do all sorts of neat tricks, like floating labels for example without any JS.

This pattern works with every pseudo-class modifier, for example peer-focus, peer-required, and peer-disabled.

It’s important to note that the peer marker can only be used on previous siblings because of how the general sibling combinator works in CSS.

Hover, Focus, and Other States - 图15

Won’t work, only previous siblings can be marked as peers

  1. <label>
  2. <span class="peer-invalid:text-red-500 ...">Email</span>
  3. <input type="email" class="peer ..."/>
  4. </label>

Pseudo-elements

Before and after

Style the ::before and ::after pseudo-elements using the before and after modifiers:

Hover, Focus, and Other States - 图16

  1. <label class="block">
  2. <span class="after:content-['*'] after:ml-0.5 after:text-red-500 block text-sm font-medium text-slate-700">
  3. Email
  4. </span>
  5. <input type="email" name="email" class="mt-1 px-3 py-2 bg-white border shadow-sm border-slate-300 placeholder-slate-400 focus:outline-none focus:border-sky-500 focus:ring-sky-500 block w-full rounded-md sm:text-sm focus:ring-1" placeholder="you@example.com" />
  6. </label>

When using these modifiers, Tailwind will automatically add content: '' by default so you don’t have to specify it unless you want a different value:

Hover, Focus, and Other States - 图17

  1. <blockquote class="text-2xl font-semibold italic text-center text-slate-900">
  2. When you look
  3. <span class="before:block before:absolute before:-inset-1 before:-skew-y-3 before:bg-pink-500 relative inline-block">
  4. <span class="relative text-white">annoyed</span>
  5. </span>
  6. all the time, people think that you're busy.
  7. </blockquote>

It’s worth noting that you don’t really need ::before and ::after pseudo-elements for most things in Tailwind projects — it’s usually simpler to just use a real HTML element.

For example, here’s the same design from above but using a <span> instead of the ::before pseudo-element, which is a little easier to read and is actually less code:

  1. <blockquote class="text-2xl font-semibold italic text-center text-slate-900">
  2. When you look
  3. <span class="relative">
  4. <span class="block absolute -inset-1 -skew-y-3 bg-pink-500" aria-hidden="true"></span>
  5. <span class="relative text-white">annoyed</span>
  6. </span>
  7. all the time, people think that you're busy.
  8. </blockquote>

Save before and after for situations where it’s important that the content of the pseudo-element is not actually in the DOM and can’t be selected by the user.

Note that if you’ve disabled our preflight base styles, the content property will not be set to an empty string by default, and you will need to include content-[''] any time you use the before and after modifiers.

Hover, Focus, and Other States - 图18

If you’ve disabled preflight make sure to set the content manually

  1. <div class="before:content-[''] before:block ...">
  2. <!-- ... -->
  3. </div>

Placeholder text

Style the placeholder text of any input or textarea using the placeholder modifier:

Hover, Focus, and Other States - 图19

  1. <label class="relative block">
  2. <span class="sr-only">Search</span>
  3. <span class="absolute inset-y-0 left-0 flex items-center pl-2">
  4. <svg class="h-5 w-5 fill-slate-300" viewBox="0 0 20 20"><!-- ... --></svg>
  5. </span>
  6. <input class="placeholder:italic placeholder:text-slate-400 block bg-white w-full border border-slate-300 rounded-md py-2 pl-9 pr-3 shadow-sm focus:outline-none focus:border-sky-500 focus:ring-sky-500 focus:ring-1 sm:text-sm" placeholder="Search for anything..." type="text" name="search"/>
  7. </label>

File input buttons

Style the button in file inputs using the file modifier:

Hover, Focus, and Other States - 图20

  1. <form class="flex items-center space-x-6">
  2. <div class="shrink-0">
  3. <img class="h-16 w-16 object-cover rounded-full" src="https://images.unsplash.com/photo-1580489944761-15a19d654956?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1361&q=80" alt="Current profile photo" />
  4. </div>
  5. <label class="block">
  6. <span class="sr-only">Choose profile photo</span>
  7. <input type="file" class="block w-full text-sm text-slate-500
  8. file:mr-4 file:py-2 file:px-4
  9. file:rounded-full file:border-0
  10. file:text-sm file:font-semibold
  11. file:bg-violet-50 file:text-violet-700
  12. hover:file:bg-violet-100
  13. "/>
  14. </label>
  15. </form>

Note that Tailwind’s border reset is not applied to file input buttons. This means that to add a border to a file input button, you need to explicitly set the border-style using a class like file:border-solid alongside any border-width utility:

  1. <input type="file" class="file:border file:border-solid ..." />

List markers

Style the counters or bullets in lists using the marker modifier:

Hover, Focus, and Other States - 图21

  1. <ul role="list" class="marker:text-sky-400 list-disc pl-5 space-y-3 text-slate-500">
  2. <li>5 cups chopped Porcini mushrooms</li>
  3. <li>1/2 cup of olive oil</li>
  4. <li>3lb of celery</li>
  5. </ul>

We’ve designed the marker modifier to be inheritable, so although you can use it directly on an <li> element, you can also use it on a parent to avoid repeating yourself.

Highlighted text

Style the active text selection using the selection modifier:

Hover, Focus, and Other States - 图22

Try selecting some of this text with your mouse

Hover, Focus, and Other States - 图23

  1. <div class="selection:bg-fuchsia-300 selection:text-fuchsia-900">
  2. <p>
  3. So I started to walk into the water. I won't lie to you boys, I was
  4. terrified. But I pressed on, and as I made my way past the breakers
  5. a strange calm came over me. I don't know if it was divine intervention
  6. or the kinship of all living things but I tell you Jerry at that moment,
  7. I <em>was</em> a marine biologist.
  8. </p>
  9. </div>

We’ve designed the selection modifier to be inheritable, so you can add it anywhere in the tree and it will be applied to all descendant elements.

This makes it easy to set the selection color to match your brand across your entire site:

  1. <html>
  2. <head>
  3. <!-- ... -->
  4. </head>
  5. <body class="selection:bg-pink-300">
  6. <!-- ... -->
  7. </body>
  8. </html>

First-line and first-letter

Style the first line in a block of content using the first-line modifier, and the first letter using the first-letter modifier:

Hover, Focus, and Other States - 图24

  1. <p class="first-line:uppercase first-line:tracking-widest
  2. first-letter:text-7xl first-letter:font-bold first-letter:text-slate-900
  3. first-letter:mr-3 first-letter:float-left
  4. ">
  5. Well, let me tell you something, funny boy. Y'know that little stamp, the one
  6. that says "New York Public Library"? Well that may not mean anything to you,
  7. but that means a lot to me. One whole hell of a lot.
  8. </p>

Dialog backdrops

Style the backdrop of a native

element using the backdrop modifier:

  1. <dialog class="backdrop:bg-gray-50">
  2. <form method="dialog">
  3. <!-- ... -->
  4. </form>
  5. </dialog>

If you’re using native <dialog> elements in your project, you may also want to read about styling open/closed states using the open modifier.


Media queries

Responsive breakpoints

To style an element at a specific breakpoint, use responsive modifiers like md and lg.

For example, this will render a 3-column grid on mobile, a 4-column grid on medium-width screens, and a 6-column grid on large-width screens:

  1. <div class="grid grid-cols-3 md:grid-cols-4 lg:grid-cols-6">
  2. <!-- ... -->
  3. </div>

Check out the Responsive Design documentation for an in-depth look at how these features work.

Prefers color scheme

The prefers-color-scheme media query tells you whether the user prefers a light theme or dark theme, and is usually configured at the operating system level.

Use utilities with no modifier to target light mode, and use the dark modifier to provide overrides for dark mode:

Hover, Focus, and Other States - 图25

  1. <div class="bg-white dark:bg-slate-900 rounded-lg px-6 py-8 ring-1 ring-slate-900/5 shadow-xl">
  2. <div>
  3. <span class="inline-flex items-center justify-center p-2 bg-indigo-500 rounded-md shadow-lg">
  4. <svg class="h-6 w-6 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><!-- ... --></svg>
  5. </span>
  6. </div>
  7. <h3 class="text-slate-900 dark:text-white mt-5 text-base font-medium tracking-tight">Writes Upside-Down</h3>
  8. <p class="text-slate-500 dark:text-slate-400 mt-2 text-sm">
  9. The Zero Gravity Pen can be used to write in any orientation, including upside-down. It even works in outer space.
  10. </p>
  11. </div>

Check out the Dark Mode documentation for an in-depth look at how this feature works.

Prefers reduced motion

The prefers-reduced-motion media query tells you if the user has requested that you minimize non-essential motion.

Use the motion-reduce modifier to conditionally add styles when the user has requested reduced motion:

Hover, Focus, and Other States - 图26

Try emulating `prefers-reduced-motion: reduce` in your developer tools to hide the spinner

Hover, Focus, and Other States - 图27

  1. <button type="button" class="bg-indigo-500 ..." disabled>
  2. <svg class="motion-reduce:hidden animate-spin ..." viewBox="0 0 24 24"><!-- ... --></svg>
  3. Processing...
  4. </button>

Tailwind also includes a motion-safe modifier that only adds styles when the user has not requested reduced motion. This can be useful when using the motion-reduce helper would mean having to “undo” a lot of styles:

  1. <!-- Using `motion-reduce` can mean lots of "undoing" styles -->
  2. <button class="hover:-translate-y-0.5 transition motion-reduce:hover:translate-y-0 motion-reduce:transition-none ...">
  3. Save changes
  4. </button>
  5. <!-- Using `motion-safe` is less code in these situations -->
  6. <button class="motion-safe:hover:-translate-x-0.5 motion-safe:transition ...">
  7. Save changes
  8. </button>

Prefers contrast

The prefers-contrast media query tells you if the user has requested more or less contrast.

Use the contrast-more modifier to conditionally add styles when the user has requested more contrast:

Hover, Focus, and Other States - 图28

Try emulating `prefers-contrast: more` in your developer tools to see the changes

Hover, Focus, and Other States - 图29

  1. <form>
  2. <label class="block">
  3. <span class="block text-sm font-medium text-slate-700">Social Security Number</span>
  4. <input class="border-slate-200 placeholder-slate-400 contrast-more:border-slate-400 contrast-more:placeholder-slate-500"/>
  5. <p class="mt-2 opacity-10 contrast-more:opacity-100 text-slate-600 text-sm">
  6. We need this to steal your identity.
  7. </p>
  8. </label>
  9. </form>

Tailwind also includes a contrast-less modifier you can use to conditionally add styles when the user has requested less contrast.

Viewport orientation

Use the portrait and landscape modifiers to conditionally add styles when the viewport is in a specific orientation:

  1. <div>
  2. <div class="portrait:hidden">
  3. <!-- ... -->
  4. </div>
  5. <div class="landscape:hidden">
  6. <p>
  7. This experience is designed to be viewed in landscape. Please rotate your
  8. device to view the site.
  9. </p>
  10. </div>
  11. </div>

Print styles

Use the print modifier to conditionally add styles that only apply when the document is being printed:

  1. <div>
  2. <article class="print:hidden">
  3. <h1>My Secret Pizza Recipe</h1>
  4. <p>This recipe is a secret, and must not be shared with anyone</p>
  5. <!-- ... -->
  6. </article>
  7. <div class="hidden print:block">
  8. Are you seriously trying to print this? It's secret!
  9. </div>
  10. </div>

Attribute selectors

RTL support

Right-to-left support is experimental and the details of how it works may change. Share your feedback in the community to help us get it ready for prime time.

Use the rtl and ltr modifiers conditionally add styles in right-to-left and left-to-right modes respectively when building multi-directional layouts:

Hover, Focus, and Other States - 图30

  1. <div class="group flex items-center">
  2. <img class="shrink-0 h-12 w-12 rounded-full" src="..." alt="" />
  3. <div class="ltr:ml-3 rtl:mr-3">
  4. <p class="text-sm font-medium text-slate-700 group-hover:text-slate-900">...</p>
  5. <p class="text-sm font-medium text-slate-500 group-hover:text-slate-700">...</p>
  6. </div>
  7. </div>

Note that the ltr modifier will not take effect unless the dir attribute is explicitly set to ltr, so if you are building a multi-directional site make sure to always set a direction, not just in rtl mode.

Hover, Focus, and Other States - 图31

Always set the direction, even if left-to-right is your default

  1. <html dir="ltr">
  2. <!-- ... -->
  3. </html>

Remember, these modifiers are only useful if you are building a site that needs to support both left-to-right and right-to-left layouts. If you’re building a site that only needs to support a single direction, you don’t need these modifiers — just apply the styles that make sense for your content.

Open/closed state

Use the open modifier to conditionally add styles when a <details> or <dialog> element is in an open state:

Hover, Focus, and Other States - 图32

Try toggling the disclosure to see the styles change

Hover, Focus, and Other States - 图33

  1. <div class="max-w-lg mx-auto p-8">
  2. <details class="open:bg-white dark:open:bg-slate-900 open:ring-1 open:ring-black/5 dark:open:ring-white/10 open:shadow-lg p-6 rounded-lg" open>
  3. <summary class="text-sm leading-6 text-slate-900 dark:text-white font-semibold select-none">
  4. Why do they call it Ovaltine?
  5. </summary>
  6. <div class="mt-3 text-sm leading-6 text-slate-600 dark:text-slate-400">
  7. <p>The mug is round. The jar is round. They should call it Roundtine.</p>
  8. </div>
  9. </details>
  10. </div>

Custom modifiers

Using arbitrary variants

Just like arbitrary values let you use custom values with your utility classes, arbitrary variants let you write custom selector modifiers directly in your HTML.

Arbitrary variants are just format strings that represent the selector, wrapped in square brackets. For example, this arbitrary modifier selects an element only when it is the third child:

HTML

Generated CSS

  1. <ul role="list">
  2. {#each items as item}
  3. <li class="[&:nth-child(3)]:underline">{item}</li>
  4. {/each}
  5. </ul>

The format string is the same as what you’d use with the addVariant plugin API, with the & representing the selector being modified.

Arbitrary variants can be stacked with built-in modifiers or with each other, just like the rest of the modifiers in Tailwind:

HTML

Generated CSS

  1. <ul role="list">
  2. {#each items as item}
  3. <li class="lg:[&:nth-child(3)]:hover:underline">{item}</li>
  4. {/each}
  5. </ul>

If you need spaces in your selector, you can use an underscore. For example, this arbitrary modifier selects all p elements within the element where you’ve added the class:

HTML

Generated CSS

  1. <div class="[&_p]:mt-4">
  2. <p>Lorem ipsum...</p>
  3. <ul>
  4. <li>
  5. <p>Lorem ipsum...</p>
  6. </li>
  7. <!-- ... -->
  8. </ul>
  9. </div>

You can also use at-rules like @media or @supports in arbitrary variants:

HTML

Generated CSS

  1. <div class="flex [@supports(display:grid)]:grid">
  2. <!-- ... -->
  3. </div>

With at-rule custom modifiers the & placeholder isn’t necessary, just like when nesting with a preprocessor.

You can even combine at-rules and regular selector modifiers by including the selector modifier within curly braces after the at-rule:

HTML

Generated CSS

  1. <button type="button" class="[@media(any-hover:hover){&:hover}]:opacity-100">
  2. <!-- ... -->
  3. </button>

Creating a plugin

If you find yourself using the same arbitrary modifier multiple times in your project, it might be worth extracting it to a plugin using the addVariant API:

tailwind.config.js

  1. let plugin = require('tailwindcss/plugin')
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. plugin(function ({ addVariant }) {
  6. // Add a `third` variant, ie. `third:pb-0`
  7. addVariant('third', '&:nth-child(3)')
  8. })
  9. ]
  10. }

Learn more in the adding variant plugins documentation.


Advanced topics

Using with your own classes

All of Tailwind’s modifiers are available to use with your own custom classes as long as you’ve defined them in one of Tailwind’s layers or added them using a plugin:

main.css

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer utilities {
  5. .content-auto {
  6. content-visibility: auto;
  7. }
  8. }

HTML

  1. <div class="lg:content-auto">
  2. <!-- ... -->
  3. </div>

Ordering stacked modifiers

When stacking modifiers, they are applied from the inside-out, like nested function calls:

  1. // These modifiers:
  2. 'dark:group-hover:focus:opacity-100'
  3. // ...are applied like this:
  4. dark(groupHover(focus('opacity-100')))

For the most part this doesn’t actually matter, but there are a few situations where the order you use actually generates meaningfully different CSS.

For example, if you have darkMode configured to class, combining the dark and group-hover modifiers generates a different result depending on the order you use:

  1. /* dark:group-hover:opacity-100 */
  2. .dark .group:hover .dark\:group-hover\:opacity-100 {
  3. opacity: 1;
  4. }
  5. /* group-hover:dark:opacity-100 */
  6. .group:hover .dark .group-hover\:dark\:opacity-100 {
  7. opacity: 1;
  8. }

In the first example, the dark element needs to be a parent of the group element, but in the second example it’s reversed.

Another place this is important is when using modifiers like prose-headings that are included with the official typography plugin:

  1. /* prose-headings:hover:underline */
  2. .prose-headings\:hover\:underline:hover :is(:where(h1, h2, h3, h4, th)) {
  3. text-decoration: underline;
  4. }
  5. /* hover:prose-headings:underline */
  6. .hover\:prose-headings\:underline :is(:where(h1, h2, h3, h4, th)):hover {
  7. text-decoration: underline;
  8. }

In the first example, every single heading is underlined when you hover over the article itself, whereas in the second example each heading is only underlined when you hover over that heading.


Appendix

Quick reference

A quick reference table of every single modifier included in Tailwind by default.

ModifierCSS
hover&:hover
focus&:focus
focus-within&:focus-within
focus-visible&:focus-visible
active&:active
visited&:visited
target&:target
first&:first-child
last&:last-child
only&:only-child
odd&:nth-child(odd)
even&:nth-child(even)
first-of-type&:first-of-type
last-of-type&:last-of-type
only-of-type&:only-of-type
empty&:empty
disabled&:disabled
enabled&:enabled
checked&:checked
indeterminate&:indeterminate
default&:default
required&:required
valid&:valid
invalid&:invalid
in-range&:in-range
out-of-range&:out-of-range
placeholder-shown&:placeholder-shown
autofill&:autofill
read-only&:read-only
open&[open]
before&::before
after&::after
first-letter&::first-letter
first-line&::first-line
marker&::marker
selection&::selection
file&::file-selector-button
backdrop&::backdrop
placeholder&::placeholder
sm@media (min-width: 640px)
md@media (min-width: 768px)
lg@media (min-width: 1024px)
xl@media (min-width: 1280px)
2xl@media (min-width: 1536px)
dark@media (prefers-color-scheme: dark)
portrait@media (orientation: portrait)
landscape@media (orientation: landscape)
motion-safe@media (prefers-reduced-motion: no-preference)
motion-reduce@media (prefers-reduced-motion: reduce)
contrast-more@media (prefers-contrast: more)
contrast-less@media (prefers-contrast: less)
print@media print
rtl[dir=“rtl”] &
ltr[dir=“ltr”] &

Pseudo-class reference

This is a comprehensive list of examples for all the pseudo-class modifiers included in Tailwind to complement the pseudo-classes documentation at the beginning of this guide.

hover (:hover)

Style an element when the user hovers over it with the mouse cursor using the hover modifier:

  1. <div class="bg-black hover:bg-white ...">
  2. <!-- ... -->
  3. </div>

focus (:focus)

Style an element when it has focus the focus modifier:

  1. <input class="border-gray-300 focus:border-blue-400 ..." />

focus-within (:focus-within)

Style an element when it or one of its descendants has focus using the focus-within modifier:

  1. <div class="focus-within:shadow-lg ...">
  2. <input type="text" />
  3. </div>

focus-visible (:focus-visible)

Style an element when it has been focused using the keyboard using the focus-visible modifier:

  1. <button class="focus:outline-none focus-visible:ring ...">
  2. Submit
  3. </button>

active (:active)

Style an element when it is being pressed using the active modifier:

  1. <button class="bg-blue-500 active:bg-blue-600 ...">
  2. Submit
  3. </button>

visited (:visited)

Style a link when it has already been visited using the visited modifier:

  1. <a href="https://seinfeldquotes.com" class="text-blue-600 visited:text-purple-600 ...">
  2. Inspiration
  3. </a>

target (:target)

Style an element if its ID matches the current URL fragment using the target modifier:

  1. <div id="about" class="target:shadow-lg ...">
  2. <!-- ... -->
  3. </div>

first (:first-child)

Style an element if it’s the first child using the first modifier:

  1. <ul>
  2. {#each people as person}
  3. <li class="py-4 first:pt-0 ...">
  4. <!-- ... -->
  5. </li>
  6. {/each}
  7. </ul>

last (:last-child)

Style an element if it’s the last child using the last modifier:

  1. <ul>
  2. {#each people as person}
  3. <li class="py-4 last:pb-0 ...">
  4. <!-- ... -->
  5. </li>
  6. {/each}
  7. </ul>

only (:only-child)

Style an element if it’s the only child using the only modifier:

  1. <ul>
  2. {#each people as person}
  3. <li class="py-4 only:py-0 ...">
  4. <!-- ... -->
  5. </li>
  6. {/each}
  7. </ul>

odd (:nth-child(odd))

Style an element if it’s an oddly numbered child using the odd modifier:

  1. <table>
  2. {#each people as person}
  3. <tr class="bg-white odd:bg-gray-100 ...">
  4. <!-- ... -->
  5. </tr>
  6. {/each}
  7. </table>

even (:nth-child(even))

Style an element if it’s an evenly numbered child using the even modifier:

  1. <table>
  2. {#each people as person}
  3. <tr class="bg-white even:bg-gray-100 ...">
  4. <!-- ... -->
  5. </tr>
  6. {/each}
  7. </table>

first-of-type (:first-of-type)

Style an element if it’s the first child of its type using the first-of-type modifier:

  1. <nav>
  2. <img src="/logo.svg" alt="Vandelay Industries" />
  3. {#each links as link}
  4. <a href="#" class="ml-2 first-of-type:ml-6 ...">
  5. <!-- ... -->
  6. </a>
  7. {/each}
  8. </table>

last-of-type (:last-of-type)

Style an element if it’s the last child of its type using the last-of-type modifier:

  1. <nav>
  2. <img src="/logo.svg" alt="Vandelay Industries" />
  3. {#each links as link}
  4. <a href="#" class="mr-2 last-of-type:mr-6 ...">
  5. <!-- ... -->
  6. </a>
  7. {/each}
  8. <button>More</button>
  9. </table>

only-of-type (:only-of-type)

Style an element if it’s the only child of its type using the only-of-type modifier:

  1. <nav>
  2. <img src="/logo.svg" alt="Vandelay Industries" />
  3. {#each links as link}
  4. <a href="#" class="mx-2 only-of-type:mx-6 ...">
  5. <!-- ... -->
  6. </a>
  7. {/each}
  8. <button>More</button>
  9. </table>

empty (:empty)

Style an element if it has no content using the empty modifier:

  1. <ul>
  2. {#each people as person}
  3. <li class="empty:hidden ...">{person.hobby}</li>
  4. {/each}
  5. </ul>

disabled (:disabled)

Style an input when it’s disabled using the disabled modifier:

  1. <input class="disabled:opacity-75 ..." />

enabled (:enabled)

Style an input when it’s enabled using the enabled modifier, most helpful when you only want to apply another style when an element is not disabled:

  1. <input class="enabled:hover:border-gray-400 disabled:opacity-75 ..." />

checked (:checked)

Style a checkbox or radio button when it’s checked using the checked modifier:

  1. <input type="checkbox" class="appearance-none checked:bg-blue-500 ..." />

indeterminate (:indeterminate)

Style a checkbox or radio button in an indeterminate state using the indeterminate modifier:

  1. <input type="checkbox" class="appearance-none indeterminate:bg-gray-300 ..." />

default (:default)

Style an option, checkbox or radio button that was the default value when the page initially loaded using the default modifier:

  1. <input type="checkbox" class="default:ring-2 ..." />

required (:required)

Style an input when it’s required using the required modifier:

  1. <input class="required:border-red-500 ..." />

valid (:valid)

Style an input when it’s valid using the valid modifier:

  1. <input class="valid:border-green-500 ..." />

invalid (:invalid)

Style an input when it’s invalid using the invalid modifier:

  1. <input class="invalid:border-red-500 ..." />

in-range (:in-range)

Style an input when it’s value is within a specified range limit using the in-range modifier:

  1. <input min="1" max="5" class="in-range:border-green-500 ..." />

out-of-range (:out-of-range)

Style an input when it’s value is outside of a specified range limit using the out-of-range modifier:

  1. <input min="1" max="5" class="out-of-range:border-red-500 ..." />

placeholder-shown (:placeholder-shown)

Style an input when the placeholder is shown using the placeholder-shown modifier:

  1. <input class="placeholder-shown:border-gray-500 ..." placeholder="you@example.com" />

autofill (:autofill)

Style an input when it has been autofilled by the browser using the autofill modifier:

  1. <input class="autofill:bg-yellow-200 ..." />

read-only (:read-only)

Style an input when it is read-only using the read-only modifier:

  1. <input class="read-only:bg-gray-100 ..." />