How to skip first child?

后端 未结 6 854
误落风尘
误落风尘 2020-12-03 02:30

one

two

three

6条回答
  •  误落风尘
    2020-12-03 03:10

    Quentin's :not() solution works great for modern browsers:

    p:not(:first-child) { color: red; }
    

    His alternative for older browsers also works well, except it makes use of an overriding rule for the first child. It's not required, however...

    You can simply use a sibling selector to apply the same rule as the one above, without the need to override it for p:first-child. Either one of these rules will work:

    • The adjacent sibling selector, which matches any p that comes directly after a p:

      p + p { color: red; }
      
    • The general sibling selector, which matches any p that comes anywhere after a p:

      p ~ p { color: red; }
      

    Both combinators work identically here; the subtle differences between them only apply when you have other elements in the mix. Refer to the links provided for details.

提交回复
热议问题