:nth-last-of-type()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

:nth-last-of-type() CSS 偽類 根據元素在其同類型(標籤名)兄弟元素中的位置進行匹配,從末尾開始計數。

試一試

dt {
  font-weight: bold;
}

dd {
  margin: 3px;
}

dd:nth-last-of-type(3n) {
  border: 2px solid orange;
}
<dl>
  <dt>Vegetables:</dt>
  <dd>1. Tomatoes</dd>
  <dd>2. Cucumbers</dd>
  <dd>3. Mushrooms</dd>
  <dt>Fruits:</dt>
  <dd>4. Apples</dd>
  <dd>5. Mangos</dd>
  <dd>6. Pears</dd>
  <dd>7. Oranges</dd>
</dl>

語法

css
:nth-last-of-type(<An+B> | even | odd) {
  /* ... */
}

引數

:nth-last-of-type() 偽類透過一個引數指定,該引數表示從末尾開始匹配元素的模式。

有關其語法的更詳細解釋,請參閱:nth-last-child

示例

HTML

html
<div>
  <span>This is a span.</span>
  <span>This is another span.</span>
  <em>This is emphasized.</em>
  <span>Wow, this span gets limed!!!</span>
  <del>This is struck through.</del>
  <span>Here is one last span.</span>
</div>

CSS

css
span:nth-last-of-type(2) {
  background-color: lime;
}

結果

規範

規範
選擇器 Level 4
# nth-last-of-type-pseudo

瀏覽器相容性

另見