Recently, when I was using the CSS:not selector, I found a problem with spaces. Maybe I have a problem with the selector's understanding, please help me to answer。 I want to select all elements except the seventh element in the main element。
main:not(section:nth-child(7)){
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
<main>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>seven</section>
<section>1</section>
<section>1</section>
<section>1</section>
</main>
main :not(section:nth-child(7)){
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
<main>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>seven</section>
<section>1</section>
<section>1</section>
<section>1</section>
</main>
And I tried to select with sibling elements, but also found the space problem. Contrary to the above, adding spaces is invalid, deleting spaces is effective.
section :not(section:nth-child(7)){
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
<main>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>seven</section>
<section>1</section>
<section>1</section>
<section>1</section>
</main>
The above code is invalid after adding a space before the not selector。
section:not(section:nth-child(7)){
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
<main>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>1</section>
<section>seven</section>
<section>1</section>
<section>1</section>
<section>1</section>
</main>
Delete: the space in front of the not selector will be effective。