CSS选择器,根据父元素中的类条件显示前n个子元素

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

CSS selector to show top n child elements based on class conditions in parent element

问题

我有一个HTML布局,其中有一个面板,其中有几个具有类"field"的子div。其中一些子div具有类"hide"。

当面板具有类"ShowSingleRow"时,我希望只显示前4个不具有"hide"类的子div。

当面板没有类"ShowSingleRow"时,我希望只显示前8个不具有"hide"类的子div。这种情况在这里有效

我尝试使用CSS选择器,如:not()、:nth-child()和:nth-of-type()来实现这一目标,但我没有得到期望的结果。

我认为问题出在**nth-of-type(-n+4)**上,在下面共享的示例中,我希望看到"one two four five"。它检查前四个子div并显示"one two four"(因为值为"three"的div具有"hide"类)。是否有一种方法可以检查前4个匹配的div,排除具有"hide"类的div?

我正在寻找一个通用的解决方案,其中子div的数量可能不同。

英文:

I have a HTML layout where there is a panel with several child divs that have the class "field". Some of these child divs have the class "hide".

When the panel has the class "ShowSingleRow", I want to display only the top 4 child divs that do not have the "hide" class.

When the panel does not have the "ShowSingleRow" class, I want to display only top 8 child divs that do not have the "hide" class. This scenario is working here.

I have tried using CSS selectors such as :not(), :nth-child(), and :nth-of-type() to achieve this, but I am not getting the desired result.

I think the issue is with nth-of-type(-n+4), in the example shared below, I want to see "one two four five". It checks first four child divs and shows "one two four" (as div with value "three" has "hide" class). Is there a way it can check top 4 matching divs excluding the ones with "hide" class?

I am looking for a generic solution where child divs can vary in count.

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

<!-- language: lang-css -->

.panel&gt;.field {
  display: none;
}

.panel:not(.ShowSingleRow)&gt;.field:not(.hide):nth-of-type(-n+8) {
  display: inline-block;
}

.panel.ShowSingleRow&gt;.field:not(.hide):nth-of-type(-n+4) {
  display: inline-block;
}

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

&lt;div class=&quot;panel ShowSingleRow&quot;&gt;
  &lt;div class=&quot;field&quot;&gt;one&lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;two&lt;/div&gt;
  &lt;div class=&quot;field hide&quot;&gt;three&lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;four&lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;five&lt;/div&gt;
  &lt;div class=&quot;field hide&quot;&gt;six&lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;seven&lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;eight&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

.panel.ShowSingleRow > .field:nth-child(-n+4 of :not(.hide))

但是目前Firefox还不支持,其他Chromium浏览器可能需要一些时间才能提供支持。

英文:

Safari, and the next version of Chrome will support

.panel.ShowSingleRow&gt;.field:nth-child(-n+4 of :not(.hide))

But there's no Firefox support yet, and other Chromium browsers may take some time to become available.

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

发表评论

匿名网友

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

确定