CSS使用:不是带CLASS的ID

时间:2022-11-25 18:06:30

Does someone know how to use the CSS selector :not() as #some_id:not(.any_class_name)?

有人知道如何使用CSS选择器:not()as #some_id:not(.any_class_name)?

The code looks as if it is right, but it doesn't work. Is there another way without the not selector? I couldn't find anything on the Internet.

代码看起来好像是正确的,但它不起作用。没有选择器还有另一种方法吗?我在互联网上找不到任何东西。

I am making a web application, which includes more than one page, but several pages have divs with id=some_id. I thought I had to add specific CSS by adding any_class_name one time using the above CSS code solve the problem, but it doesn't work.

我正在制作一个包含多个页面的Web应用程序,但是有几个页面的div具有id = some_id。我以为我必须通过添加any_class_name添加特定的CSS一次使用上面的CSS代码解决问题,但它不起作用。

3 个解决方案

#1


18  

I believe that you are reversing the selectors. You have some elements with the same class, but you want to filter out an element with an specific ID. In that case:

我相信你正在扭转选择者。您有一些具有相同类的元素,但您想要过滤掉具有特定ID的元素。在这种情况下:

HTML:

<p class="someclass">hello</p> <!-- will be targeted by css below, thus green -->
<p class="someclass" id="some-id">hi</p> <!-- will not be targeted by css below -->

CSS:

.someclass:not(#some-id){ color: green; } 
/* selects all elements with classname 'someclass', 
   but excludes the one that has also has an id of 'some-id' */

And as @secretSquirrel pointed out, note the browser compatibility: this selector is not supported by Internet Explorer 8 and older.

正如@secretSquirrel指出的那样,请注意浏览器兼容性:Internet Explorer 8及更早版本不支持此选择器。

#2


2  

This will set all the backgrounds except the ones that has <a></a>:

这将设置除 之外的所有背景:

:not(a)
{
     background: gray;
}

#3


1  

I hope this will help you.

我希望这能帮到您。

Demo

Another way is: You can override the css. May be you want something like this.

另一种方法是:你可以覆盖css。也许你想要这样的东西。

<div id="demo">
<p class="class">This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>

Your css

#demo
{
color:#0000ff;
}
.class
{
color:#ff0000;
}

#1


18  

I believe that you are reversing the selectors. You have some elements with the same class, but you want to filter out an element with an specific ID. In that case:

我相信你正在扭转选择者。您有一些具有相同类的元素,但您想要过滤掉具有特定ID的元素。在这种情况下:

HTML:

<p class="someclass">hello</p> <!-- will be targeted by css below, thus green -->
<p class="someclass" id="some-id">hi</p> <!-- will not be targeted by css below -->

CSS:

.someclass:not(#some-id){ color: green; } 
/* selects all elements with classname 'someclass', 
   but excludes the one that has also has an id of 'some-id' */

And as @secretSquirrel pointed out, note the browser compatibility: this selector is not supported by Internet Explorer 8 and older.

正如@secretSquirrel指出的那样,请注意浏览器兼容性:Internet Explorer 8及更早版本不支持此选择器。

#2


2  

This will set all the backgrounds except the ones that has <a></a>:

这将设置除 之外的所有背景:

:not(a)
{
     background: gray;
}

#3


1  

I hope this will help you.

我希望这能帮到您。

Demo

Another way is: You can override the css. May be you want something like this.

另一种方法是:你可以覆盖css。也许你想要这样的东西。

<div id="demo">
<p class="class">This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>

Your css

#demo
{
color:#0000ff;
}
.class
{
color:#ff0000;
}