div:first-child 表示第一個div,代碼以下html
<html> <head> <title></title> </head> <style> *{ margin:0; padding:0; } .show>div{ width:100px; height:100px; } .show>div:first-child{ color:red; } </style> <body> <div class="show"> <div>第一個div</div> <div>第二個div</div> <div>第三個div</div> <div>第四個div</div> </div> </body> </html>
div:last-child 表示最後一個divide
<html> <head> <title></title> </head> <style> *{ margin:0; padding:0; } .show>div{ width:100px; height:100px; } .show>div:last-child{ color:red; } </style> <body> <div class="show"> <div>第一個div</div> <div>第二個div</div> <div>第三個div</div> <div>第四個div</div> </div> </body> </html>
div:nth-child(n),n是表明第幾個divspa
<html> <head> <title></title> </head> <style> *{ margin:0; padding:0; } .show>div{ width:100px; height:100px; } .show>div:nth-child(3){ color:red; } </style> <body> <div class="show"> <div>第一個div</div> <div>第二個div</div> <div>第三個div</div> <div>第四個div</div> </div> </body> </html>
div:nth-child(2n),表示偶數個divcode
<html> <head> <title></title> </head> <style> *{ margin:0; padding:0; } .show>div{ width:100px; height:100px; } .show>div:nth-child(2n){ color:red; } </style> <body> <div class="show"> <div>第一個div</div> <div>第二個div</div> <div>第三個div</div> <div>第四個div</div> </div> </body> </html>