CSS 偽類
CSS偽類是用來添加一些選擇器的特殊效果。
語法
偽類的語法:
selector:pseudo-class {property:value;}
CSS類也可以使用偽類:
selector.class:pseudo-class {property:value;}
anchor偽類
在支持CSS 的瀏覽器中,鏈接的不同狀態都可以以不同的方式顯示
實例
a:link {color:#FF0000;} /* 未访问的链接 */
a:visited {color:#00FF00;} /* 已访问的链接 */
a:hover {color:#FF00FF;} /* 鼠标划过链接 */
a:active {color:#0000FF;} /* 已选中的链接 */
a:visited {color:#00FF00;} /* 已访问的链接 */
a:hover {color:#FF00FF;} /* 鼠标划过链接 */
a:active {color:#0000FF;} /* 已选中的链接 */
嘗試一下»
注意:在CSS定義中,a:hover必須被置於a:link和a:visited之後,才是有效的。
注意:在CSS定義中,a:active必須被置於a:hover之後,才是有效的。
注意:偽類的名稱不區分大小寫。
偽類和CSS類
偽類可以與CSS 類配合使用:
a.red:visited {color:#FF0000;}
<a class="red" href="css-syntax.html">CSS Syntax</a>
<a class="red" href="css-syntax.html">CSS Syntax</a>
如果在上面的例子的鏈接已被訪問,它會顯示為紅色。
CSS - :first - child偽類
您可以使用:first-child 偽類來選擇元素的第一個子元素
注意:在IE8的之前版本必須聲明<!DOCTYPE> ,這樣:first-child才能生效。
匹配第一個<p> 元素
在下面的例子中,選擇器匹配作為任何元素的第一個子元素的<p> 元素:
實例
<html>
<head>
<style>
p:first-child
{
color:blue;
}
</style>
</head>
<body>
<p>I am a strong man.</p>
<p>I am a strong man.</p>
</body>
</html>
<head>
<style>
p:first-child
{
color:blue;
}
</style>
</head>
<body>
<p>I am a strong man.</p>
<p>I am a strong man.</p>
</body>
</html>
嘗試一下»
匹配所有<p> 元素中的第一個<i> 元素
在下面的例子中,選擇相匹配的所有<p>元素的第一<i>元素:
實例
<html>
<head>
<style>
p > i:first-child
{
color:blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
</body>
</html>
<head>
<style>
p > i:first-child
{
color:blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
</body>
</html>
嘗試一下»
<h2匹配所有作為第一個子元素的<p>元素中的所有<i>元素:
實例
<html>
<head>
<style>
p:first-child i
{
color:blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
</body>
</html>
<head>
<style>
p:first-child i
{
color:blue;
}
</style>
</head>
<body>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
<p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
</body>
</html>
嘗試一下»
CSS - :lang 偽類
:lang 偽類使你有能力為不同的語言定義特殊的規則
注意: IE8必須聲明<!DOCTYPE>才能支持;lang偽類。
在下面的例子中,:lang 類為屬性值為no 的q元素定義引號的類型:
實例
<html>
<head>
<style>
q:lang(no) {quotes: "~" "~";}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
</body>
</html>
<head>
<style>
q:lang(no) {quotes: "~" "~";}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
</body>
</html>
嘗試一下»

更多實例
為超鏈接添加不同樣式
這個例子演示瞭如何為超鏈接添加其他樣式。
使用:focus
這個例子演示瞭如何使用:focus偽類。
所有CSS偽類/元素
選擇器 | 示例 | 示例說明 |
---|---|---|
:checked | input:checked | 選擇所有選中的表單元素 |
:disabled | input:disabled | 選擇所有禁用的表單元素 |
:empty | p:empty | 選擇所有沒有子元素的p元素 |
:enabled | input:enabled | 選擇所有啟用的表單元素 |
:first-of-type | p:first-of-type | 選擇每個父元素是p元素的第一個p子元素 |
:in-range | input:in-range | 選擇元素指定範圍內的值 |
:invalid | input:invalid | 選擇所有無效的元素 |
:last-child | p:last-child | 選擇所有p元素的最後一個子元素 |
:last-of-type | p:last-of-type | 選擇每個p元素是其母元素的最後一個p元素 |
:not(selector) | :not(p) | 選擇所有p以外的元素 |
:nth-child(n) | p:nth-child(2) | 選擇所有p元素的第二個子元素 |
:nth-last-child(n) | p:nth-last-child(2) | 選擇所有p元素倒數的第二個子元素 |
:nth-last-of-type(n) | p:nth-last-of-type(2) | 選擇所有p元素倒數的第二個為p的子元素 |
:nth-of-type(n) | p:nth-of-type(2) | 選擇所有p元素第二個為p的子元素 |
:only-of-type | p:only-of-type | 選擇所有僅有一個子元素為p的元素 |
:only-child | p:only-child | 選擇所有僅有一個子元素的p元素 |
:optional | input:optional | 選擇沒有"required"的元素屬性 |
:out-of-range | input:out-of-range | 選擇指定範圍以外的值的元素屬性 |
:read-only | input:read-only | 選擇只讀屬性的元素屬性 |
:read-write | input:read-write | 選擇沒有隻讀屬性的元素屬性 |
:required | input:required | 選擇有"required"屬性指定的元素屬性 |
:root | root | 選擇文檔的根元素 |
:target | #news:target | 選擇當前活動#news元素(點擊URL包含錨的名字) |
:valid | input:valid | 選擇所有有效值的屬性 |
:link | a:link | 選擇所有未訪問鏈接 |
:visited | a:visited | 選擇所有訪問過的鏈接 |
:active | a:active | 選擇正在活動鏈接 |
:hover | a:hover | 把鼠標放在鏈接上的狀態 |
:focus | input:focus | 選擇元素輸入後具有焦點 |
:first-letter | p:first-letter | 選擇每個<p> 元素的第一個字母 |
:first-line | p:first-line | 選擇每個<p> 元素的第一行 |
:first-child | p:first-child | 選擇器匹配屬於任意元素的第一個子元素的<]p> 元素 |
:before | p:before | 在每個<p>元素之前插入內容 |
:after | p:after | 在每個<p>元素之後插入內容 |
:lang(language) | p:lang(it) | 為<p>元素的lang屬性選擇一個開始值 |