CSS :only-of-type – define style of only child a type

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