CSS-選擇器11-first-of-type、last-of-type、nth-of-type 和 nth-last-of-type

CSS選擇器-系列文章

1、選擇器說明

選擇器 例子 例子描述 CSS
:first-of-type p:first-of-type 選擇屬于其父元素的首個 p 元素的每個 p 元素。 3
:last-of-type p:last-of-type 選擇屬于其父元素的最后 p 元素的每個 p 元素。 3
:only-of-type p:only-of-type 選擇屬于其父元素唯一的p 元素的每個 p 元素。 3
:nth-of-type(n) p:nth-of-type(2) 選擇屬于其父元素第二個 p 元素的每個p元素。 3
:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是從最后一個子元素開始計數(shù)。 3

2、效果演示

源代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS偽類選擇器</title>
    <style type="text/css">
        .div{
            width: 100px;
        }
        .div:first-of-type{
            color: red;
        }
        .div:last-of-type{
            color: blue;
        }
        .div:nth-of-type(3){
            border: 1px solid red;
        }
        .div:nth-last-of-type(2){
            border: 1px solid blue;
        }
    </style>
</head>
<body>
    <p class="p">p1</div>
    <div class="div">div1</div>
    <p class="p">p2</div>
    <div class="div">div2</div>
    <p class="p">p3</div>
    <div class="div">div3</div>
    <p class="p">p4</div>
    <div class="div">div4</div>
    <p class="p">p5</div>
    <div class="div">div5</div>
</body>
</html>

運行效果:


image.png

CSS選擇器-系列文章
下一節(jié) 選擇器12-:empty

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容