2017-04-25 143 views
0

我如何風格:nth-child(4n)甚至與元素嵌套一個div內,這裏的例子是pdivCSS樣式:第n個孩子甚至嵌套元素

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

在這種情況下,你需要用'第n-的-type'。 – Abhitalks

+0

@Abhitalks當我做p:n型(2){ background:red; }我有第三個紅色的 –

+1

哎呀!我在匆忙中誤讀了標記。這個答案可能會幫助你 - http://stackoverflow.com/a/23987180/1355315 – Abhitalks

回答

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>