我希望能够通过将一个类应用于锚的父div来更改锚的背景色,并且我需要选择器语法的帮助。
这是我的HTML,并且会有很多链接,所以这就是为什么我更愿意将类应用于div而不是应用于div中的每个锚点的原因:
<div id="container">
<div class="cell">
<a href="#">should be green</a>
<a href="#">should be green</a>
<a href="#">should be green</a>
</div>
<div class="cell color2">
<a href="#">should be red</a>
<a href="#">should be red</a>
<a href="#">should be red</a>
</div>
</div>
这是我的CSS,color2选择器的语法错误,并且不会更改color2锚点的背景色:
div.color2 a {
background: #f00;
}
#container a {
display:inline-block;
float: left;
height:14px;
background: #0f0;
}
第二个选择器的特异性大于第一个选择器,因此其样式将胜出(这是级联样式表中的“级联”,顺便说一句)。由于该ID位于选择器中,它将胜过仅包含元素或类的所有选择器。尝试这个:
#container div.color2 a {
background: #f00;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句