我可以编写CSS选择器来选择没有特定类或属性的元素吗?

HTML CSS

StafanHarry

2020-03-17

我想编写一个CSS选择器规则,以选择没有特定类的所有元素例如,给定以下HTML:

<html class="printable">
    <body class="printable">
        <h1 class="printable">Example</h1>
        <nav>
            <!-- Some menu links... -->
        </nav>
        <a href="javascript:void(0)" onclick="javascript:self.print()">Print me!</a>
        <p class="printable">
            This page is super interresting and you should print it!
        </p>
    </body>
</html>

我想编写一个选择器来选择所有没有“可打印”类元素,在这种情况下,它们是导航元素。

这可能吗?

注意:在实际的HTML中,我想使用它的地方将比包含“可打印的”类的元素多得多(在上面的示例中,我发现这是另一种方式)。

第1893篇《我可以编写CSS选择器来选择没有特定类或属性的元素吗?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
TonyPro 2020.03.17

正如其他人所说,您只需输入:not(.class)。对于CSS选择器,我建议访问此链接,这对我的整个旅程都非常有帮助:https : //code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048

div:not(.success) {
  color: red;
}

否定伪类特别有用。假设我要选择所有div,但有一个容器ID的div除外。上面的代码段将完美地完成该任务。

或者,如果我想选择除段落标记之外的所有单个元素(不建议使用),我们可以这样做:

*:not(p) {
  color: green;
}
小哥逆天 2020.03.17

如果您希望特定的类菜单登录的类丢失时具有特定的CSS

body:not(.logged-in) .menu  {
    display: none
}
L前端 2020.03.17

使用:not()伪类:

用于选择某个元素(或多个元素)以外的所有内容。我们可以使用:not() CSS伪类:not()伪类需要一个CSS选择作为其参数。选择器会将样式应用于所有元素,但指定为参数的元素除外。

例子:

/* This query selects All div elements except for   */
div:not(.foo) {
  background-color: red;
}


/* Selects all hovered nav elements inside section element except
   for the nav elements which have the ID foo*/
section nav:hover:not(#foo) {
  background-color: red;
}


/* selects all li elements inside an ul which are not odd */
ul li:not(:nth-child(odd)) { 
  color: red;
}
<div>test</div>
<div class="foo">test</div>

<br>

<section>
  <nav id="foo">test</nav>
  <nav>Hover me!!!</nav>
</section>
<nav></nav>

<br>

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

我们已经看到了此伪类的功能,它使我们可以通过排除某些元素来方便地微调选择器。此外,该伪类增加了选择器的特异性例如:

/* This selector has a higher specificity than the #foo below */
#foo:not(#bar) {
  color: red;
}

/* This selector is lower in the cascade but is overruled by the style above */
#foo {
  color: green;
}
<div id="foo">"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>

达蒙老丝 2020.03.17

您可以使用:not(.class)前面提到的选择器。

如果您关心Internet Explorer的兼容性,建议您使用http://selectivizr.com/

但是请记住在apache下运行它,否则您将看不到效果。

GilPro 2020.03.17

就像贡献:not()的上述答案以角度形式非常有效,而不是创建效果或调整视图/ DOM一样,

input.ng-invalid:not(.ng-pristine) { ... your css here i.e. border-color: red; ...}

确保在加载页面时,如果输入字段中添加了数据(即不再是原始数据)但无效,则它们只会显示无效字段(红色边框或背景等)。

猴子猴子Near 2020.03.17

我认为这应该工作:

:not(.printable)

来自“负CSS选择器”的答案

老丝Near 2020.03.17

:not否定伪类

否定CSS伪类,:not(X)是一种功能表示法,采用简单的选择器X作为参数。它与参数未表示的元素匹配。X不能包含另一个否定选择器。

您可以使用:not排除匹配元素的任何子集,就像常规CSS选择器一样进行排序。


简单示例:按类别排除

div:not(.class)

将选择所有div没有类的元素.class

div:not(.class) {
  color: red;
}
<div>Make me red!</div>
<div class="class">...but not me...</div>


复杂的示例:按类型/层次结构排除

:not(div) > div

将选择所有其他div元素的子元素div

div {
  color: black
}
:not(div) > div {
  color: red;
}
<div>Make me red!</div>
<div>
  <div>...but not me...</div>
</div>


复杂的例子:链接伪选择器

除了不能链接/嵌套:not选择器和伪元素这一显着例外,您可以将其与其他伪选择器结合使用。

div {
  color: black
}
:not(:nth-child(2)){
  color: red;
}
<div>
  <div>Make me red!</div>
  <div>...but not me...</div>
</div>


浏览器支持

:notCSS3级别选择器,在支持方面的主要例外是它是IE9 +

该规范也提出了一个有趣的观点:

:not()伪允许写入无用的选择。例如:not(*|*),根本不代表任何元素,或者 foo:not(bar)等同于foo但具有更高的特异性。

小小L 2020.03.17
:not([class])

实际上,这将选择没有class="css-selector"应用CSS类()的任何内容。

我做了一个jsfiddle演示

    h2 {color:#fff}
    :not([class]) {color:red;background-color:blue}
    .fake-class {color:green}
    <h2 class="fake-class">fake-class will be green</h2>
    <h2 class="">empty class SHOULD be white</h2>
    <h2>no class should be red</h2>
    <h2 class="fake-clas2s">fake-class2 SHOULD be white</h2>
    <h2 class="">empty class2 SHOULD be white</h2>
    <h2>no class2 SHOULD be red</h2>

支持吗? 是:Caniuse.com(2020年1月2日访问)

  • 支持度:98.74%
  • 部分支持:0.1%
  • 合计:98.84%

有趣的编辑,我在谷歌搜索:not的反义词。CSS否定?

selector[class]  /* the oposite of :not[]*/
JimTomL 2020.03.17

通常,您将类选择器添加到:not()伪类中,如下所示:

:not(.printable) {
    /* Styles */
}

:not([attribute]) {
    /* Styles */
}

但是,如果您需要更好的浏览器支持(IE8和更早版本不支持:not()),则最好为确实具有“可打印”类的元素创建样式规则即使您对实际标记说了什么,即使这样做还是不可行,则可能必须围绕该限制来进行标记。

请的是,根据属性你在这个规则的设定,其中一些既可以由后代继承的头脑 .printable,或以其他方式影响他们的这种或那种方式。例如,尽管display未继承,但display: none在上设置:not(.printable)会阻止它及其所有后代显示,因为它会从布局中完全删除元素及其子树。您通常可以通过使用visibility: hidden替代方法解决此问题,该方法将允许显示可见的后代,但隐藏的元素仍然会像最初那样影响布局。简而言之,请小心。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android