Html 如何不在特定标签上应用 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7585561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do not apply CSS on a specific TAG
提问by GibboK
I do not know if is possible in CSS, but I would like a general Role to don't apply to a specific element in my case id=special
.
In my example all the DIV should be red but I want only id=special
to have not applied this color.
I understand I can always overwrite the element with id=special
but I'm interested to know if is possible have some sort of exclusion from the general Role.
我不知道在 CSS 中是否可行,但我希望通用角色不适用于我的案例中的特定元素id=special
。在我的示例中,所有 DIV 都应该是红色的,但我只想id=special
不应用这种颜色。我知道我总是可以覆盖元素,id=special
但我很想知道是否有可能从一般角色中排除某种形式。
Any idea in CSS 2 and CSS 3?
在 CSS 2 和 CSS 3 中有什么想法吗?
Example:
例子:
<div>this is red</div>
<div>this is red</div>
<div id="special">this one must be blus</div>
div>this is red</div>
// My General Role
div
{
background-color:Red;
}
采纳答案by Germán Rodríguez
CSS2 Solution
CSS2 解决方案
div {
color: red;
}
div#special {
color: inherit;
}
回答by JNDPNT
CSS3 solution:
CSS3解决方案:
div:not(#special)
回答by zzzzBov
CSS3 allows for the :not()
selector:
CSS3 允许:not()
选择器:
div:not([id])
-or-
div:not(#special)
depending on what you want to exclude
You can read all about the selectors in the w3c docs.
As for CSS2, there's a limited set of selectors, and they don't account for negation.
至于 CSS2,有一组有限的选择器,它们不考虑否定。
回答by ayyp
You would want to use div:not(#special)
, but if it's going to be blue anyways then you might as well just override the rule. The :not()
selector is a CSS3 property and thus isn't supported by all browsers.
你会想要使用div:not(#special)
,但如果它无论如何都会是蓝色的,那么你最好覆盖规则。该:not()
选择是CSS3属性,因此并非所有的浏览器都支持。
回答by Chris Baker
This is a great example of how CSS specificity works. A general rule applying to all elements of a certain type (your div
rule turning the background red) is superseded by the more specific rule applying to an ID. This concept has existed since CSS1, and is available in all browsers. Thus:
这是 CSS 特异性如何工作的一个很好的例子。适用于特定类型的所有元素的一般规则(您的div
规则将背景变为红色)被适用于 ID 的更具体的规则所取代。这个概念从 CSS1 开始就已经存在,并且在所有浏览器中都可用。因此:
div {
background-color:red;
}
#special {
background-color: blue;
}
... will leave all divs red. By applying the special
id to a div, that one div will be blue instead.
...将使所有 div 保持红色。通过将special
id 应用于 div,该 div 将变为蓝色。
See it in action: http://jsfiddle.net/9Hyas/
看到它在行动:http: //jsfiddle.net/9Hyas/
Check out these articles for more about CSS specificity: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/and http://www.htmldog.com/guides/cssadvanced/specificity/
查看这些文章以了解有关 CSS 特异性的更多信息:http: //coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/和http://www.htmldog.com/guides /cssadvanced/特殊性/