1. 程式人生 > >[CSS3] :nth-child的用法

[CSS3] :nth-child的用法

css 標簽 span 整數 pre ast css3 一個 tty

:nth-child(2)選取第幾個標簽,“2可以是你想要的數字”

.demo01 li:nth-child(2){background:#090}

:nth-child(n+4)選取大於等於4標簽,“n”表示從整數,下同

.demo01 li:nth-child(n+4){background:#090}

:nth-child(-n+4)選取小於等於4標簽

.demo01 li:nth-child(-n+4){background:#090}

:nth-child(2n)選取偶數標簽,2n也可以是even

.demo01 li:nth-child(2n){background:#090}

:nth-child(2n-1)選取奇數標簽,2n-1可以是odd

.demo01 li:nth-child(2n-1){background:#090}

:nth-child(3n+1)自定義選取標簽,3n+1表示“隔二取一”

.demo01 li:nth-child(3n+1){background:#090}

:last-child選取最後一個標簽

.demo01 li:last-child{background:#090}

:nth-last-child(3)選取倒數第幾個標簽,3表示選取第3個

.demo01 li:nth-last-child(3){background:#090}

:nth-child的這些用法在實際中很用得著,不用單獨給需要選取的標簽加上ID或Class
 
 
 

[CSS3] :nth-child的用法