1. 程式人生 > >CSS的nth-of-type和nth-child的區別

CSS的nth-of-type和nth-child的區別

alt ctype nbsp str lan eight n+1 width lang

<!--源代碼-->
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> div{ width: 100px; height: 100px; background: #ff11ff; } p{ width: 100px; height
: 100px; background: blue; } /*div:nth-child(2n+1){*/ /*background: red;*/ /*}*/ div:nth-of-type(2n+1){ background: red; } </style> </head> <body> <div></div> <div></div> <p></p> <
div></div> <div></div> </body> </html>

使用nth-child的效果圖:                        使用nth-of-type:效果

技術分享圖片 技術分享圖片

從上面可以看到:nth-child是從父元素的第一個子元素算起走,會算其他標簽元素,而nth-of-type只會計算父元素下本標簽元素

CSS的nth-of-type和nth-child的區別