search

Home  >  Q&A  >  body text

Tailwind practice using custom classes as conditions

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粉513316221P粉513316221445 days ago524

reply all(1)I'll reply

  • P粉438918323

    P粉4389183232023-09-09 00:47:42

    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>

    reply
    0
  • Cancelreply