CSS选择没有类的第一个元素

luc*_*uca 3 html css css-selectors

我有几个<div>,其中一些有class: hidden,像这样:

<div id="firstDiv" class="hidden">content</div>
<div id="secondDiv">content</div>
Run Code Online (Sandbox Code Playgroud)

我知道要选择第一个<div>没有的class: hidden.到目前为止,我正在尝试这个CSS:div:not(.hidden):first-child但它不起作用.

如何正确编写选择器?

avr*_*ool 6

你可以使用像这样

HTML

<div id="firstDiv" class="hidden">content</div>
<div id="secondDiv">content</div> <!-- only this one will be selected -->
<div id="thirdDiv">content</div>
Run Code Online (Sandbox Code Playgroud)

CSS

div:not(.hidden)
{
    background-color: red;
}
div:not(.hidden) ~ div:not(.hidden)
{
    background-color: white; /*reset everything to normal*/
}
Run Code Online (Sandbox Code Playgroud)

  • @avrahamcool我是如此愚蠢.即使是愚蠢的简单复制和粘贴.我刚刚拼错了.我想我现在需要休息一下; _; (2认同)