Tailwind practice using custom classes as conditions
P粉513316221
P粉513316221 2023-09-08 14:08:57
0
1
514

Can I set a condition in tailwind? If the parent element has a class, such as "small", then this element will use the attribute "p-0". Is it possible to achieve this without writing all the options, but using something like "parent-small: p-0"?

I tried adding only the necessary classes to the style file, but this resulted in a lot of duplicate classes and I was wondering if I could use tailwind to achieve this.

P粉513316221
P粉513316221

reply all(1)
P粉438918323

Yes! You may consider using the group variant like this:

<script src="https://cdn.tailwindcss.com/3.3.2"></script>

<div class="group">
  <div class="p-10 bg-red-200 group-[.small]:p-0">
    Foo
  </div>
</div>

<div class="group small">
  <div class="p-10 bg-green-200 group-[.small]:p-0">
    Bar
  </div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!