:nth-last-of-type()-倒数第n个同级兄弟元素-css结构性伪类选择器 ypxx.net

:nth-last-of-type()

版本:CSS3

:nth-last-of-type(an+b)这个 CSS 伪类,匹配那些在它之后有an+b-1个相同类型兄弟节点的元素,其中n为正值或零值。它基本上和:nth-of-type()一样,只是它从结尾处反序计数,而不是从开头处。

语法

E:nth-last-of-type(n){sRules}

匹配同类型中的倒数第n个同级兄弟元素E。

  • E元素是某个元素的子元素,E的父元素最高是,即E可以是的子元素,也就是说E可以是
  • 该选择符总是能命中父元素的倒数第n个为E的子元素,不论倒数第n个子元素是否为E

浏览器支持

IE9+以及新版浏览器都支持:nth-last-of-type()

例子

p:nth-last-of-type(2){background:pink;}

元素1

元素2

元素3

.demo1 p:nth-last-of-type(2){background:pink;}

元素1

元素2

元素3