如何选择 <element> 的所有实例 - 并且仅在 - .class 中?(例如,选择“.mid-container”类中的所有 <h1> 元素,同时排除“.mid-container”之外的 <h1> 元素)。
作为参考,这是我尝试过的示例测试,但它都失败了,因为 <h'x'> 元素被选择在“中间容器”之外(并且 的代码.mid-container.h2
甚至不起作用):
.mid-container>h1 {
color: #EAF6F6;
}
.mid-container.h2 {
color: #66BFBF;
}
.mid-container h3{
color: #11999E;
}
您的第一次和第三次尝试都是正确的,具体取决于您所追求的确切行为。
第一个 ,.mid-container > h1
将选择 的所有<h1>
直接子元素.mid-container
。
第三个 ,.mid-container h1
将选择<h1>
元素下方任何位置的所有元素.mid-container
。
如果它不起作用,则可能是您缺少结束标记或在标记中的某处存在其他结构性问题。
.mid-container {
border: 1px solid grey;
}
.mid-container h1 {
color: blue;
}
.mid-container > h1 {
background: red;
color: white;
}
<div>
<h1>Not in mid-container</h1>
</div>
<div class="mid-container">
<h1>Immediate child of mid-container</h1>
<div>
<h1>Deeper child of mid-container</h1>
</div>
</div>
<div>
<h1>Not in mid-container</h1>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句