:nth-child(n) 選擇器匹配屬于其父元素的第 N 個(gè)子元素,不論元素的類型。
n 可以是數(shù)字、關(guān)鍵詞或公式。
CSS3中有一個(gè)神奇的玩意,可以輕松選取你想要的標(biāo)簽并給與修改添加樣式,是不是很給力,它就是“:nth-child”。
下面我將用幾個(gè)典型的實(shí)例來給大家講解:nth-child的實(shí)際用途:
Tips:還用低版本的IE瀏覽器的哥們請繞過!
.demo01 li:nth-child(2){background:#090}
.demo01 li:nth-child(n+4){background:#090}
.demo01 li:nth-child(-n+4){background:#090}
.demo01 li:nth-child(2n){background:#090}
.demo01 li:nth-child(2n-1){background:#090}
.demo01 li:nth-child(3n+1){background:#090}
.demo01 li:last-child{background:#090}
.demo01 li:nth-last-child(3){background:#090}
ok,:nth-child的這些用法在實(shí)際中很用得著,不用單獨(dú)給需要選取的標(biāo)簽加上ID或Class,是不是很拉風(fēng)!
上一篇:學(xué)好js,這些js函數(shù)概念一定要知道
下一篇:HTML5語義化總結(jié)
掃一掃 加微信咨詢