Selects a specified child element if it is the only child element of its type under the parent element.
Example:
p:only-of-type {
color: red;
}
Above example sets text color (red) to the P child element based on its type when P type child element count is one under the parent element.
Example:
<!DOCTYPE html>
<html>
<head>
<title> CSS pseudo-class selector - :only-of-type</title>
<style>
p:only-of-type {
color: red;
}
div {
border: 1px solid red;
padding: 20px;
width: 500px;
}
</style>
</head>
<body>
<div id="div1">
<p>1) P child element of the Parent element (DIV id="div1")</p>
</div>
<div id="div2">
<p> 1) P child element of the Parent element (DIV id="div2")</p>
<span>2) SPAN child element of the Parent element (DIV id="div2")</span>
</div>
<div id="div3">
<span>1) SPAN child element of the Parent element (DIV id="div3")</span>
</div>
<div id="div4">
<p> 1) P child element of the Parent element (DIV id="div4")</p>
<p> 2) P child element of the Parent element (DIV id="div4")</p>
</div>
</body>
</html>
Result:
In the above example,
Using following Document Object Model (DOM) tree, above example is explained.