Home > UI/UX Development > CSS > CSS: only-child, only-of-type selector with Example

CSS: only-child, only-of-type selector with Example

This post was most recently updated on December 28th, 2016

CSS selectors are patterns or specific elements you want to select and apply styles to it. Here are 2 not most commonly used but useful selectors.

There have been many scenarios where you want to check

  1. if an element is the only child of a parent

    Click here for demo. only-child selector would not allow other element of any type under parent div. So it would strictly search for single child p elements to apply the style.

  2. if an element is only child of a parent of its type

    Click here for demo. only-of-type would only allow elements of other type under parent div. So it would search elements that do not have duplicate p elements in a parent div.

     

Browser Compatibility:

  only-child only-of-type
chrome 4.0 4.0
ie 9.0 9.0
firefox 3.5 3.5
compatible_safari 3.2 3.2

 

This Article is TAGGED in , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">