选择器
1. 子选择器 >
只选择直接的子元素(即第一代后代,儿子),即嵌套层次为1的,嵌套更深的则忽略(第二代即以上,即孙子结点及以上),例如
h1 > strong {color:red;}
<h1>This is <strong>very</strong> <strong>very</strong> important.</h1>
<h1>This is <em>really <strong>very</strong></em> important.</h1> //只会选择第一个h1下的strong标签,而第二个h1下的strong又隔了一个em标签则不符合要求
2. 后代选择器
理解为选择 在...中的....,不管嵌套层次多深都会选取
3. 相邻兄弟选择器
紧跟在某元素之后具有相同父元素的元素
h1 + p {margin-top:50px;}
4. 属性选择器
要选择包含某种属性而不管属性值是什么的元素:
a[title][href]{ }
也可选择属性值为某特定值的元素:
a[href="http://www.w3school.com.cn/"][title="W3School"] {color: red;}
此时属性值需完全匹配,若选择属性值中的部分属性(部分字符串)则需使用波浪号:
p[class~="important"] {color: red;}
子串匹配属性
特定属性选择
*[lang|="en"] {color: red;}//<span style="font-family: Verdana, Arial, 宋体; line-height: 18px; background-color: rgb(249, 249, 249);">选择 lang 属性等于 en 或以 en- 开头的所有元素</span>