Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:only-of-type — the only element of its type

The :only-of-type pseudo-class selects an element with no siblings of the same type. Siblings of other types do not prevent a match; classes and IDs do not determine type.

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

Syntax

E:only-of-type { /* declarations */ }

E:only-of-type selects an element of type E with no siblings of the same type, meaning the same local name and namespace. In ordinary HTML, this means the tag name. The pseudo-class takes no arguments.

Unlike :only-child, it allows siblings of other types. Text and comments do not affect matching. Classes do not change type: .note:only-of-type does not mean “the only element with class .note”.

It matches the same elements as :first-of-type:last-of-type, but :only-of-type alone has specificity (0,1,0), whereas that pair has (0,2,0). In the example, only #solo matches, despite its sibling h2.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:only-of-type example</title>
<style>
  p:only-of-type { border: 2px solid green; }
</style>
<section>
  <h2>One paragraph</h2>
  <p id="solo">Alpha</p>
</section>
<section>
  <p id="first">Beta</p>
  <p id="second">Gamma</p>
</section>
<pre id="result"></pre>
<script>
  const ids = Array.from(document.querySelectorAll('p:only-of-type'), p => p.id);
  document.getElementById('result').textContent = ids.join(', ');
</script>
</html>

Result

solo

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