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

2016-11-28  本文已影响0人  Maggie_77

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

例如:

<body>
     <p>我是p1</p> 
     <h1>我是h1</h1>
     <p>我是p2</p>
</body>

上述代码中p:first-child和p:first-of-type均选中<p>我是p1</p>

<body>
     <h1>我是h1</h1>
     <p>我是p1</p> 
    <p>我是p2</p>
</body>

上述代码中p:first-of-type能选中<p>我是p1</p>但p:first-child无效(因为不存在第一个子元素的p)

上一篇 下一篇

猜你喜欢

热点阅读