How do I select an element that has a certain class?

后端 未结 5 1341
清歌不尽
清歌不尽 2020-12-04 15:14

My understanding is that using element.class should allow for a specific element assigned to a class to receive different \"styling\" than the rest of the class

5条回答
  •  一个人的身影
    2020-12-04 15:55

    It should be this way:

    h2.myClass looks for h2 with class myClass. But you actually want to apply style for h2 inside .myClass so you can use descendant selector .myClass h2.

    h2 {
        color: red;
    }
    
    .myClass {
        color: green;
    }
    
    .myClass h2 {
        color: blue;
    }
    

    Demo

    This ref will give you some basic idea about the selectors and have a look at descendant selectors

提交回复
热议问题