CSS选择器$parent > $child选择嵌套列表中所有的
  • 子元素。
  • huangapple go评论61阅读模式
    英文:

    CSS selector $parent > $child selects all <li> child elements in nested lists

    问题

    The css selector $parent > $immediateChild is not working for nested lists.
    只有level-1列表中的直接&lt;li&gt;应为红色,但选择器选择了所有嵌套列表中的&lt;li&gt;

    ul.level-1 &gt; li
    {
        color: red;
    }
    
    &lt;ul class=&quot;level-1&quot;&gt;
        &lt;li&gt;Level 1
          &lt;ul&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    

    Also found this post and it states that the second &lt;ul&gt; needs to be in the &lt;li&gt; of the first <ul> to have valid html. I did that but it's not working.

    /EDIT: I just transformed my nested list to &lt;div&gt;s instead like before. It's a menu with a submenu and sub-submenu. The nested list is hard to address via CSS and there were also issues with my flexbox submenu.

    So, I can't recommend doing menus with a (nested) list because all styles are natively inherited to all childs which is probably not what you want. The accepted answer would solve this particular problem, though.

    英文:

    The css selector $parent > $immediateChild is not working for nested lists.
    Only the direct &lt;li&gt; of the level-1 list should be red, but the selector selects all &lt;li&gt; in all nested lists.

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

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

    ul.level-1 &gt; li
    {
        color: red;
    }
    

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

    &lt;ul class=&quot;level-1&quot;&gt;
        &lt;li&gt;Level 1
          &lt;ul&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    

    <!-- end snippet -->

    Also found this post and it states that the second &lt;ul&gt; needs to be in the &lt;li&gt; of the first <ul> to have valid html. I did that but it's not working.

    /EDIT: I just transformed my nested list to &lt;div&gt;s instead like before. It's a menu with a submenu and sub-submenu. The nested list is hard to address via CSS and there were also issues with my flexbox submenu.

    So, I can't recommend doing menus with a (nested) list because all styles are natively inherited to all childs which is probably not what you want. The accepted answer would solve this particular problem, though.

    答案1

    得分: 2

    样式会自动应用("继承")到目标子元素的后代元素上,就像你的例子中一样。

    如果你想让第二层的 li 元素的文本颜色为黑色,你需要再次选择它们:

    ul.level-1 > li {
        color: red;
    }
    ul.level-1 > li > ul > li {
        color: black;
    }
    
    <ul class="level-1">
        <li>第一层 - 1个子元素
            <ul>
                <li>第二层</li>
            </ul>
        </li>
        <li>第一层 - 无子元素</li>
        <li>第一层 - 1个或多个子元素
            <ul>
                <li>第二层</li>
                <li>第二层</li>
            </ul>
        </li>
    </ul>
    
    英文:

    Styles automatically get applied ("inherited") to descendants of targeted children, just like in your example.

    If you want the second layer of li elements to e.g. have black text color, you have to target them again:

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

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

    ul.level-1 &gt; li
    {
        color: red;
    }
    ul.level-1 &gt; li &gt; ul &gt; li
    {
        color: black;
    }
    

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

    &lt;ul class=&quot;level-1&quot;&gt;
        &lt;li&gt;Level 1 - 1 child
          &lt;ul&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;Level 1 - no children&lt;/li&gt;
        &lt;li&gt;Level 1 - 1+ children
          &lt;ul&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    

    <!-- end snippet -->

    答案2

    得分: 2

    A lot of styles are inherited down to the child elements of the element you style.如果CSS属性的默认值是inherit,它将使用父元素为该属性设置的值。只需对第二个ul进行样式设置就足以改变颜色。另一种解决方法是重置颜色:color: initial;这也会使文本颜色恢复到原始颜色(在大多数情况下为黑色)。StackOverflow上的这个答案列出了一些继承属性。web.dev上也有一些相关信息。

    英文:

    A lot of styles are inherited down to the child elements of the element you style.

    If the default value of an CSS property is inherit, it uses the value the parent has set for that property.

    Styling the second ul would already be enough to change the color.

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

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

    ul.level-1 &gt; li
    {
        color: red;
    }
    
        ul.level-1 &gt; li &gt; ul
        {
            color: green;
        }
    

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

    &lt;ul class=&quot;level-1&quot;&gt;
        &lt;li&gt;Level 1
          &lt;ul&gt;
              &lt;li&gt;Level 2&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    

    <!-- end snippet -->

    Another way to fix the problem is to reset the color:

    color: initial;
    

    This would also make the text color revert to the original (black in most of the cases).

    This answer on StackOverflow has a list of inherited properties.

    web.dev also has some information about it.

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

    发表评论

    匿名网友

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

    确定