CSS :only-of-type-Pseudoklasse
Der :only-of-type-Selektor trifft auf Elemente zu, die das einzige Kind ihres Typs sind.
p:only-of-type trifft auf einen Absatz nur zu, wenn er der einzige Absatz innerhalb seines Elternelements ist.
Die Selektoren :only-of-type und :only-child ähneln sich, mit der Ausnahme, dass der :only-child-Selektor das Element auswählt, wenn sein Elternelement keine anderen Kinder irgendeines Typs hat.
Version
Syntax
CSS :only-of-type-Syntax
css
:only-of-type {
css declarations;
}Beispiel für den :only-of-type-Selektor:
CSS :only-of-type-Codebeispiel
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:only-of-type {
background: #1c87c9;
}
</style>
</head>
<body>
<h2>:only-of-type selector example</h2>
<div>
<p>Lorem ipsum is simply dummy text.</p>
</div>
<div>
<p>Lorem ipsum is simply dummy text.</p>
<p>Lorem ipsum is simply dummy text.</p>
</div>
</body>
</html>Practice
Was stellt die Pseudoklasse ':only-of-type' in CSS dar?