- Published on
How to Use :not() in Tailwind CSS
- Authors
- Name
- Ripal & Zalak
How to Use :not() in Tailwind CSS
Tailwind CSS doesn’t provide a built-in :not()
variant, but you can still achieve the same effect using arbitrary variants and new features in Tailwind 4.
1. Using Arbitrary Variants (Tailwind v3+)
With Tailwind v3, you can apply :not()
using arbitrary variants:
<li class="border-gray-500 [&:not(:last-child)]:border-b">Item</li>
This applies border-b
to all <li>
elements except the last one.
not-*
Variant
2. Using Tailwind 4’s Tailwind v4 introduces native not-*
variants, making it even easier to exclude specific elements:
<ul>
<li class="not-last:border-b border-gray-500">Item</li>
</ul>
This will add a border to all <li>
elements except the last one.
last:border-0
as an Alternative
3. Using If you want to remove the border from the last element instead:
<ul>
<li class="border-b border-gray-500 last:border-0">Item</li>
</ul>
This approach is simple and doesn’t require :not()
.
group
for More Complex Selectors
4. Using For interactive elements, you can use group
and :not()
within arbitrary variants:
<div class="group">
<button class="group-[:not(:hover)]:bg-gray-300">Hover Me</button>
</div>
This applies a background color unless the button is hovered.
FAQs
:not()
directly?
Why doesn’t Tailwind support Tailwind focuses on utility-based classes, and :not()
is often replaced with alternative solutions like last:*
or first:*
.
:not()
work in all Tailwind versions?
Does No, arbitrary variants were introduced in Tailwind v3, and not-*
variants were added in Tailwind v4.
What’s the best method to exclude elements in Tailwind?
Use not-*
in Tailwind v4 or arbitrary variants ([&:not(:last-child)]
) in Tailwind v3.