TW Elements - TailwindCSS Focus, active & other states. Free UI/UX design course
Tags: tailkwind, design, html, tutorial
Published: 2024-06-24 16:00:00

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8b8n0cf3tw692jkt8ly.gif)

And here is the button code:
**HTML**

```
<a
  href="#"
  target="_blank"
  role="button"
  data-twe-ripple-init
  data-twe-ripple-color="light"
  class="mb-4 inline-block rounded bg-primary px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-white shadow-primary-3 transition duration-150 ease-in-out hover:bg-primary-accent-300 hover:shadow-primary-2 focus:bg-primary-accent-300 focus:shadow-primary-2 focus:outline-none focus:ring-0 active:bg-primary-600 active:shadow-primary-2 motion-reduce:transition-none dark:shadow-black/30 dark:hover:shadow-dark-strong dark:focus:shadow-dark-strong dark:active:shadow-dark-strong">
  Primary
</a>
```

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:

**HTML**

```
<div
  class="bg-blue-500 hover:bg-red-400 focus:bg-yellow-600 active:bg-green-700">
  Random element
</div>
```

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3m7z6y3km2u3yfzqtuig.png)

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:

**HTML**
```
<a
  class="text-neutral-500 hover:text-neutral-700 focus:text-neutral-700 disabled:text-black/30 dark:text-neutral-200 dark:hover:text-neutral-300 dark:focus:text-neutral-300 lg:px-2 [&.active]:text-black/90 dark:[&.active]:text-zinc-400"
  href="#"
  data-twe-nav-link-ref
  >Dashboard</a
>
```

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.