Focus, active and other states

Hover isn't the only state supported by Tailwind CSS.

Thanks to Tailwind we can use directly in our HTML any state available in regular CSS.

Below are examples of several pseudo-class states supported in Tailwind CSS.

You don't have to try to memorize them now, we'll cover them in detail in the next lessons. For now, just be aware of their existence so you won't be surprised if you spot them in some TW Elements component.

  • Focus: Applied when an element has the focus. It is also enabled by default in Tailwind CSS. The modifier used is focus:
  • Active: Applied when an element is being activated by the user. The modifier used is active:
  • Visited: Applied once a user has visited a link. The modifier used is visited:
  • Disabled: Applied when an element is disabled. The modifier used is disabled:
  • Group-hover and group-focus: These are special states in Tailwind CSS that are used to apply styles to one element when another element is hovered or focused. They are typically used with the group utility class. The modifiers used are group-hover: and group-focus: respectively.
  • First-child and last-child: Applied to the first or last child of its parent. The modifiers used are first: and last:
  • Even and odd: Applied to even or odd children of its parent. The modifiers used are even: and odd:
  • Dark: Applied when dark mode is active. The modifier used is dark:
  • Placeholder: Applied to style placeholder text in form inputs. The modifier used is placeholder:
  • Checked: Applied when a checkbox or radio button is checked. The modifier used is checked:
  • Focus-within: Applied when an element itself has focus or contains an element that has focus. The modifier used is focus-within:

Using multiple states

There is no problem in Tailwind if you want to use multiple states in one element.

A good example is a simple button that must handle both hover, focus, active, dark, and sometimes other states.

And here is the button code:

I know, it looks disturbing. The amount of classes in HTML raises concerns about being too cluttered, but it's another thing you just have to get used to in Tailwind.

Anyway, note that we can apply any modifiers to our button to handle any states.

We just add them side by side:

Multiple states in the Navbar

Take another look at the Navbar in our project.

When you hover over a link on the left or an icon on the right, the color will change. However, when you move the cursor away, the color returns to its original. Thanks to the hover: modifier.

However, if you click on a specific link or icon and then move the cursor away, the changed color will remain the same until you unclick somewhere else on the page. Thanks to the focus: modifier.

So if you look closely at the sample link in our Navbar, you'll see that we use different modifiers here to handle different states:

You probably noticed there, in addition to modifiers such as focus: or active:, also the modifier dark:

It is used to support the famous dark mode, which we will deal with in the next lesson.

John Doe

About author

Michal Szymanski

Co Founder at TW Elements and MDBootstrap / Listed in Forbes „30 under 30" / Open-source enthusiast / Dancer, nerd & book lover.

Author of hundreds of articles on programming, business, marketing and productivity. In the past, an educator working with troubled youth in orphanages and correctional facilities.