css选择器 兼容性 IE6 IE7 【经验分享帖】
你也许已经掌握了id、class、后台选择器这些基本的css选择器。但这远远不是css的全部。下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器兼容性问题。掌握了它们,才能真正领略css的巨大灵活性。
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
1. *
* {margin: 0; padding: 0;}
星状选择符会在页面上的每一个元素上起作用。web设计者经常用它将页面中所有元素的margin和padding设置为0。 *选择符也可以在子选择器中使用。
#container * { border: 1px solid black; }
上面的代码中会应用于id为container元素的所有子元素中。 除非必要,我不建议在页面中过的的使用星状选择符,因为他的作用域太大,相当耗浏览器资源。 兼容浏览器:IE6+、Firefox、Chrome、Safari、Opera
2. #
#container { width: 960px; margin: auto; }
井号作用域有相应id的元素。id是我们最常用的css选择器之一。id选择器的优势是精准,高优先级(优先级基数为100,远高于class的10), 作为javascript脚本钩子的不二选择,同样缺点也很明显优先级过高,重用性差,所以在使用id选择器前,我们最好问下自己,真的到了非用id选择 器的地步? 兼容浏览器:IE6+、Firefox、Chrome、Safari、Opera
3. .X
.error { color: red; }
这是一个class(类)选择器。class选择器与id选择器的不同是class选择器能作用于期望样式化的一组元素。 兼容浏览器:IE6+、Firefox、Chrome、Safari、Opera
4. .X.Y
.x.y 同时满足两个条件才执行。我们来看个例子:
<div class="x y">XXX</div>
.x.y{ color: red; }
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera。
5. X Y
li a { text-decoration: none; }
这 也是我们最常用的一种选择器——后代选择器。用于选取X元素下子元素Y,要留意的点是,这种方式的选择器将选取其下所有匹配的子元素,无视层级,所以有 的情况是不宜使用的,比如上述的代码去掉li下的所有a的下划线,但li里面还有个ul,我不希望ul下的li的a去掉下划线。使用此后代选择器的时候要 考虑是否希望某样式对所有子孙元素都起作用。这种后代选择器还有个作用,就是创建类似命名空间的作用。比如上述代码样式的作用域明显为li。 兼容浏览器:IE6+、Firefox、Chrome、Safari、Opera
6. X
a { color: red; } ul { margin-left: 0; }
标签选择器。使用标签选择器作用于作用域范围内的所有对应标签。优先级仅仅比*高。 兼容浏览器:IE6+、Firefox、Chrome、Safari、Opera
7. X:visited和X:link
a:link { color: red; } a:visted { color: purple; }
使用:link伪类作用于未点击过的链接标签。:hover伪类作用于点击过的链接。 兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
8. X+Y
ul + p { color: red; }
相邻选择器,上述代码中就会匹配在ul后面的第一个p,将段落内的文字颜色设置为红色。(只匹配第一个元素) 兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
9. X>Y
div#container > ul {border: 1px solid black;}
<div id="container"> <ul> <li>List Item <ul> <li>Child </li> </ul> </li> <li>List Item </li> <li>List Item </li> <li>List Item </li> </ul> </div>
子 选择器。与后代选择器X Y不同的是,子选择器只对X下的直接子级Y起作用。在上面的css和html例子中,div#container>ul仅对container中最 近一级的ul起作用。从理论上来讲X > Y是值得提倡选择器,可惜IE6不支持。 兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
10. X ~ Y
ul ~ p {color: red;}
相邻选择器,与前面提到的X+Y不同的是,X~Y匹配与X相同级别的所有Y元素,而X+Y只匹配第一个。 兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
11. X[title]
a[title] {color: green;}
属性选择器。比如上述代码匹配的是带有title属性的链接元素。
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
12. X[title="foo"]
a[] {color: #1f6053;}
属性选择器。 上面的代码匹配所有拥有href属性,且href为http://www.itcast.cn的所有链接。
这个功能很好,但是多少又有些局限。如果我们希望匹配href包含css9.net的所有链接该怎么做呢?看下一个选择器。
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
13.
X[title*="css9.net"]
a[href*="itcast.cn"] {color: #1f6053;}
属性选择器。正如我们想要的,上面代码匹配的是href中包含"itcast.cn"的所有链接。
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
14.
X[href^="http"]
a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"] { color: red; }
看上去比较麻烦。另一个解决办法是为所有的图片链接加一个特定的属性,例如‘data-file’
<a href="path/to/image.jpg" data-filetype="image"> 图片链接 </a>
- css代码如下:
a[data-filetype="image"] { color: red;}
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
15.
X[foo~="bar"]
属性选择器。属性选择器中的波浪线符号可以让我们匹配属性值中用空格分隔的多个值中的一个。看下面例子:
html代码
<a href="path/to/image.jpg" data-info="external image"> Click Me, Fool </a>
a[data-info~="external"] { color: red; } a[data-info~="image"] { border: 1px solid black; }
兼容浏览器:IE7+、Firefox、Chrome、Safari、Opera
checked伪类用来匹配处于选定状态的界面元素,如radio、checkbox。
input[type=radio]:checked {border: 1px solid black;}
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
18. X:after和X:before
这两个伪类与content结合用于在元素的前面或者后面追加内容,看一个简单的例子:
h1:after {content:url(/i/logo.gif)}
.clearfix:after { content: ""; display: block; clear: both; visibility: hidden; font-size: 0; height: 0; } .clearfix { *display: inline-block; height: 1%; }
19. X:hover
div:hover { background: #e3e3e3; }
需要注意的是,在ie 6中,:hover只能用于链接元素。
这里分享一个经验,在设定链接划过时出现下滑线时,使用border-bottom会比text-decoration显得更漂亮些。代码如下:
a:hover { border-bottom: 1px solid black; }
20. X:not(selector)
p::first-letter { float: left; font-size: 2em; font-weight: bold; font-family: cursive; padding-right: 2px; }
p::first-line { font-weight: bold; font-size: 1.2em; }
li:nth-child(3) { color: red; }
tr:nth-child(2n) { background-color: gray; }
23. X:nth-last-child(n)
li:nth-last-child(2) { color: red; }
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
24. X:nth-of-type(n)
ul:nth-of-type(3) { border: 1px solid black; }
兼容浏览器:IE9+、Firefox、Chrome、Safari
25. X:nth-last-of-type(n)
ul:nth-last-of-type(3) { border: 1px solid black; }
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
26. X:first-child
:first-child伪类用于匹配一个序列的第一个元素。我们经常用它来实现一个序列的第一个元素或最后一个元素的上(下)边框,如:
ul:nth-last-of-type(3) { border: 1px solid black; }
27. X:last-child
ul > li:last-child { border-bottom:none; }
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
div p:only-child { color: red; }
<div> <p> My paragraph here. </p> </div> <div> <p> Two paragraphs total. </p> <p> Two paragraphs total. </p> </div>
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
li:only-of-type { font-weight: bold; }
兼容浏览器:IE9+、Firefox、Chrome、Safari、Opera
30. X:first-of-type
:first-of-type伪类与:nth-of-type(1)效果相同,匹配出现的第一个元素。我们来看个例子:
ul:first-of-type > li:nth-child(2) { font-weight: bold; }
p + ul li:last-child { font-weight: bold; }
ul:first-of-type li:nth-last-child(1) { font-weight: bold; }
| 总结: 如果你正在使用老版本的浏览器,如IE 6,在使用上面css选择器时一定要注意它是否兼容。不过,这不应成为阻止我们学习使用它的理由。在设计时,你可以参考浏览器兼容性列表,也可以通过脚本手段让老版本的浏览器也支持它们。 另一点,我们在使用javascript类库的选择器时,例如jquery,要尽可能的使用这些原生的css3选择器,因为类库的选择器引擎会通过浏览器内置解析它们,这样会获得更快的速度。 |
浙公网安备 33010602011771号