英文:
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列表中的直接<li>
应为红色,但选择器选择了所有嵌套列表中的<li>
。
ul.level-1 > li
{
color: red;
}
<ul class="level-1">
<li>Level 1
<ul>
<li>Level 2</li>
</ul>
</li>
</ul>
Also found this post and it states that the second <ul>
needs to be in the <li>
of the first <ul> to have valid html. I did that but it's not working.
/EDIT: I just transformed my nested list to <div>
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 <li>
of the level-1 list should be red, but the selector selects all <li>
in all nested lists.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
ul.level-1 > li
{
color: red;
}
<!-- language: lang-html -->
<ul class="level-1">
<li>Level 1
<ul>
<li>Level 2</li>
</ul>
</li>
</ul>
<!-- end snippet -->
Also found this post and it states that the second <ul>
needs to be in the <li>
of the first <ul> to have valid html. I did that but it's not working.
/EDIT: I just transformed my nested list to <div>
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 > li
{
color: red;
}
ul.level-1 > li > ul > li
{
color: black;
}
<!-- language: lang-html -->
<ul class="level-1">
<li>Level 1 - 1 child
<ul>
<li>Level 2</li>
</ul>
</li>
<li>Level 1 - no children</li>
<li>Level 1 - 1+ children
<ul>
<li>Level 2</li>
<li>Level 2</li>
</ul>
</li>
</ul>
<!-- 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 > li
{
color: red;
}
ul.level-1 > li > ul
{
color: green;
}
<!-- language: lang-html -->
<ul class="level-1">
<li>Level 1
<ul>
<li>Level 2</li>
</ul>
</li>
</ul>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论