:nth-child(n) 選擇器匹配父元素中的第 n 個子元素,元素類型沒有限制。n 可以是一個數(shù)字,一個關鍵字,或者一個公式,默認為0。
常見的寫法如下:
ul li:nth-child(odd){background-color:green;}/設置單行樣式/
ul li:nth-child(even){background-color:gray}/設置雙行樣式/
ul li:nth-child(3n){background-color:green;}/* 3,6,9,12… */
ul li:nth-child(4){background-color:green;}/單獨設置第四個li/
ul li:nth-child(3n+2){background-color:green;}/匹配2,5,8,11…/
ul li:nth-child(n+6){background-color:green;}/從第六個li開始匹配/
ul li:nth-child(-n+6){background-color:green;}/匹配1-6的元素/
ul li:nth-child(n+3):nth-child(-n+5){background-color:green;}/匹配3-5的元素/
ul li:nth-child(3n-1){background-color:green;}/匹配(3-1),(6-1),(9-1)…/
n 可以是一個數(shù)字,一個關鍵字,或者一個公式,單獨寫n的時候,你必須從1開始,如nth-child(3);用在表達式中,n代表的是從0開始的整數(shù),如nth-child(3n+2){font-size:12px;},匹配的是第2,5,8,11…個元素
留言反饋