zoukankan      html  css  js  c++  java
  • 前端必须掌握30个CSS3选择器

    本文中将综合前端开发中常用的30个CSS3选择器,并且附带了浏览器的支持情况,希望对大家有所帮助。

    1、*:通用元素选择器

    1
    2
    3
    4
    5
    
    * {   margin:0;   padding:0;  }


    *选择器是选择页面上的全部元素,上面的代码作用是把全部元素的margin和padding设为0,最基本的清除默认CSS样式方法

    *选择器也可以应用到子选择器中,例如下面的代码:

    1
    2
    3
    4
    
    #container * {   border:1px solid black;  }


    这样ID为container 的所有子标签元素都被选中了,并且设置了border。

    查看演示

    兼容性
    1. IE6+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    #ID:ID选择器

    1
    2
    3
    4
    5
    
    #container{     960px;     margin: auto;  }


    ID选择器是CSS中效率最高的选择器,使用的时候要保证ID的唯一性。

    查看演示

    兼容性
    1. IE6+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    .class:类选择器

    1
    2
    3
    4
    
    .error{    color: red;  }


    类选择器效率低于ID选择器,一个页面可以有多个class,并且class可以放在不同的标签中使用。

    查看演示

    兼容性
    1. IE6+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X Y:标签组合选择器

    1
    2
    3
    4
    
    li a{    text-decoration: none;  }


    标签组合选择器也是常用的选择器。

    查看演示

    兼容性
    1. IE6+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X:标签选择器

    1
    2
    3
    

    acolor: red; }

    ulmargin-left:0; }


    如果你只是想要页面中的某个标签样式改变,可以选择使用标签选择器。

    查看演示

    兼容性
    1. IE6+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    1
    2
    3
    

    a:linkcolor: red; }

    a:vistedcolor: purple; }


    伪类选择器,最常用的为A标签

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X + Y:毗邻元素选择器

    1
    2
    3
    4
    
    ul + p{     color: red;  }


    毗邻元素选择器,匹配的是所有紧随X元素之后的同级元素Y

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X > Y:子元素选择器

    1
    2
    3
    4
    

    div #container > ul{    

    border:1px solid black;  

    }


    匹配#container下的所有子元素。
    关于X>YX Y的区别请看下面的html实例:

    1
    2
    3
    4
    5
    6
    
    <div id="container">  
    
    *   List Item
            *   Child*   List Item*   List Item*   List Item  
    </div>


    选择器#container > ul只会匹配到第一个UL,也就是#container的子元素UL,而不是li里面的ul,但是div ul则可以匹配到所有DIV里面的ul。

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X ~ Y:

    1
    2
    3
    4
    
    ul ~ p{     color: red;  }


    匹配任何在X元素之后的同级P元素。也就是选择了UL之后的同级所有的元素。

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[title]:属性选择器

    1
    2
    3
    4
    
    a[title]{     color: green;  }


    匹配具有某属性的标签,例如实例中是匹配具有title属性的a标签。

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[href=”foo”]

    1
    2
    3
    4
    

    a[href="http://js8.in"]{    

    color:#1f6053; /* nettuts green */

    }


    也属于属性选择器,匹配属性中为某个值的标签。例如实例中匹配的为href="http://js8.in"的a标签,而其他链接的a标签不选择。

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[href*=”nettuts”]

    1
    2
    3
    4
    

    a[href*="tuts"]{    

    color:#1f6053; /* nettuts green */

    }


    属于属性选择器,匹配href中所有含有tuts的标签。正则匹配

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[href^=”http”]

    1
    2
    3
    4
    5
    

    a[href^="http"]{    

    background:url(path/to/external/icon.png) no-repeat;    

    padding-left:10px;  

    }


    与上面的属相选择标签类似,但是匹配的以http开头的A标签,正则匹配

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[href$=”.jpg”]

    1
    2
    3
    4
    
    a[href$=".jpg"]{     color: red;  }


    匹配属性中以.jpg结尾的标签,正则匹配,也是属性选择器的一种

    查看演示

    兼容性
    1. IE7+

    2. Firefox

    3. Chrome

    4. Safari

    5. Opera

    X[data-*=”foo”]

    如果你要匹配所有的图片链接,你可以通过下面的CSS来实现:

    1
    2
    3
    4
    5
    6
    7
    
    a[href$=".jpg"],  
    a[href$=".jpeg"],  
    a[href$=".png"],  
    a[href$=".gif"]{     color: red;  }


    但是如果我们给a标签添加一个data-filetype属性,我们就可以使用下面的CSS来快速的选择我们需要匹配的标签了。

    1
    2
    3
    
    [ Image Link ](path/to/image.jpg)  
    </html>

    css
    a[data-filetype=”image”] {
    color: red;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    

    [查看演示]

    (http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/attributes6.html)

    ##### 兼容性1.  IE7+
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X[foo~="bar"]

    css

    a[data-info~=”external”] {

    color: red;
    }

    a[data-info~=”image”] {
    border: 1px solid black;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    匹配属性中具有多个空格分隔的值、其中一个值等于“bar”的X元素,例如下面的例子:
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/attributes7.html)
    
    ##### 兼容性1.  IE7+
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:checked

    css

    input[type=radio]:checked {
    border: 1px solid black;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    这个选择器主要用于checkbox,选择checkbox为当前选中的那个标签。
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/checked.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:after

    css

    .clearfix:after {
    content: “”;
    display: block;
    clear: both;
    visibility: hidden;
    font-size: 0;
    height: 0;
    }

    .clearfix {
    *display: inline-block;
    _height: 1%;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    

    `before` 和`after`是在选择的标签之前或者之后插入内容,一般用于清除浮动,

    但是对于IE6、IE7是不可用的。

    ##### 兼容性1.  IE8+
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:hover

    css

    div:hover {

    background: #e3e3e3;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    最常用的就是A标签了,但是在IE6浏览器下除了A标签之外,其他标签`div:hover`不匹配。
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/generalcombinator.html)
    
    ##### 兼容性1.  IE6+(IE6只可以使用在A标签中)
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:not(selector)

    css

    *:not(p) {
    color: green;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    选择除了()中选择器之外的标签元素。
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/not.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X::pseudoElement

    css

    p::first-line {

    font-weight: bold;
    font-size: 1.2em;
    }
    p::first-letter {
    float: left;
    font-size: 2em;
    font-weight: bold;
    font-family: cursive;
    padding-right: 2px;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    分别用于匹配元素的第一行和第一个字母。看实例:
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/pseudoElements.html)
    
    ##### 兼容性1.  IE6+
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:nth-child(n)

    css

    li:nth-child(3) {
    color: red;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    匹配X元素中从头数第几个标签,例如上面的代码是匹配的是第三个li标签。
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/nth.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox 3.5+
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:nth-last-child(n)

    css

    li:nth-last-child(2) {
    color: red;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    与上一个选择器相反,这个选择器是倒序匹配第几个标签,上面的代码的意思是匹配倒数第二个li标签
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/nthLast.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox 3.5+
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:nth-of-type(n)

    css

    ul:nth-of-type(3) {
    border: 1px solid black;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    `:nth-child()`作用类似,但是仅匹配使用同种标签的元素
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/nthOfType.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox 3.5+
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:nth-last-of-type(n)

    css

    ul:nth-last-of-type(3) {
    border: 1px solid black;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    `:nth-last-child() `作用类似,但是仅匹配使用同种标签的元素
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/generalcombinator.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox 3.5+
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:first-child

    css

    ul li:first-child {

    border-top: none;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    匹配其父元素的第n个子元素,第一个编号为1
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/firstChild.html)
    
    ##### 兼容性1.  IE7+
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:last-child

    css

    ul > li:last-child {
    color: green;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    匹配其父元素的倒数第n个子元素,第一个编号为1
    
    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/firstChild.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:only-child

    css

    div p:only-child {
    color: red;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    

    匹配父元素下仅有的一个子元素,等同于:first-child:last-child

    或 :nth-child(1):nth-last-child(1)

    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/onlyChild.html)
    
    ##### 兼容性1.  IE9
    2.  Firefox
    3.  Chrome
    4.  Safari
    5.  Opera
    
    ### X:only-of-type

    css

    li:only-of-type {
    font-weight: bold;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    

    匹配父元素下使用同种标签的唯一一个子元素,

    等同于:first-of-type:last-of-type或 :nth-of-type(1):nth-last-of-type(1)

    [查看演示](http://d2o0t5hpnwv4c1.cloudfront.net/840_cssSelectors/selectors/onlyOfType.html)
    
    ##### 兼容性1.  IE92.  Firefox3.5+
    3.  Chrome4.  Safari5.  Opera### X:first-of-type

    css

    li:only-of-type {
    font-weight: bold;
    }
    ```
    匹配父元素下使用同种标签的第一个子元素,等同于:nth-of-type(1)

    查看演示

    兼容性
    1. IE9

    2. Firefox 3.5+

    3. Chrome

    4. Safari

    5. Opera

    原文出处:http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/

  • 相关阅读:
    Java语言编码规范(Java Code Conventions) 转载
    Flex 彻底屏蔽右键 (转载)
    JAVA 读取文件(收集)
    可以运行SWT的精简版JRE 1.4.2_04, 压缩后仅1.3MB[整理]
    转载:多核平台下的JAVA优化
    森林消防智慧预警:火灾监测 Web GIS 可视化平台
    绿色数字园区运维:一屏群集 3D 可视化智慧楼宇
    js删除json key
    学习、应用Web标准一路走来——《重构之美》原创系列文章快速入口。
    中国互联网的鱼死网破新时代。
  • 原文地址:https://www.cnblogs.com/sdream/p/5462435.html
Copyright © 2011-2022 走看看