如何组合多个任意值,例如在Tailwind中使用的`:has`。

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

How to combine multiple arbitrary values such as :has in Tailwind

问题

在纯CSS中,你可以这样实现:

  1. &:not(:has(*)){
  2. display: none
  3. }

然而,在Tailwind CSS中,我无法找到相应的方法。我尝试了一些方法,比如下面的方式,但都无法正常工作:

  1. [&:not(:has(*)):hidden]
  2. [&:not([&:has(*)]):hidden]

是否有一种方法可以结合多个任意值?

英文:

I have a problem where I want an element to not show, if it has children.

In plain CSS this is:

  1. &:not(:has(*)){
  2. display: none
  3. }

However, in Tailwind I can not figure it out. I've tried few things such as below, but none would work:

  1. [&:not(:has(*)):hidden]
  2. [&:not([&:has(*)]):hidden]

Is there a way to combine multiple arbitrary values?

答案1

得分: 1

你的语法不正确 - 你希望附加到变体的类位于方括号之外:

  1. <!-- begin snippet: js hide: false console: false babel: false -->
  2. <!-- language: lang-html -->
  3. <script src="https://cdn.tailwindcss.com"></script>
  4. <div class="[&]:not(:has(*)):hidden">
  5. Foo
  6. </div>
  7. <div class="[&]:not(:has(*)):hidden">
  8. <div>Bar</div>
  9. </div>
  10. <!-- end snippet -->
英文:

Your syntax is incorrect – you'd want the class attached to the variant to be outside the square brackets:

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

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

  1. &lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
  2. &lt;div class=&quot;[&amp;:not(:has(*))]:hidden&quot;&gt;
  3. Foo
  4. &lt;/div&gt;
  5. &lt;div class=&quot;[&amp;:not(:has(*))]:hidden&quot;&gt;
  6. &lt;div&gt;Bar&lt;/div&gt;
  7. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 12:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528632.html
匿名

发表评论

匿名网友

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

确定