[英]How to create a CSS style which override another CSS style
我有2种CSS样式,除了一种特定的“宽度”和另一种没有的“宽度”外,它们几乎相同。 如何创建继承另一个CSS样式但覆盖属性的CSS样式?
.FirstStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
width: 5px;
float: left;
}
.SecondStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
float: left;
}
你可以这样尝试
在以上两个类中,宽度未定义
.FirstStyle, .SecondStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
width: 5px;
float: left;
}
在这里,您只能为FirstStyle类定义宽度..(无论如何,该类位于其他类之下可能会覆盖上面指定的最终宽度)
.FirtsStyle
{
width: 5px;
}
如何创建继承另一个CSS样式但覆盖属性的CSS样式?
后续的CSS样式会自动覆盖前面的属性,因此只需在要覆盖的属性下面添加该属性,它将继承除指定的新样式之外的所有内容。
例:
HTML:
<div class="someClass anotherClass">
....
</div>
CSS:
.someClass {
width: 100px;
color: green;
}
.anotherClass {
width: 200px; <-- This will override the above values
color: red;
}
.someClass {
width: 450px !important; <-- The !important tag will override both suceeding as well as preceeding properties
}
.someClass {
width: 500px; <-- This will override the first two styling properties
}
您可以计算CSS特异性值
笔记
:first-line
)得到0,0,0,1,不同于它们的伪类弟兄得到0,0,1,0 :not()
本身:not()
添加任何特异性,仅添加括号内的内容。 !important
值是自动获胜。 它甚至覆盖了标记中的内联样式。 可以覆盖!important
值的唯一方法是使用稍后在CSS中声明的另一个!important规则,否则使用相等或很大的特异性值。 您可以将其添加为特异性值1,0,0,0,0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.