nth-child doesn't respond to class selector

后端 未结 3 793
生来不讨喜
生来不讨喜 2020-12-01 09:04

Unless it\'s not supposed to but I can\'t seem to get nth-child to acknowledge the class selector.

I have say 4 divs inside another div, all of various c

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 09:43

    Try the :nth-of-type() pseudo-selector instead:

    #content .foo:nth-of-type(1) { margin-top: 0; }
    

    Note that :nth-of-type() counts the elements with the same name. So .foo:nth-of-type(1) will not select the first element with the class foo but any first element that is the first in the list of elements grouped by the same name. If you have some document like this:

    1x2 3x4

    .foo:nth-of-type(1) will select the elements 1 and 3 as both are the first of its own type.

提交回复
热议问题