
CSS中的first-of-type伪类选择器可以用来选中同类型元素中的第一个元素并为其设置样式。这个选择器可以用于多个标签元素,例如p、div、span等等。
下面是一个具体的示例代码:
HTML代码:
<div class="container"> <h1>标题1</h1> <p>第一段文字</p> <p>第二段文字</p> <h2>标题2</h2> <p>第三段文字</p> <p>第四段文字</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/js/7343" title="html点击切换广告画报特效"><img src="https://img.php.cn/upload/jscode/000/287/557/63e5be8a571a7343.png" alt="html点击切换广告画报特效" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/js/7343" title="html点击切换广告画报特效" class="overflowclass">html点击切换广告画报特效</a> <p class="overflowclass">一款html点击切换广告画报特效</p> </div> <a rel="nofollow" href="/xiazai/js/7343" title="html点击切换广告画报特效" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div> </div>
CSS代码:
p:first-of-type {
font-weight: bold;
}
在上面的代码中,我们选择了第一个p标签元素并将其设置为粗体字。结果在浏览器中会显示出来的效果是,第一段文字会被设置为粗体字。
同样的,我们也可以使用first-of-type选择器来选择其他标签元素的第一个。例如:
h1:first-of-type {
font-size: 24px;
}
这里我们选择了第一个h1标签元素并将其字体的大小设置为24像素。
需要注意的是,使用first-of-type选择器仅能选择同类型元素的第一个元素。如果我们要选择不同类型元素中的第一个元素,可以使用:first-child。
总之,使用first-of-type伪类选择器能够非常方便地为同类型元素中的第一个元素设置特定的样式。










