美文网首页
:first-child和:first-of-type的作用和区

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

作者: Maggie_77 | 来源:发表于2016-11-28 11:39 被阅读0次

: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)

相关文章

网友评论

      本文标题::first-child和:first-of-type的作用和区

      本文链接:https://www.haomeiwen.com/subject/llkopttx.html