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

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

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

问题

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

&:not(:has(*)){
 display: none
}

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

[&:not(:has(*)):hidden] 

[&:not([&:has(*)]):hidden]

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

英文:

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

In plain CSS this is:

&:not(:has(*)){
 display: none
}

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

[&:not(:has(*)):hidden] 

[&:not([&:has(*)]):hidden]

Is there a way to combine multiple arbitrary values?

答案1

得分: 1

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

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

<!-- language: lang-html -->
    <script src="https://cdn.tailwindcss.com"></script>

    <div class="[&]:not(:has(*)):hidden">
      Foo
    </div>

    <div class="[&]:not(:has(*)):hidden">
      <div>Bar</div>
    </div>

<!-- 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 -->

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

&lt;div class=&quot;[&amp;:not(:has(*))]:hidden&quot;&gt;
  Foo
&lt;/div&gt;

&lt;div class=&quot;[&amp;:not(:has(*))]:hidden&quot;&gt;
  &lt;div&gt;Bar&lt;/div&gt;
&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:

确定