Skip to main content
Inspiring
March 8, 2023
Pregunta

Need help targeting all instances of a class EXCEPT when it's a child of another (specific) one

  • March 8, 2023
  • 3 respuestas
  • 720 visualizaciones

Hi,

 

How do I target all instances of .class1 EXCEPT when it's a child of .class2? These instances would be protected from those CSS change, so only instances of .class1 that AREN'T children of .class2 would be affected.

 

Is there an easy syntax for this?

    Este tema ha sido cerrado para respuestas.

    3 respuestas

    Nancy OShea
    Community Expert
    Community Expert
    March 8, 2023

    You'll need to provide an example of what you mean because I can't think of one.

     

    Tags containing more than one class are expressed in a single string like this Bootstrap example.

    <nav id="topNav" class="navbar navbar-top navbar-expand-md navbar-dark bg-dark">

    </nav>

     

    In the above example, the tag <nav> containing unique ID "topNav" is the parent of ALL classes in the string.

     

    Nancy O'Shea— Product User & Community Expert
    Community Expert
    March 8, 2023

    Here at the studio we work quite a bit with GPT, so here is his answer :

     

    The following CSS rule selects all elements of class .class1 that are not children of class .class2 :

    .class1:not(.class2 > .class1) {
      /* Your styles here */
    }

    But that supose that .class1 are first child of .class2.

     

    So, I asked GPT : "I thought that the pseudo-class :not could not contain a combination"

     

    GPT get back :

    You are right that the :not() pseudo-class cannot contain selector combinations, such as descendant, child, or sibling selectors. Therefore, if the elements of .classe1 are not direct children but nested one level deeper, the selector combination would not work with the :not() pseudo-class.

     

    In this case, you can use another approach by combining selectors to select only the elements of .classe1 that are not descendants of .classe2.

    Here is an example of a CSS rule that could work:

    .classe1 {
      /* Your styles for all elements with .classe1 here */
    }
    .classe2 .classe1 {
      /* Your styles for all elements with .classe1 that are descendants of .classe2 here */
    }
    .classe1:not(.classe2) {
      /* Your styles for all elements with .classe1 that are not descendants of .classe2 here */
    }

    In this approach, we have defined two different sets of styles for the elements of .classe1:

    • The base styles that apply to all elements with .classe1.
    • The styles for elements with .classe1 that are descendants of .classe2.

     

    Then, we have used the :not() pseudo-class to select all elements of .classe1 that are not descendants of .classe2, excluding the elements of .classe1 that are descendants of .classe2.

    This way, the styles defined in the last rule will only be applied to elements of .classe1 that are not descendants of .classe2.

    B i r n o u
    Legend
    March 8, 2023

    I always like the way GPT answers and detailled all his proposal.
    I can't wait to use it again in VSC
    lol

    Legend
    March 8, 2023
    quote

    I always like the way GPT answers and detailled all his proposal.
    I can't wait to use it again in VSC
    lol


    By @B i r n o u

     

    Is it actually correct though. I cant make out what it means. I've tried a few html combinations and its seems as though IF an element with the class name of class1 is inside a parent element with a class name of class2 the :NOT will work, regardless what else is in the parent and where it is.

    Legend
    March 8, 2023

    This should work:

    .class1:not(.class2 .class1) {

    color: red;

    }

     

    Global browser support is 93%.