CSS :not() selector. Apply style if parent does not exist

后端 未结 2 1641
小鲜肉
小鲜肉 2021-01-04 19:51

I am trying to apply a style to a div based on its parent class. I am using the :not() selector to select the div whose parent is not .container1, the second di

2条回答
  •  独厮守ぢ
    2021-01-04 20:08

    You're selecting wrong elements. No reverse lookups possible, see here:

    div:not(.container1) > .myDiv {
      color: red;
    }
    Div 1
    Div 2


    Ideally, you'd group those parent divs under the same class in order to avoid the super-generic div selector:

    .container:not(.container1) > .myDiv {
      color: red;
    }
    Div 1
    Div 2

提交回复
热议问题