CSS选取器
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.<span style="color: black;">选取</span>器的功能</p>将所<span style="color: black;">必须</span>的元素选中,<span style="color: black;">能够</span>操作这些元素的CSS样式<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.基本<span style="color: black;">选取</span>器<span style="color: black;">归类</span>(常用<span style="color: black;">选取</span>器)</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">// 语法格式
<span style="color: black;">选取</span>器{
属性1 : 值1;
属性2 : 值2;
...
}
</div>元素<span style="color: black;">选取</span>器以元素的名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器id<span style="color: black;">选取</span>器以id名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器区别class: id是身份证,<span style="color: black;">拥有</span><span style="color: black;">独一</span>性,<span style="color: black;">因此</span><span style="color: black;">不可</span><span style="color: black;">同期</span>设置多个相同的id名id名字:以“#”号开头class<span style="color: black;">选取</span>器以class名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器<span style="color: black;">能够</span>存在多个相同的class名字class名字:以“.”号开头后代<span style="color: black;">选取</span>器(多个选项用空格隔开)<span style="color: black;">便是</span><span style="color: black;">选取</span>元素内部某一种元素的所有元素:<span style="color: black;">包含</span>子元素和其他后代元素<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"father"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">span</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son1"</span><span style="color: black;">></</span><span style="color: black;">span</span><span style="color: black;">></span> //选中它 -----> .father span{} <span style="color: black;">或</span> .father .son1{}
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son2"</span><span style="color: black;">></</span><span style="color: black;">div</span><span style="color: black;">></span>// 选中它 -----> .father div{} <span style="color: black;">或</span> .father .son2{}<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
</div>群组<span style="color: black;">选取</span>器(多个选项用逗号隔开)指<span style="color: black;">同期</span>对几个<span style="color: black;">选取</span>器进行相同的操作<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">span</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son1"</span><span style="color: black;">></</span><span style="color: black;">span</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son2"</span><span style="color: black;">></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>// 选中body里面的两个标签 ----> span,div{} <span style="color: black;">或</span> .son1,.son2{}等方式
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.层次<span style="color: black;">选取</span>器<span style="color: black;">归类</span>(进阶<span style="color: black;">选取</span>器)</p>后代<span style="color: black;">选取</span>器( M N )<span style="color: black;">选取</span>M元素内部后代的N元素(所有N元素,<span style="color: black;">包含</span>子代元素或孙代元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (1)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (2)
<span style="color: black;"><</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (3)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (4)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (5)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
div p{} -----> 会选中 (1)(2)(3)(4)(5)
</div>子代<span style="color: black;">选取</span>器( M > N )<span style="color: black;">选取</span>M元素内部子代的N元素(所有第 1 级N元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (1) // <span style="color: black;">第1</span>级 p元素
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (2) // 第二季 p元素
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
div > p{} ------> 会选中 (1)
</div>兄弟<span style="color: black;">选取</span>器( M ~ N)<span style="color: black;">选取</span>M元素后的所有同级N元素(只<span style="color: black;">选取</span>后面的兄弟元素,不<span style="color: black;">选取</span>前面的兄弟元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (1)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (2)
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"brother_div"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (3)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (4)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (5)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (6)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
#brother_div ~ p {} -----> 会选中(5)(6),不会选中(1)(2)
</div>相邻<span style="color: black;">选取</span>器( M + N)(区别兄弟<span style="color: black;">选取</span>器)<span style="color: black;">选取</span>M元素相邻的下一个N元素(M、N是同级元素)常用场景:用于两个元素之间实现效果<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (1)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (2)
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"brother_div"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (3)
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>孙元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (4)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span>(5)<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span>子元素<span style="color: black;"></</span><span style="color: black;">p</span><span style="color: black;">></span> (6)
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
#brother_div + p {} ------> 选中 (5)
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.:first-letter和:first-line<span style="color: black;">选取</span>器</p>:first-letter<span style="color: black;">选取</span>器选中元素内容的“<span style="color: black;">第1</span>个字”<span style="color: black;">或</span>“<span style="color: black;">第1</span>个字母”:first-line<span style="color: black;">选取</span>器(只针对文字,不针对字母)选中元素内容的“<span style="color: black;">第1</span>行文字”
感谢楼主的分享!我学到了很多。 外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/ 一看到楼主的气势,我就觉得楼主同在社区里灌水。 论坛外链网http://www.fok120.com/
页:
[1]