使用自定义类作为Tailwind中的条件。

huangapple go评论62阅读模式
英文:

Use custom class as conditions in tailwind

问题

在Tailwind中,可以使用以下方式实现条件样式:

<div class="parent-small:p-0">...</div>

这将在父元素具有"class='small'"时应用样式"p-0"到该元素。这种方式可以帮助你避免编写冗长的重复样式类。

英文:

Can I make a condition in the tailwind that if the parent has a class for example "small", then this element will have the property "p-0" used. Is it possible to implement this without writing all the options in css, but using something like this: "parent-small: p-0"

I tried to just add the necessary classes to the style file, but this results in a lot of monotonous classes, and I would like to know if it is possible to do this using tailwind

答案1

得分: 0

是的!你可以考虑使用group变体,如下所示:

<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>
英文:

Yep! You could consider using a group variant like:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.2&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;group&quot;&gt;
  &lt;div class=&quot;p-10 bg-red-200 group-[.small]:p-0&quot;&gt;
    Foo
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;group small&quot;&gt;
  &lt;div class=&quot;p-10 bg-green-200 group-[.small]:p-0&quot;&gt;
    Bar
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 21:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定