Css :only-child pseudo class represents an element that has a parent element and whose parent element has no other element children.
Example – css :only-child
Change background color of p tag if it is only child of its parent.
<style type="text/css" media="screen">
p:only-child {
background-color:lightgreen;
}
</style>
<div>
<p>First div child p1</p>
</div>
<hr>
<div>
<div>2nd div child div1</div>
<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-of-type - define style of only child a type