Tailwind CSS UX/UI Design Course - Focus, active & other states
No pun here today :/
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 aregroup-hover:
andgroup-focus:
respectively.First-child and last-child: Applied to the first or last child of its parent. The modifiers used are
first:
andlast:
Even and odd: Applied to even or odd children of its parent. The modifiers used are
even:
andodd:
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:
<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:
<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.
Dashboard
Team
Projects
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:
<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.