2014-04-01 53 views
0

如何僅選擇多個與CSS具有相同名稱的div中的第一個?例如,我有這樣的:如何僅選擇名稱相同的第一個div?

<div class="div">dsdas</div> 
<div class="div">123</div> 
<div class="div">73</div> 
<div class="div">63</div> 
<div class="div">53</div> 
<div class="div">45</div> 

如何只選擇第一個div(類「div」)?

+0

看看這個,還有顯示如何在CSS解決它。 http://stackoverflow.com/a/6447072/2529682 –

回答

1

使用僞選擇符:nth-​​of-type()。

.div:nth-of-type(1) { 

} 

Working DEMO

+0

謝謝。它應該是這樣的.div:nth-​​of-type(1){marginwidth-left:0px; } – user3442381

+0

這將選擇第一個div,而不是第一個div,它是特定類的成員。 – Quentin

+0

重新編輯:':nth-​​type-type'總是基於** type **進行選擇,它不會選擇與選擇器的前一部分匹配的第一個東西。 – Quentin

0

使用此

.div:nth-of-type(1) 
{ 
    //add style here 
} 

入住這fiddle

+0

這將選擇第一個div如果它是類的成員,而不是第一個div是特定類的成員。 – Quentin

相關問題