E:nth-last-child(n) { sRules }
HTML示例代碼:
第1個(gè)p
第2個(gè)p
第1個(gè)span
第3個(gè)p
第2個(gè)span
如上HTML,假設(shè)要命中倒數(shù)第一個(gè)p(即正數(shù)第3個(gè)p),那么CSS選擇符應(yīng)該是:
p:nth-last-child(2){color:#f00;}
而不是:
p:nth-last-child(1){color:#f00;}
因?yàn)榈箶?shù)第1個(gè)p,其實(shí)是倒數(shù)第2個(gè)子元素。基于選擇符從右到左解析,首先要找到第1個(gè)子元素,然后再去檢查該子元素是否為p,如果不是p,則n遞增,繼續(xù)查找
假設(shè)不確定倒數(shù)第1個(gè)子元素是否為E,但是又想命中倒數(shù)第1個(gè)E,應(yīng)該這樣寫(xiě):
p:last-of-type{color:#f00;}
或者這樣寫(xiě):
p:nth-last-of-type(1){color:#f00;}
IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|
6.0-8.0 | 2.0+ | 4.0+ | 3.1+ | 3.5+ | 3.2+ | 2.1+ | 18.0+ |
IE9.0+ |