Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:last-of-type — last element of its type

The :last-of-type pseudo-class selects the last element of its type among siblings, not necessarily the last child.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:last-of-type

:last-of-type matches the last element of its type among siblings. Type means the element name with its namespace taken into account, not a CSS class. The pseudo-class takes no arguments and is equivalent to :nth-last-of-type(1).

Unlike :last-child, it allows later siblings of other types. An element that is the only one of its type also matches. .note:last-of-type does not mean “the last element with class .note”.

In the example, #demo > p:last-of-type selects only Beta, although a span follows. The > operator restricts matching to direct children.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:last-of-type</title>
<style>
  #demo > p:last-of-type { color: rgb(0, 128, 0); }
</style>
<div id="demo">
  <p id="first">Alpha</p>
  <p id="last">Beta</p>
  <span>Gamma</span>
</div>
<script>
  const matches = document.querySelectorAll('#demo > p:last-of-type');
  console.log(Array.from(matches, element => element.id).join(', '));
</script>
</html>

Result

last

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources