Rob*_*ool 7 css inheritance css-selectors
如何在不更改或删除父选择器的情况下使此链接使用子选择器?(我希望链接为蓝色.)
<html>
<head>
<style>
.parent a { color:Red; }
.child { color:Blue; }
</style>
</head>
<body>
<div class="parent">
<a class="child" href="http://www.stackoverflow.com">
stackoverflow
</a>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,在这种情况下,父母会覆盖孩子!
使用a.child的选择.
<html>
<head>
<style>
.parent a { color:Red; }
a.child { color:Blue; }
</style>
</head>
<body>
<div class="parent">
<a class="child" href="http://www.stackoverflow.com">
stackoverflow
</a>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是由于CSS特异性.额外的a追求.parent使它更具体而不仅仅是.parent,相应地,更具体而不仅仅是.child.Obalix的建议为选择器提供了相同的特异性,两者都具有基本HTML元素和类别名称.当特异性相等时,它将应用层次结构中指定的最深值,如您所料.
本文(以及它所链接的资源)在解释CSS特异性方面做得很好:http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html