CSS 保留父级规则而不是类
在这种情况下:
<ul id="menu_pages">
<li class="menu_pages_title">
<div id="menu_pages_container">
<a href="#">AAA</a>
<div class="page_content">
<div class="page_content_actions">
<a class="edit" href="#">BBB</a>
</div>
test 1
</div>
</div>
</li>
</ul>
ul#menu_pages li{
margin-bottom: 10px;
}
ul#menu_pages li a:link,ul#menu_pages li a:active,ul#menu_pages li a:visited{
display:block;
padding:5px;
border:1px solid #C3D5DF;
line-height:25px;
font-size: 15px;
font-weight: bold;
color:#C3D5DF;
}
div.page_content_actions li a.edit:link, div.page_content_actions a.edit:active, div.page_content_actions a.edit:visited {
background: url('/site_images/page_edit.png') left no-repeat;
display:block;
padding:0;
border:0;
font-size: 11px;
font-weight: bold;
color:#fff;
}
.edit 类应该获得自己的规则集(在 .page_content_actions 内),但 .edit 只获取背景,其余部分来自一般的 #menu_pages li a。这是为什么?
in this situation:
<ul id="menu_pages">
<li class="menu_pages_title">
<div id="menu_pages_container">
<a href="#">AAA</a>
<div class="page_content">
<div class="page_content_actions">
<a class="edit" href="#">BBB</a>
</div>
test 1
</div>
</div>
</li>
</ul>
ul#menu_pages li{
margin-bottom: 10px;
}
ul#menu_pages li a:link,ul#menu_pages li a:active,ul#menu_pages li a:visited{
display:block;
padding:5px;
border:1px solid #C3D5DF;
line-height:25px;
font-size: 15px;
font-weight: bold;
color:#C3D5DF;
}
div.page_content_actions li a.edit:link, div.page_content_actions a.edit:active, div.page_content_actions a.edit:visited {
background: url('/site_images/page_edit.png') left no-repeat;
display:block;
padding:0;
border:0;
font-size: 11px;
font-weight: bold;
color:#fff;
}
the .edit class should get its own rules set (inside .page_content_actions) but instead from .edit is just getting the background, the rest is coming from the general #menu_pages li a. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CSS 尝试选择元素的最具体定义,
例如
1.
2.
(2) 具有优先级,因为它更具体
CSS tries to pick most specific definition of an element
e.g
1.
2.
(2) has priority because its more specific
您可以使用
!important
来防止覆盖样式示例:
在上面的示例中,将应用
important
版本。you can use the
!important
to prevent overriding of stylesExample:
in the above example the
important
version would be applied.CSS 遵循特异性规则,这意味着如果您有两个可能同时适用的类,则会对它们进行权衡并应用更具体的一个。请参阅这篇优秀文章以获得更深入的解释:
http://htmldog.com/guides/cssadvanced/特异性/
在您的情况下,
#menu_pages li a
的特异性为 102(id 选择器为 100,2 个 html 元素选择器为 1+1),而a.edit< /code> 的特异性仅为 11(类选择器为 10,html 选择器为 1)。要么降低第一个的特异性,要么增加第二个的特异性,以达到您想要的效果。
此外,您可以在任何想要覆盖具有更高特异性的规则的规则末尾使用
!important
。请注意,这必须单独应用于每种样式。CSS follows specificity rules, which means that if you have two classes that might both apply, they are weighed out and the more specific one is applied. See this excellent article for a more in-depth explanation:
http://htmldog.com/guides/cssadvanced/specificity/
In your case,
#menu_pages li a
has a specificity of 102 (100 for an id selector and 1+1 for 2 html element selectors) whilea.edit
has a specificity of just 11 (10 for a class selector and 1 for an html selector). Either reduce the specificity of the first or increase the specificity of the second to achieve your desired effect.Additionally, you could use
!important
at the end of any rule that you want to override another rule with higher specificity. Note that this must be applied to each style individually.因为更具体,或者有一个更长的选择器 (#menu_pages li a) 会覆盖简单的 .edit 选择器。
Because being more specific, or having a longer selector (#menu_pages li a) overides the simple .edit one.