Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target elements with a specific data attribute with Tailwind CSS?

Tags:

tailwind-css

I have several message boxes and each box has a data-author attribute.

If I want to apply styles to these message boxes in CSS, I can just do:

[data-author="Ben"] {
  background-color: blue;
}

But I'm not sure how to do this with Tailwind CSS or whether it's even possible. Any idea?

Thanks

like image 987
user84296 Avatar asked Sep 12 '25 15:09

user84296


1 Answers

As of Tailwind v3.2, data attribute variants are supported.

<!-- Will apply -->
<div data-size="large" class="data-[size=large]:p-8">
  <!-- ... -->
</div>

<!-- Will not apply -->
<div data-size="medium" class="data-[size=large]:p-8">
  <!-- ... -->
</div>

See here: https://tailwindcss.com/blog/tailwindcss-v3-2#data-attribute-variants

like image 151
landen Avatar answered Sep 15 '25 19:09

landen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!