为什么元素样式会覆盖元素上设置的类?
我们的问题是这样的。 我们有一段这样的代码。
<div class="parent">
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 1</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 2</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 3</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 4</a>
</div>
<div class="clear">
</div>
</div>
这一切都在类 .content
的全局容器中。
CSS 代码:
.content a
{
font-size: 11px;
}
.parent a
{
font-size: 16px;
}
出于某种原因,浏览器没有应用 .parent a
,而是应用 .content a
。
出了什么问题,为什么应用容器 CSS 而不是更接近的 .parent CSS?
Our problem is like this.
We have piece of code like this.
<div class="parent">
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 1</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 2</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 3</a>
</div>
<div class="child">
<a href="#" class="child_item" onclick="return false;" onfocus="blur();">Item 4</a>
</div>
<div class="clear">
</div>
</div>
This is all in global container with class .content
.
CSS code:
.content a
{
font-size: 11px;
}
.parent a
{
font-size: 16px;
}
For some reason, instead of applying .parent a
, browsers are applying .content a
.
What is wrong and how come container CSS is applied instead of closer .parent a CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个规则具有相同的特异性,因此样式声明中最后出现的规则将获胜...您确定
.parent a
规则是在 之后 指定的 < code>.content a-规则?解决这个问题的另一种方法是稍微增加特异性,即:
.parent .child_item {
字体大小:16px;
}
编辑:您可以在此处尝试您的测试用例:http://jsfiddle .net/gburw/
为了证明我的观点,尝试切换 CSS 声明,您将看到最后定义的规则将“获胜”。
编辑2:您可以在此处了解有关 CSS 特异性的更多信息。这是一个非常容易掌握的概念,困难的部分是避免与其他开发人员发生特殊性战争 =) 因此,您应该想出一种在公司中编写 CSS 的标准方法。遵循 Pagespeed 和 YSlow 的准则也始终是一个好主意。
Both rules have the same specificity, so whichever rule comes last in the style declarations will win... Are you sure that the
.parent a
-rule is specified after the.content a
-rule?Another way to solve it would be to increase the specificity slightly, i.e:
.parent .child_item {
font-size: 16px;
}
Edit: You can play around with your test case here: http://jsfiddle.net/gburw/
To prove my point, try switching the CSS-declarations and you will see that whichever rule is defined last will "win".
Edit 2: You can read more about CSS specificity here. It's a pretty simple concept to grasp, the hard part is avoiding specificity wars with fellow developers =) So you should come up with a standard way you write CSS in your company. Following the guidelines of Pagespeed and YSlow is also always a good idea.
或者,如果您确实希望应用
.parent a
。您可以这样做:这将赋予它比
.content a
更多的权重,无论最后声明的是哪个。Or if you really want
.parent a
to be applied. You can do this:that will give it more weight than
.content a
regardless of which was declared last.听起来像是 CSS 特异性的问题。检查并确保您的 CSS 选择器实际上是:
而不是类似
#container .content a
的内容。如果情况并非如此,您还可以将.parent a
的特异性增加到.parent .child a
。Sounds like an issue of CSS Specificity. Check to make sure that your CSS selectors are actually:
and not someting like
#container .content a
. You could also increase the specificity of.parent a
to.parent .child a
if that's not the case.