我有关于CSS声明优先级的问题。我的页面包含一个外部CSS文件,其中包含一个规则和一些内联CSS声明,这些声明应该覆盖该规则。据我所知,内联样式声明应该覆盖外部CSS声明。然而,当我在Chrome中查看页面时,表格的第二行显示为蓝色,而它应该显示为内部样式声明中定义的红色。
我在这里错过了什么
下面是HTML:
<html>
<head>
<link rel="stylesheet" href="screen.css" type="text/css" media="screen, projection">
<style type="text/css">
td,tr,th
{
background: Red;
}
</style>
</head>
<body>
<table>
<tr>
<td>11</td>
<td>22</td>
</tr>
<tr>
<td>aa</td>
<td>bb</td>
</tr>
</table>
</body>
</html>下面是CSS文件的内容:
tbody tr:nth-child(even) td,
tbody tr.even td
{
background: #e5ecf9;
}发布于 2012-08-23 16:53:05
在计算哪个规则具有最高特异性时,选择器的数量很重要。
CSS特异性的两个优秀可视化是http://www.standardista.com/css3/css-specificity/和http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html。
您应该避免仅将!important粘贴在要覆盖的规则上(请参见What are the implications of using "!important" in CSS?),而应更改选择器,以使您的规则具有更大或相同的权重。
例如,下面的代码会将所有单元格设置为background:red
thead tr:nth-child(1n) th, tbody tr:nth-child(1n) td {
background:red;
}发布于 2012-08-23 17:00:12
在这种情况下,更详细的规则将得到它。
在你的HTML中试试这个:
<style type="text/css" media="screen, projection">
tbody tr:nth-child(even) td,
tbody tr:nth-child(even) tr,
tbody tr:nth-child(even) th
{
background: Red;
}
</style>诚挚的问候
https://stackoverflow.com/questions/12088007
复制相似问题