How to exclude particular class name in CSS selector?

后端 未结 3 1313
挽巷
挽巷 2020-11-29 21:02

I\'m trying to apply background-color when a user mouse hover the element whose class name is \"reMode_hover\".

But I do not want to change color if th

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 22:00

    Method 1

    The problem with your code is that you are selecting the .remode_hover that is a descendant of .remode_selected. So the first part of getting your code to work correctly is by removing that space

    .reMode_selected.reMode_hover:hover
    

    Then, in order to get the style to not work, you have to override the style set by the :hover. In other words, you need to counter the background-color property. So the final code will be

    .reMode_selected.reMode_hover:hover {
      background-color:inherit;
    }
    .reMode_hover:hover {
      background-color: #f0ac00;
    }
    

    Fiddle

    Method 2

    An alternative method would be to use :not(), as stated by others. This will return any element that doesn't have the class or property stated inside the parenthesis. In this case, you would put .remode_selected in there. This will target all elements that don't have a class of .remode_selected

    Fiddle

    However, I would not recommend this method, because of the fact that it was introduced in CSS3, so browser support is not ideal.

    Method 3

    A third method would be to use jQuery. You can target the .not() selector, which would be similar to using :not() in CSS, but with much better browser support

    Fiddle

提交回复
热议问题