The :not
selector in TailwindCSS is pretty much similar to the :nth
selector. The :not
selector is helpful in various use cases, for example, you have a list of elements and you don’t want to apply styles to all of them.
How to Use :not
Selector in TailwindCSS
Same as other selectors, you can use arbitrary values to not select a specific element(s) from the list. In the following example, there are 3 different headings with a paragraph. Let’s say you don’t want to style the paragraph elements but only the heading elements. You can get the benefit of the :not
selector in TailwindCSS. Here is the sample code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<div class="[&>*:not(p)]:font-bold [&>*:not(p)]:mt-10 "> <h1>This is heading </h1> <p> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Fugiat quis itaque officiis. Repellat nemo laudantium corporis. At temporibus consequatur doloribus voluptates praesentium a voluptate vero nulla! Ea repudiandae illum corrupti. </p> <h2>This is heading two</h2> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque sequi aut necessitatibus sapiente ex cum enim molestias? Aspernatur fugiat facilis pariatur vitae repellat, veritatis culpa cupiditate qui tempore quidem. Ipsum. </p> <h2>This is heading three</h2> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque sequi aut necessitatibus sapiente ex cum enim molestias? Aspernatur fugiat facilis pariatur vitae repellat, veritatis culpa cupiditate qui tempore quidem. Ipsum. </p> </div> |
In the above code, the font-bold
and the mt-10
class will only be applied to headings because the :not
selector is eliminating the paragraph elements. You can see the working example as well the output in the following screenshot.
Not Styling the First or Last Element with :not
Selector in TailwindCSS
There are other use-cases of :not
selector in TailwindCSS. For example, you are applying a border to a list of items and you don’t want to apply the border to the last element. Here is an example where you can stop the last element from being styled.
1 2 3 4 5 6 7 8 9 10 |
<div class="[&>*:not(:last-child)]:border "> <p class=" border-b-blue-300 py-2">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class=" border-b-blue-300 py-2">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class=" border-b-blue-300 py-2">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class=" border-b-blue-300 py-2">This is a paragraph to show an example of :not selector in tailwindcss</p> </div> |
Here is the working code example and you can see the output in the screenshot below.
Same as the above example, you can do the same to not apply styles to the first element.
1 2 3 4 5 6 7 8 9 10 |
<div class="[&>*:not(:first-child)]:text-gray-600 "> <p class="">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class="">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class="">This is a paragraph to show an example of :not selector in tailwindcss</p> <p class="">This is a paragraph to show an example of :not selector in tailwindcss</p> </div> |
Conclusion
You can use the :not
selector in TailwindCSS to not apply styles to specific types of elements. You can also use it with other selectors like first-child
, last-child
to achieve whatever you want. The :not
selecor is quite similar to the :nth-child
selector in TailwindCSS.