1、ID和类选择器
相对于上下文选择器而言,利用它们可以不用考虑文档的层次结构。只要你在html标记中为元素添加了id和class属性,就可以在css选择器中使用ID和类名,直接选中文档中特定的区域
可以给id和class属性设定任何值,但不能以数字或者特殊符号开头。
1.1 类属性
类属性就是HTML元素的class属性,body标签中包含的任何HTML元素都可以添加这个属性
1 <html> 2 <head> 3 <meta charset="utf-8" /> 4 <title>HTML5 Template</title> 5 <style> 6 p {font-family:helvetica,sans-serif;font-size:1.2em;} 7 .specialtext {font-style:italic;} 8 p.specialtext {color:red;} 9 p.specialtext span {font-weight:bold;} 10 </style> 11 </head> 12 <body> 13 <h1 class="specialtext">this is a heading with the <span>same class</span>as the second paragraph.</h1> 14 <p>this tag has no class.</p> 15 <p class="specialtext">when a tag has a class attribute,you can target it <span>regardless</span> of its position in the hierarchy.</p> 16 </body> 17 </html>