How to select the first, second, or third element with a given class name?

后端 未结 7 2242

How can I select a certain element in a list of elements? I have the following:

my text
7条回答
  •  被撕碎了的回忆
    2020-11-27 15:06

    Perhaps using the "~" selector of CSS?

    .myclass {
        background: red;
    }
    
    .myclass~.myclass {
        background: yellow;
    }
    
    .myclass~.myclass~.myclass {
        background: green;
    }
    

    See my example on jsfiddle

提交回复
热议问题