0

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>
It is invalid after selection, but it will be valid after adding a space before the: not selector. This is the reason?

        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。

lcrazyl
  • 3
  • 3

1 Answers1

-1

Main is just parent element you can directly use the select tag as a selector and apply not and nth-of-type

this article will give you information about :nth-child() vs :nth-of-child()

if you found this will help you then please give me like

main section:not(:nth-of-type(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>