<div id="nchildChk">
<p>I am p1</p>
<span>I am a span</span>
<p>I am p2</p>
<p>I am p3</p>
<p>I am p4</p>
<div>
--------------------------------
<div id="nType">
<p>I am p1</p>
<span>I am a span</span>
<p>I am p2</p>
<p>I am p3</p>
<p>I am p4</p>
</div>
CSS
/*This wont work as the second element of the parent is a span
But if we remove the span element the p element is again going to be the
second element*/
/*The '#nchildChk' makes sure only the div with with that name is is styled applied*/
#nchildChk p:nth-child(2){
color:red;
}
/*This works as it find the second element of type p in the parent.
More Strict than nth-child*/
#nType p:nth-of-type(2){
color:red;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.