前端之CSS——CSS选择器

本周学习CSS的选择器。

工作中最常使用的就是元素选择器,ID选择器,类选择器,后代选择器。

还有一些并不常用但是功能强大的选择器容易被忽视,这次学习总结下。

1.后代选择器和子代选择器

后代选择器:element1 element2, 查找是element1后代的所有element2

子代选择器:element1>element2, 查找是element1子代的element2

2.伪类选择器:first-child和:first-of-tye

first-child:element:first-child,查找element,该element是其父的第一个子元素

first-of-tye:element:first-of-tye,查找element,该element是其父的第一个该类型子元素

3.伪类选择器:last-child和:last-of-tye

和2正好相反

4.伪类选择器:nth-child(n)和:nth-of-type(n)

2的升级版,更加灵活

nth-child(n):element:nth-child(n),查找element,该element是其父的第n个子元素

nth-of-type(n):element:nth-of-type(n),查找element,该element是其父的第n个该类型子元素

5.伪类选择器:nth-last-child(n)和:nth-last-of-type(n)

3的升级版

猜你喜欢

转载自www.cnblogs.com/chechedan/p/9005113.html