Css :only-of-type pseudo class represents an element that has a parent element and whose parent element has no other element children of same type.
Example – css :only-of-type
Change background color of p tag if it is only child of its parent of type p. Parent can have other type children.
<style type="text/css" media="screen">
p:only-of-type {
background-color:lightgreen;
}
</style>
<div>
<div>First div child div1</div>
<p>First div child p1</p>
</div>
<hr>
<div>
<p>2nd div child p1</p>
<p>2nd div child p2</p>
</div>Related
- Css :nth-child
- CSS :nth-of-type
- Css :nth-child vs :nth-of-type
- CSS :nth-last-child
- Css :first-child selector - first child element
- Css :first-of-type selector - first child element of type
- CSS :nth-last-of-type
- Css :last-child selector - last child element
- Css :last-of-type selector - last child element of type
- CSS :only-child - define style if element if only child