I need to exclude the first child from a display none rule using :not(:first-child)
- see example below:
.child-div {
background: green;
width: 30px;
height: 30px;
margin: 1rem;
}
.whatever {
background: red;
width: 30px;
height: 30px;
margin: 1rem;
}
.child-div:not(:first-child) {
display: none;
}
<div class="parent-div">
<div class="whatever"></div>
<div class="child-div"></div>
<div class="child-div"></div>
<div class="child-div"></div>
<div class="child-div"></div>
</div>
I was expecting ONE green square but that is not the case. All are hidden.
From this: css selector :first-child
The first-child applies to the li. It should select lis that are first children under any div.
From this: CSS selector for first element with class
the :first-child pseudo-class represents the very first child of its parent. That's it.
child-div
are the only children in that parent div.
From this: not:first-child selector
If you need to support legacy browsers, or if you are hindered by the :not selector's limitation (it only accepts a simple selector as an argument) then you can use another technique:
MDN definition of a "simple selector":
A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
I am using a class:
<div class="child-div"></div>
.child-div:not(:first-child) {
display: none;
}
Howcome I can't select everything except the first child in my example?
I also tried:
first-of-type
- but had no luck