:first-child和:first-of-type的作用和区别

:first-child(nth-child(1)) 匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。
:first-of-type(nth-of-type(1)) 匹配父元素下使用同种标签的第一个子元素

例如:


     

我是p1

我是h1

我是p2

上述代码中p:first-child和p:first-of-type均选中

我是p1


     

我是h1

我是p1

我是p2

上述代码中p:first-of-type能选中

我是p1

但p:first-child无效(因为不存在第一个子元素的p)

你可能感兴趣的:(:first-child和:first-of-type的作用和区别)