Html 将 CSS 样式应用于除特定 ID 之外的所有元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19464660/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:37:18  来源:igfitidea点击:

Apply CSS Style on all elements except with a SPECIFIC ID

htmlcss

提问by Rajesh Paul

CSS Code(what I need)

CSS 代码(我需要的)

<style>
    div[id!='div1']// I actually needed an inequality operator for NOT EQUAL TO
    {
        font-size:40px;
    }
</style>

HTML code

HTML代码

<body>
    <div>abc</div>
    <div>def</div>
    <div id='div1'>ghi</div>
</body>

The CSS didn't work as I intended.

CSS 没有按我的预期工作。

I actually wanted to define the style for all <div>-elements except the one with id='div1'.

我实际上想为所有<div>元素定义样式,除了带有id='div1'.

How can I do that?

我怎样才能做到这一点?

回答by Vucko

Use the :notselector:

使用:not选择器:

div:not(#bar){
    color:red;
}
<div>foo</div>
<div id="bar">bar</div>



Update : nameinstead of ID:

更新:name而不是ID

div:not([name="bar"]){
    color:red;
}
<div>foo</div>
<div name="bar">bar</div>



Update: CSS2 selector, similar answer to Tom Heard-s:

更新:CSS2 选择器,与Tom Heard-s类似的答案:

div{
    color:red;
}

div[name="bar"]{
    color:blue;
}
<div>foo</div>
<div name="bar">bar</div>

Also, see selectivizr

另见选择性

回答by Tom Heard

CSS3 solution:

CSS3解决方案:

div:not(#div1)

CSS2 solution:

CSS2解决方案:

div {
    color: red;
}

div#div1 {
    color: inherit;
}

By name:

按名字:

CSS3 solution:

CSS3解决方案:

div:not([name=div1]) {
    color: red;
}

CSS2 Solution (note not supported in IE6 - but who cares anymore):

CSS2 解决方案(注意 IE6 不支持 - 但谁在乎):

div {
    color: red;
}

div[name=div1] {
    color: inherit;
}

回答by Michael Falck Wedelg?rd

If you are only to target browsers supporting CSS3 selectors you could do this:

如果您只针对支持 CSS3 选择器的浏览器,您可以这样做:

div:not(#div1) {
...my styles...
}