最近在研究jQuery的选择器,你们知道jQuery的选择器和css的选择器很是类似,因此整理一下css选择器;css
css1-css3提供很是丰富的选择器,可是因为某些选择器被各个浏览器支持的状况不同,因此不少选择器在实际css开发中不多用到。html
选择器css3 |
含义浏览器 |
示例url |
*spa |
通用元素选择器,匹配任何元素.net |
* { margin:0; padding:0; }code |
Ehtm |
标签选择器,匹配全部使用E标签的元素blog |
p { font-size:2em; } |
.info和E.info |
class选择器,匹配全部class属性中包含info的元素 |
.info { background:#ff0; } p.info { background:#ff0; } |
#info和E#info |
id选择器,匹配全部id属性等于footer的元素 |
#info { background:#ff0; } p#info { background:#ff0; } |
选择器 | 含义 |
示例 |
E,F |
多元素选择器,同时匹配全部E元素或F元素,E和F之间用逗号分隔 |
Div,p { color:#f00; } |
E F |
后代元素选择器,匹配全部属于E元素后代的F元素,E和F之间用空格分隔 |
#nav li { display:inline; } li a { font-weight:bold; } |
E > F |
子元素选择器,匹配全部E元素的子元素F |
div > strong { color:#f00; } |
E + F |
毗邻元素选择器,匹配全部紧随E元素以后的同级元素F |
p + p { color:#f00; } |
选择器 | 含义 |
示例 |
E[att] |
匹配全部具备att属性的E元素,不考虑它的值。(注意:E在此处能够省略,好比“[cheacked]”。如下同。) |
p[title] { color:#f00; } |
E[att=val] |
匹配全部att属性等于“val”的E元素 |
div[class=”error”] { color:#f00; } |
E[att~=val] |
匹配全部att属性具备多个空格分隔的值、其中一个值等于“val”的E元素 |
td[class~=”name”] { color:#f00; } |
E[att|=val] |
匹配全部att属性具备多个连字号分隔(hyphen-separated)的值、其中一个值以“val”开头的E元素,主要用于lang属性,好比“en”、“en-us”、“en-gb”等等 |
p[lang|=en] { color:#f00; } |
注:CSS 2.1 属性选择器还有一个特色就是使用多个选择器,同事知足这多个选择器:blockquote[class=quote][cite] { color:#f00; }
选择器 | 含义 |
示例 |
E:first-child |
匹配父元素的第一个子元素 |
p:first-child { font-style:italic; } input[type=text]:focus { color:#000; background:#ffe; } input[type=text]:focus:hover { background:#fff; } q:lang(sv) { quotes: “\201D” “\201D” “\2019″ “\2019″; } |
E:link |
匹配全部未被点击的连接 |
|
E:visited |
匹配全部已被点击的连接 |
|
E:active |
匹配鼠标已经其上按下、尚未释放的E元素 |
|
E:hover |
匹配鼠标悬停其上的E元素 |
|
E:focus |
匹配得到当前焦点的E元素 |
|
E:lang(c) |
匹配lang属性等于c的E元素 |
选择器 | 含义 |
示例 |
E:first-line |
匹配E元素的第一行 |
p:first-line { font-weight:bold; color;#600; } .preamble:first-letter { font-size:1.5em; font-weight:bold; } .cbb:before { content:”"; display:block; height:17px; width:18px; background:url(top.png) no-repeat 0 0; margin:0 0 0 -18px; } a:link:after { content: ” (” attr(href) “) “; } |
E:first-letter |
匹配E元素的第一个字母 |
|
E:before |
在E元素以前插入生成的内容 |
|
E:after |
在E元素以后插入生成的内容 |
选择器 |
含义 |
示例 |
E ~ F |
匹配任何在E元素以后的同级F元素 |
p ~ ul { background:#ff0; } |
选择器 | 含义 |
示例 |
E[att^=”val”] |
属性att的值以”val”开头的元素 |
div[id^="nav"] { background:#ff0; } |
E[att$=”val”] |
属性att的值以”val”结尾的元素 |
|
E[att*=”val”] |
属性att的值包含”val”字符串的元素 |
选择器 | 含义 |
示例 |
E:enabled |
匹配表单中激活的元素 |
input[type="text"]:disabled { background:#ddd;} |
E:disabled |
匹配表单中禁用的元素 |
|
E:checked |
匹配表单中被选中的radio(单选框)或checkbox(复选框)元素 |
|
E::selection |
匹配用户当前选中的元素 |
选择器 | 含义 |
示例 |
E:root |
匹配文档的根元素,对于HTML文档,就是HTML元素 |
p:nth-child(3) { color:#f00; } p:nth-child(odd) { color:#f00; } p:nth-child(even) { color:#f00; } p:nth-child(3n+0) { color:#f00; } p:nth-child(3n) { color:#f00; } tr:nth-child(2n+11) { background:#ff0; } tr:nth-last-child(2) { background:#ff0; } p:last-child { background:#ff0; } p:only-child { background:#ff0; } p:empty { background:#ff0; } |
E:nth-child(n) |
匹配其父元素的第n个子元素,第一个编号为1 |
|
E:nth-last-child(n) |
匹配其父元素的倒数第n个子元素,第一个编号为1 |
|
E:nth-of-type(n) |
与:nth-child()做用相似,可是仅匹配使用同种标签的元素 |
|
E:nth-last-of-type(n) |
与:nth-last-child() 做用相似,可是仅匹配使用同种标签的元素 |
|
E:last-child |
匹配父元素的最后一个子元素,等同于:nth-last-child(1) |
|
E:first-of-type |
匹配父元素下使用同种标签的第一个子元素,等同于:nth-of-type(1) |
|
E:last-of-type |
匹配父元素下使用同种标签的最后一个子元素,等同于:nth-last-of-type(1) |
|
E:only-child |
匹配父元素下仅有的一个子元素,等同于:first-child:last-child或 :nth-child(1):nth-last-child(1) |
|
E:only-of-type |
匹配父元素下使用同种标签的惟一一个子元素,等同于:first-of-type:last-of-type或 :nth-of-type(1):nth-last-of-type(1) |
|
E:empty |
匹配一个不包含任何子元素的元素,注意,文本节点也被看做子元素 |
选择器 | 含义 |
示例 |
E:not(s) |
匹配不符合当前选择器的任何元素 |
:not(p) { border:1px solid #ccc; } |
选择器 | 含义 |
|
E:target |
匹配文档中特定”id”点击后的效果 |
CSS选择器浏览器支持状况,其中包括最新的CSS3和Safari 4.0 Beta的支持状况。感谢Estelle Weyl的总结。
2、选择器的优先级别
通常而言,选择器越特殊,它的优先级越高。也就是选择器指向的越准确,它的优先级就越高。例如:
.polaris span {color:red;} span {color:blue;}
浏览器天然会显示红色的字,由于.polaris span {color:red;}的优先级高;
对于什么状况下使用什么选择器,用不一样选择器的原则是:
第一:准确的选到要控制的标签;
第二:使用最合理优先级的选择器;
第三:HTML和CSS代码尽可能简洁美观。
后代选择器的定位原则
在这里介绍一下对于后代选择器,浏览器是如何查找元素的呢?
浏览器CSS匹配不是从左到右进行查找,而是从右到左进行查找。好比DIV#divBox p span.red{color:red;},浏览器的查找顺序以下:先查找html中全部class='red'的span元素,找到后,再查找其父辈元素中是否有p元素,再判断p的父元素中是否有id为divBox的div元素,若是都存在则匹配上。
浏览器从右到左进行查找的好处是为了尽早过滤掉一些无关的样式规则和元素。