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

<div> vs <nav> when selecting last-of-type by .class in a nested structure

问题

我想保持简单。这个想法是选择嵌套结构中特定类的最后一个直接子元素。为什么它适用于 <nav> 标签,但不适用于 <div>

使用 <div> 标签:

.super > .sub:last-of-type {
  background-color: turquoise;
}
<div class="super">
  <div class="sub">1</div>
  <div class="sub">2</div>
  <div class="super">
    <div class="sub">_1</div>
    <div class="sub">_2</div>
    <div class="super">
      <div class="sub">__1</div>
      <div class="sub">__2</div>
    </div>
  </div>
</div>

使用 <nav> 标签:

.super > .sub:last-of-type {
  background-color: turquoise;
}
<nav class="super">
  <div class="sub">1</div>
  <div class="sub">2</div>
  <nav class="super">
    <div class="sub">_1</div>
    <div class="sub">_2</div>
    <nav class="super">
      <div class="sub">__1</div>
      <div class="sub">__2</div>
    </nav>
  </nav>
</nav>

所以我有两个问题:

  1. 为什么?
  2. 我能否使用 <div> 标签实现与 <nav> 标签相同的行为?
英文:

I want to keep it simple. The idea is to select the last immediate child by a certain class in a nested structure. Why it works with &lt;nav&gt; tag but &lt;div&gt;?

With &lt;div&gt; tag:

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

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

.super &gt; .sub:last-of-type {
  background-color: turquoise;
}

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

&lt;div class=&quot;super&quot;&gt;
  &lt;div class=&quot;sub&quot;&gt;1&lt;/div&gt;
  &lt;div class=&quot;sub&quot;&gt;2&lt;/div&gt;
  &lt;div class=&quot;super&quot;&gt;
    &lt;div class=&quot;sub&quot;&gt;_1&lt;/div&gt;
    &lt;div class=&quot;sub&quot;&gt;_2&lt;/div&gt;
    &lt;div class=&quot;super&quot;&gt;
      &lt;div class=&quot;sub&quot;&gt;__1&lt;/div&gt;
      &lt;div class=&quot;sub&quot;&gt;__2&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

With &lt;nav&gt; tag:

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

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

.super &gt; .sub:last-of-type {
  background-color: turquoise;
}

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

&lt;nav class=&quot;super&quot;&gt;
  &lt;div class=&quot;sub&quot;&gt;1&lt;/div&gt;
  &lt;div class=&quot;sub&quot;&gt;2&lt;/div&gt;
  &lt;nav class=&quot;super&quot;&gt;
    &lt;div class=&quot;sub&quot;&gt;_1&lt;/div&gt;
    &lt;div class=&quot;sub&quot;&gt;_2&lt;/div&gt;
    &lt;nav class=&quot;super&quot;&gt;
      &lt;div class=&quot;sub&quot;&gt;__1&lt;/div&gt;
      &lt;div class=&quot;sub&quot;&gt;__2&lt;/div&gt;
    &lt;/nav&gt;
  &lt;/nav&gt;
&lt;/nav&gt;

<!-- end snippet -->

So I have two questions here.

  1. Why?
  2. Could I even achieve the same &lt;nav&gt; behaviour with &lt;div&gt; tag?

答案1

得分: 3

我认为你感到困惑的原因是 :last-of-type 可以被理解为"标签的最后一个"而不是"选择器的最后一个"。

所以,当你使用 .sub:last-of-type 并且选择所有的 DIV 元素时,实际上是在说"最后一个同时也有 .sub 类的 div"。让我们再看一下你的代码:

<div class="super">
  <div class="sub">1</div>
  <div class="sub">2</div> <!-- 这不是最后一个 DIV -->
  <div class="super">
    <div class="sub">_1</div>
    <div class="sub">_2</div> <!-- 这不是最后一个 DIV -->
    <div class="super">
      <div class="sub">__1</div>
      <div class="sub">__2</div> <!-- 这是最后一个 DIV -->
    </div>
  </div>
</div>

在上面的示例中,你有两个不是最后一个 DIV 类型的 DIV 元素,尽管它们是带有 .sub 类的类型的最后一个 - 这不是 :last-of-type 选择器的目标。这很令人困惑。

当你使用 nav 时,.sub 元素确实是 "DIV" 类型的最后一个:

<nav class="super">
  <div class="sub">1</div>
  <div class="sub">2</div> <!-- 这是 DIV 类型的最后一个 -->
  <nav class="super">
    <div class="sub">_1</div>
    <div class="sub">_2</div> <!-- 这是 DIV 类型的最后一个 -->
    <nav class="super">
      <div class="sub">__1</div>
      <div class="sub">__2</div> <!-- 这是 DIV 类型的最后一个 -->
    </nav>
  </nav>
</nav>

更新(来自评论): 我建议使用除 DIV 以外的任何标签作为 .super 元素,就像上面的 nav 标签一样。你甚至可以创建自己的自定义元素

问题提问者最终找到了一个仅使用 CSS 的解决方案,我将根据他们的要求发布,尽管我个人发现它很难阅读和理解。不过这是一个聪明的解决方案:

.super > .sub:has(+ .super), 
.super > .sub:last-child { 
  background-color: turquoise; 
}
英文:

I think the confusion you are having is that :last-of-type can be thought of as "last of tag" rather than "last of selector".

So, when you do .sub:last-of-type and you are using all DIVs, you are effectively saying "the last div which also has the class .sub". Let's look at your code again:

&lt;div class=&quot;super&quot;&gt;
  &lt;div class=&quot;sub&quot;&gt;1&lt;/div&gt;
  &lt;div class=&quot;sub&quot;&gt;2&lt;/div&gt; &lt;!-- this is not the last DIV --&gt;
  &lt;div class=&quot;super&quot;&gt;
    &lt;div class=&quot;sub&quot;&gt;_1&lt;/div&gt;
    &lt;div class=&quot;sub&quot;&gt;_2&lt;/div&gt; &lt;!-- this is not the last DIV --&gt;
    &lt;div class=&quot;super&quot;&gt;
      &lt;div class=&quot;sub&quot;&gt;__1&lt;/div&gt;
      &lt;div class=&quot;sub&quot;&gt;__2&lt;/div&gt; &lt;!-- this IS the last DIV --&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

In the above example, you have two DIVs which are not the last of type DIV, even thought they are the last of type with class .sub - that's not what the :last-of-type selector targets. It's confusing.

When you use nav, the .sub elements are indeed the last of type "DIV":

&lt;nav class=&quot;super&quot;&gt;
  &lt;div class=&quot;sub&quot;&gt;1&lt;/div&gt;
  &lt;div class=&quot;sub&quot;&gt;2&lt;/div&gt; &lt;!-- this is the last of type DIV --&gt;
  &lt;nav class=&quot;super&quot;&gt;
    &lt;div class=&quot;sub&quot;&gt;_1&lt;/div&gt;
    &lt;div class=&quot;sub&quot;&gt;_2&lt;/div&gt; &lt;!-- this is the last of type DIV --&gt;
    &lt;nav class=&quot;super&quot;&gt;
      &lt;div class=&quot;sub&quot;&gt;__1&lt;/div&gt;
      &lt;div class=&quot;sub&quot;&gt;__2&lt;/div&gt; &lt;!-- this is the last of type DIV --&gt;
    &lt;/nav&gt;
  &lt;/nav&gt;
&lt;/nav&gt;

UPDATE (from comments): My suggestion would be to use any tag other than DIV for the .super elements as is done with the nav tags above. You can even create your own custom elements.

The OP eventually found a CSS-only solution which I am posting on their behest, though I personally find it hard to read and understand. It is a clever solution nonetheless:

.super &gt; .sub:has(+ .super), 
.super &gt; .sub:last-child { 
  background-color: turquoise; 
}

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

发表评论

匿名网友

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

确定