2017-04-25 4 views
0

Wie kann ich Stil :nth-child(4n) auch mit dem Element ist in einem div verschachtelt, Beispiel hier ist p innen div:CSS-Stil: n-te-Kind sogar verschachtelte Elemente

p:nth-child(2) { 
 
    background: red; 
 
}
<p>The first paragraph.</p> 
 
<div> 
 
    <p>The second paragraph.</p> 
 
</div> 
 
<p>The third paragraph.</p> 
 
<p>The fourth paragraph.</p>

+0

In diesem Fall würden Sie 'n-of-Baumuster zur müssen. – Abhitalks

+0

@abhitalks wenn ich p: n-Typ (2) { Hintergrund: rot; } Ich habe das dritte p in rot –

+1

Hoppla! Ich habe das Markup in Eile falsch gelesen. Diese Antwort könnte Ihnen helfen - http://stackoverflow.com/a/23987180/1355315 – Abhitalks

Antwort

0

.content *:nth-child(2) { 
 
     background: red; 
 
    }
<!-- begin snippet: js hide: false console: true babel: false --> 
 
<div class="content"> 
 
    <p>The first paragraph.</p> 
 
    <div> 
 
     <p>The second paragraph.</p> 
 
    </div> 
 
    <p>The third paragraph.</p> 
 
    <p>The fourth paragraph.</p> 
 

 
    <p><b>Note:</b> Internet Explorer 8 and earlier versions do not support the :nth-child() selector.</p> 
 
</div>

0

body p:nth-child(2n) { 
 
    background: red; 
 
} 
 
body div:nth-child(2n) p { 
 
    background: red; 
 
}
<body> 
 
<p>The first paragraph.</p> 
 
<div> 
 
    <p>The second paragraph.</p> 
 
</div> 
 
<p>The third paragraph.</p> 
 
<p>The fourth paragraph.</p> 
 
<body>

Verwandte Themen