CSS3 selector :first-of-type with class name?

后端 未结 9 1980
梦毁少年i
梦毁少年i 2020-11-22 03:06

Is it possible to use the CSS3 selector :first-of-type to select the first element with a given class name? I haven\'t been successful with my test so I\'m thin

9条回答
  •  既然无缘
    2020-11-22 03:36

    This is an old thread, but I'm responding because it still appears high in the list of search results. Now that the future has arrived, you can use the :nth-child pseudo-selector.

    p:nth-child(1) { color: blue; }
    p.myclass1:nth-child(1) { color: red; }
    p.myclass2:nth-child(1) { color: green; }
    

    The :nth-child pseudo-selector is powerful - the parentheses accept formulas as well as numbers.

    More here: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

提交回复
热议问题