您好,欢迎访问一九零五行业门户网

【自学CSS碰到的“坑”】谁才是first-child_html/css_WEB-ITnose

看过css伪类选择器之后,心想也就如此嘛,:first-child选择元素的第一个子元素,有什么难的,可一到实践中,还是到处碰壁啊。
1 2 3 apple 4 orange 5 pear 6 grape 7 8 9 i am learning css.
10 i want to be a programmer.
11
12
设置ul的第一个子元素的背景颜色,我想当然地使用了 ul:first-child{backgroud-color:#ccc;},结果发现整个ul元素都被选中了!
再回头看手册“:first-child选择器用于选取属于其父元素的首个子元素的指定选择器”,针对上述代码,也就是说应该设置成这样:
li:first-child{backgroud-color:#ccc;}或者.fruit>:first-child{backgroud-color:#ccc;}
同理,若想选择
的第一个元素,应该设置:
p:first-child{backgroud-color:#789;}或者.content>:first-child{backgroud-color:#789;}
其它类似信息

推荐信息