CSS :only-child – define style if element if only child

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>
refresh

Related

Specification

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments