CSS选择器:last-child与:last-of-type的区别
:last-child----represents the last element among a group of sibling elements. CSS伪类 :last-child 代表在⼀兄弟元素中的最后⼀个元素。
从代码和图可以看出:last-child选择了最后⼀个li标签。
css兄弟选择器同时,我们换另外⼀段代码,看看是否还有这样的效果。
从代码和图可以看出,:last-child并没有起到我们想要的作⽤。如果,这个时候去掉最后的div标签,再看看效果。
这时,效果出来了,那么,可以总结了。
:last-child表⽰其⽗元素的最后⼀个⼦元素,且这个元素是css指定的元素,才可以⽣效。
:last-of-type
关于:last-of-type⼿册中是这么解释的:
The :last-of-type CSS pseudo-class represents the last element of its type among a group of sibling elem
ents.
CSS伪类:last-of-type代表在⼀兄弟元素中的最后⼀个指定类型的元素。
直接⽤上⾯两个栗⼦。
没有⼲扰元素,OK。
有⼲扰元素,OK。
:last-of-type表⽰其⽗元素下的最后⼀个指定类型的元素。
结尾
总结了⼀番,对于什么场景下使⽤:last-child和:last-of-type⼜了相应的认识,以后犯错的可能性就会降低了很多。