DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
關於 CSS 選擇器的完整梳理
編輯:CSS詳解     

把自己在學習與工作中用到的選擇器整理成一份筆記,從最基礎的元素選擇器一路到屬性選擇器,每個都附上可驗證的範例。

1. 元素選擇器

最簡單也最常見,直接以標籤名作為選擇器:

html {color:black;} // 設定 html 元素顏色為 black
div {color:gray;}   // 設定 div 元素顏色為 gray
h1 {color:silver;}  // 設定 h1 元素顏色為 silver

想讓某個元素套用某個樣式,寫「元素{樣式}」即可。

2. 選擇器分組

把多個元素寫在一起共用樣式:

div,p,strong{color:#FF0}
.part_one div a,.part_two a,.part_three p{color:#FFF}

3. 類選擇器

<div class="mydiv">This is content.</div>

.mydiv{color:#F00;}

4. ID 選擇器

<div id="mydiv1">This is content.</div>

#mydiv1{color:#F00;}

寫法與類選擇器幾乎一樣,差別在權重:同一元素上,ID 選擇器會蓋掉類選擇器。

5. 後代選擇器

「誰裡面的誰」,包含所有層級:

div ul{color:#FF0}

6. 子元素選擇器

只選直系子代,不管孫輩:

div > h1{color:red;}

<div><h1>my h1</h1></div>             // 有效
<div><div><h1>li h1</h1></div></div> // 有效
<div><ul><li><h1>li h1</h1></li></ul></div>

第三行的 h1 是 li 的直系子代,而不是 div 的,所以不會被選中。

7. 相鄰兄弟選擇器

選緊接在另一元素之後、且擁有相同父元素的元素:

h1 + p {color:red;}

<h1>header</h1>
<p>text</p>
<p>text</p>

只會選到第一個 p,第二個 p 並非緊接在 h1 之後。

li + li {font-weight:bold;}

<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>

這個樣式會套到第二、第三個 li,第一個 li 上方沒有相鄰的 li,因此不受影響。

8. 偽類

a:link、a:visited、a:hover、a:active 都是偽類的一種(這幾個合稱偽類中的「偽錨」)。

:focus 很實用,常用來設定輸入框聚焦時的背景:

input:focus{background-color:yellow;}

<input type="text" name="fname" />
<input type="text" name="lname" />
<input type="submit" value="Submit" />

這樣連送出按鈕都會變黃,有點惱人。加上屬性選擇器收斂範圍即可:

input[type="text"]:focus{background-color:yellow;}

:first-child 必須先宣告 DOCTYPE,否則在 IE 中不生效:

p:first-child {font-weight: bold;}
li:first-child {font-weight: bold;}

<p>test words1</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
<ul>
<p>test words ul p</p>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
<p>test words2</p>
<p>test words3</p>

一開始很容易誤解成「某元素的頭一個子元素」,但其實它的意思是「身為第一個孩子的這個元素」。以上程式碼會選中第一個 p(test words1)與第二個 ul 中的 p(test words ul p),其餘的 p 都不符合;同時每個 ul 中的第一個 li 也會被選中,因為它們的父元素不同。

:lang 為帶有指定 lang 屬性的元素定義引號樣式:

q:lang(no){quotes: "~" "~"}

<p>text <q lang="no">what?</q></p>

9. 偽元素

:first-letter 為文字的第一個字母加上特殊樣式(僅能用於塊級元素):

p:first-letter{color:#ff0000;font-size:xx-large;}

偽元素可以與類搭配:

p.article:first-letter{color: #FF0000;}

<p class="article">This is a paragraph in an article。</p>

可套用在 first-letter 上的屬性:font、color、background、margin、padding、border、text-decoration、vertical-align(僅當 float 為 none)、text-transform、line-height、float、clear。

:first-line 為首行加上特殊樣式:

p:first-line{color:#0000ff;}

:before / :after 在元素前後插入內容,最常見的用途就是清除浮動:

div:after{
clear:both;
content:".";
display:block;
height:0;
visibility:hidden;
font-size:0;
}
h1:before{content:url(images/logo.gif)}  /* 在 h1 之前插入圖片 */
h1:after {content:url(images/logo.gif)}  /* 在 h1 之後插入圖片 */

10. 屬性選擇器

在 IE8 及更早版本中使用時,必須宣告 <!DOCTYPE>。

[attribute] 選取帶有指定屬性的元素:

[title]{color:F00;}

<h1 title="hello css">hello h1~!</h1>
<h2>hello h2~!</h2>

只有 h1 會被設定為 F00,h2 不受影響。變化寫法:

*[title]{color:F00;}                  // 所有帶 title 的元素
a[title]{color:F00;}                  // 帶 title 的 a 元素
.mydiv ul li a[title]{color:F00;}     // 限定在 .mydiv 結構內
#mydiv ul li a[title]{color:F00;}     // 限定在 id 為 mydiv 的結構內

[attribute=value] 選取屬性與值都符合的元素。這招在某些場景很實用,例如高亮,不必動用 jQuery 或後端判斷:

a[target="_blank"]{color:yellow;}   // 所有 target 為 _blank 的 a
h1[title="mytitle"]{color:yellow;}  // 所有 title 為 mytitle 的 h1

[attribute~=value] 選取屬性值中包含指定詞彙的元素:

img[title~="first"]{border:3px solid yellow}

<img src="a.jpg" title="this is first img" />
<img src="b.jpg" title="this is second img" />

[attribute|=value] 選取屬性值以指定值開頭、且該值是完整單詞的元素:

[class|=my]{background:yellow;}

<h1 class="my-header">this is header</h1>  // 有效
<p class="my-text">this is text</p>       // 有效
<p class="my-content">this is content</p> // 有效
<p class="my">this is content</p>         // 有效
<p class="my content">this is content</p> // 無效
<p class="mycontent">this is content</p>  // 無效
<p class="my_content">this is content</p> // 無效

[attribute^=value] 匹配屬性值以指定值開頭的元素:

div[class^="my"]{background:#FF0;}

<div class="my">this is first</div>       // 有效
<div class="my_first">this is first</div> // 有效
<div class="my-first">this is first</div> // 有效
<div class="second">this is second</div>
<div class="third_my">this is third</div>

[attribute$=value] 匹配屬性值以指定值結尾的元素:

div[class$="my"]{background:#ffff00;}

<div class="my_first">this is first</div>
<div class="second">this is second</div>
<div class="third_my">this is third</div>   // 有效
<div class="third-my">this is third</div>   // 有效
<div class="my">this is third</div>         // 有效

[attribute*=value] 匹配屬性值中包含指定值的元素:

div[class*="my"]{background:#ffff00;}

<div class="my_first">this is first</div>   // 有效
<div class="second">this is second</div>
<div class="secmyond">this is second</div> // 有效
<div class="sec-my-ond">this is second</div> // 有效
<div class="third_my">this is third</div>   // 有效
<div class="third-my">this is third</div>   // 有效
<div class="my">this is third</div>         // 有效

把 ^、$、* 三個記起來——開頭、結尾、包含——大部分日常需求都能靠屬性選擇器優雅解決,不必再寫一堆額外的 class。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved