你必須記住的30個CSS選擇器

開篇

有30個CSS選擇器你必須爛熟於心,它們適應於當今各大主流瀏覽器。

1.*

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

*選擇器選擇的是每一個單一元素。很多程序員用上面的CSS將所有元素的margin和padding清零。雖然這是有效的,但最好還是別這麼做,這會使得瀏覽器的負擔很重。

*選擇器也可以用在孩子選擇器中。

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

這會使#container所有孩子都有border,但還是那句話,如果不是必須得這麼做,還是別用星選擇器。

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

2.#x

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

id選擇器的優先級很高,因此在用之前問問自己:我僅僅是爲了找到這個元素而去給他加id的嗎?

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

3..x

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

class選擇器和id選擇器不同,首先優先級沒有id高,再者id選擇器只能選擇一個,而class選擇器則可以篩選出來一組。

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

4.x y

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

當不是選擇所有後代時,後代選擇器將會非常有用,但有一點得注意: 
如果你的選擇器是x y z a b.error, 那你就用錯了。你得問問自己是否需要應用每一層?

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

5.x

a { color: red; }
ul { margin-left: 0; }
  • 1
  • 2

如果想選擇同一類元素,那就不要用id或class了,直接用元素選擇器。

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

6.x:visted and x:link

a:link { color: red; }
a:visted { color: purple; }
  • 1
  • 2

我們常常用僞類:link篩選a標籤是還未被點擊;而用:visited去篩選哪些別點擊過了。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

7.x + y

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

相鄰選擇器會選擇第一個相鄰的元素,如上面的例子會讓ul後第一個段落的字體變爲紅色(而ul與p之間是不能有其他元素的)。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

8.x > y

div#container > ul {
  border: 1px solid black;
}
  • 1
  • 2
  • 3

這也是一種後代選擇器,但它與x y這種後代選擇器不同,它只能選擇直系後代。如:

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在這個例子中,#cotainer > ul只會選擇第一個ul,而不會search到ul裏面那個包含li的ul。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

9.x ~ y

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

這種兄弟選擇器與x + y類似,但不同的是,後者只能篩選第一個p,而這種卻可以滿足ul下所有的直系p標籤。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

10.x[title]

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

屬性選擇器。這將選擇所有具有title屬性的a標籤。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

11.x[href="foo"]

a[href="http://net.tutsplus.com"] {
  color: #1f6053; /* nettuts green */
}
  • 1
  • 2
  • 3

這個選擇器可以選擇鏈接爲href="http://net.tutsplus.com"的a標籤,可如果這個裏這個鏈接變了呢?,這未免有些嚴格,可以適當的用正則表達式去匹配。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

12.x[href*="nettuts"]

a[href*="tuts"] {
  color: #1f6053; /* nettuts green */
}
  • 1
  • 2
  • 3

‘*’號將匹配href中含有nuttuts字符,如果想更加嚴格,還可以用^$表示開始和結束。

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

13.x[href^="http"]

a[href^="http"] {
   background: url(path/to/external/icon.png) no-repeat;
   padding-left: 10px;
}
  • 1
  • 2
  • 3
  • 4

這樣去篩選具有有效href的a將匹配http://和https://.

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

14.x[href$=".jpg"]

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

這將會選擇鏈接爲jpg格式的圖片鏈接,可是如果圖片類型爲png或gif等怎麼辦?

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

15.x[data-*="foo"]

a[data-filetype="image"] {
   color: red;
}
  • 1
  • 2
  • 3

按照規則14.我們可能得:

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

可這也太。。。 
我們可以加一個屬性用以標示。

<a href="path/to/image.jpg" data-filetype="image"> Image Link </a>
  • 1
a[data-filetype="image"] {
   color: red;
}
  • 1
  • 2
  • 3

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

16.x[foo~="bar"]

a[data-info~="external"] {
   color: red;
}

a[data-info~="image"] {
   border: 1px solid black;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

~將會讓我們匹配到屬性值被空格分隔符分開的值,如:

"<a href="path/to/image.jpg" data-info="external image"> Click Me, Fool </a>
  • 1

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

17.x:checked

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

這個常常對checkbox非常有用。

view demo

Compatibility

  • IE9+
  • Firefox
  • Chrome
  • Safari
  • Opera

18.x:after

僞類beforeafter已經有了一些新的用法,比如最常見的:

.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
  • 12
  • 13

沒錯,這就是默認標準clearfix的實現原理。

Compatibility

  • IE8+
  • Firefox
  • Chrome
  • Safari
  • Opera

19.x:hover

div:hover {
  background: #e3e3e3;
}
  • 1
  • 2
  • 3

但是得注意,:hover在早期IE中並不適用。

Compatibility

  • IE6+(In IE6, :hover must be applied to an anchor element)
  • Firefox
  • Chrome
  • Safari
  • Opera

20.x:not(selector)

div:not(#container) {
   color: blue;
}
  • 1
  • 2
  • 3

反選選擇器。

view demo

Compatibility

  • IE9+
  • Firefox
  • Chrome
  • Safari
  • Opera

21.x::pseudoElement

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

僞元素選擇器,注意儘量還是按標準來,多使用::而不是:

view demo

Compatibility

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera

22.x:nth-child(n)

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

選擇一組中特定的孩子。n表示第幾個,也可以是表達式,如2n+1,2n.

view demo

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

23.x:nth-last-child(n)

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

如果li有400個,而你需要target到第397個,那用這個咱合適不過了。

view demo

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

24.x:nth-of-type(n)

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

如果ul沒有id,而你又要找第三個ul,那個這種方式是不錯。

view demo

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

25.x:nth-last-of-type(n)

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

ul:nth-of-type剛好相反。

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

26.x:first-child

ul li:first-child {
   border-top: none;
}
  • 1
  • 2
  • 3

view demo

Compatibility

  • IE7+
  • Firefox
  • Chrome
  • Safari
  • Opera

27.x:last-child

Example
<ul>
   <li> List Item </li>
   <li> List Item </li>
   <li> List Item </li>
</ul>
  • 1
  • 2
  • 3
  • 4
  • 5
ul {
 width: 200px;
 background: #292929;
 color: white;
 list-style: none;
 padding-left: 0;
}

li {
 padding: 10px;
 border-bottom: 1px solid black;
 border-top: 1px solid #3c3c3c;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

view demo

但是我不想要第一個的上邊框和最後一個的下邊框,可以這麼做:

li:first-child {
    border-top: none;
}

li:last-child {
   border-bottom: none;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

view demo

Compatibility

  • IE9+
  • Firefox
  • Chrome
  • Safari
  • Opera

28.X:only-child

div p:only-child {
   color: red;
}
  • 1
  • 2
  • 3

這將匹配div裏只有一個p的元素。如:

<div><p> My paragraph here. </p></div>

<div>
   <p> Two paragraphs total. </p>
   <p> Two paragraphs total. </p>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

view demo

Compatibility

  • IE9+
  • Firefox
  • Chrome
  • Safari
  • Opera

29.X:only-of-type

li:only-of-type {
   font-weight: bold;
}
  • 1
  • 2
  • 3

這將匹配元素內只有一個li的元素,有時這個做法很簡便。比如要尋找只有一個列表的ul,可以:

ul > li:only-of-type {
   font-weight: bold;
}
  • 1
  • 2
  • 3

view demo

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

30.x:first-of-type

Example
<div>
   <p> My paragraph here. </p>
   <ul>
      <li> List Item 1 </li>
      <li> List Item 2 </li>
   </ul>

   <ul>
      <li> List Item 3 </li>
      <li> List Item 4 </li>
   </ul>   
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

如何尋找上面的 “List Item 2”呢?

辦法1
ul:first-of-type > li:nth-child(2) {
   font-weight: bold;
}
  • 1
  • 2
  • 3
辦法2
p + ul li:last-child {
   font-weight: bold;
}
  • 1
  • 2
  • 3
辦法3
ul:first-of-type li:nth-last-child(1) {
   font-weight: bold;   
}
  • 1
  • 2
  • 3

view demo

Compatibility

  • IE9+
  • Firefox 3.5+
  • Chrome
  • Safari
  • Opera

總結

上述選擇器在IE6中的使用要尤其小心,但是別因爲這而影響你閱讀這篇文章。你可以參考一下瀏覽器兼容表,或者你可以用 Dean Edward’s excellent IE9.js script 爲舊版瀏覽器提供這些選擇器的支持。

另外,當你在使用一些Javascript庫時,如jQuery,請儘量的使用這些原生的CSS3選擇器,因爲瀏覽器選擇器引擎將會按照瀏覽器的原生方式去解析,這將使得你的代碼更加高效

注:此文爲譯文我的博客請戳 | 原文請戳

發佈了42 篇原創文章 · 獲贊 21 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章