而不是使用!important
,你可以使用属性选择器 https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors这将覆盖默认值,因为它比简单地使用更具体class
。不擅长特异性概念?参考this http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/文章。
为了计算选择器的特异性,您可以使用this http://specificity.keegan.st/网站。
[1] Using class[attr=class]
.input-large[class="input-large"] {
width: 400px;
}
OR
[2] Using tag[attr=class]
input[class="input-large"] {
width: 400px;
}
并使用!important
如果你在正确的地方使用它,这并不是一个糟糕的做法。
Note that above selectors, [1] will make all elements width
to 400px
having a class
of input-large
and in case of selector [2], it will set width
to 400px
for all input
elements having class
of input-large
, so if you want, you can call multiple classes i.e .class.class
on input
tag and use the selector below..
.input-large.input-large-altered {
width: 400px;
}
所以这会选择any如果您想更具体并且只想设置,则设置这两个类的元素input type="text"
比你总是可以写一个更具体的选择器,比如
input[type="text"].input-large.input-large-altered {
width: 400px;
}
所以上面那句就only目标为input
元素,其type
属性持有一个value
of text
AND拥有两个班级,即.input-large
.input-large-altered
Demo http://jsfiddle.net/xryQK/1/
Demo 2 http://jsfiddle.net/xryQK/2/
Demo 3 http://jsfiddle.net/xryQK/557/(多班)