last-of-type選擇器
“:last-of-type”選擇器和“:first-of-type”選擇器功能是一樣的,不同的是他選擇是父元素下的某個類型的最后一個子元素。
示例演示
通過“:last-of-type”選擇器,將容器“div.wrapper”中最后一個段落元素背景設置為橙色
(提示:這個段落不是“div.wrapper”容器的最后一個子元素)。
HTML代碼:
<div class="wrapper"> <p>我是第一個段落</p> <p>我是第二個段落</p> <p>我是第三個段落</p> <div>我是第一個Div元素</div> <div>我是第二個Div元素</div> <div>我是第三個Div元素</div> </div>
CSS代碼:
.wrapper > p:last-of-type{ background: orange; }
演示結果:

nth-last-of-type(n)選擇器
“:nth-last-of-type(n)”選擇器和“:nth-of-type(n)”選擇器是一樣的,選擇父元素中指定的某種子元素類型,但它的起始方向是從最后一個子元素開始,而且它的使用方法類似於上節中介紹的“:nth-last-child(n)”選擇器一樣。
示例演示
通過“:nth-last-of-type(n)”選擇器將容器“div.wrapper”中的倒數第三個段落背景設置為橙色。
HTML代碼:
<div class="wrapper"> <p>我是第一個段落</p> <p>我是第二個段落</p> <p>我是第三個段落</p> <p>我是第四個段落</p> <p>我是第五個段落</p> <div>我是一個Div元素</div> <p>我是第六個段落</p> <p>我是第七個段落</p> </div>
CSS代碼:
.wrapper > p:nth-last-of-type(3){ background: orange; }
演示結果:

