錨偽類
在支持 CSS 的瀏覽器中,鏈接的不同狀態(tài)都可以不同的方式顯示,這些狀態(tài)包括:活動狀態(tài),已被訪問狀態(tài),未被訪問狀態(tài),和鼠標(biāo)懸停狀態(tài)。
a:link
{color: #FF0000} /* 未訪問的鏈接 */ a:visited
{color: #00FF00} /* 已訪問的鏈接 */ a:hover
{color: #FF00FF} /* 鼠標(biāo)移動到鏈接上 */ 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.asp">CSS Syntax</a>
假如上面的例子中的鏈接被訪問過,那么它將顯示為紅色。
CSS2 - :first-child 偽類
您可以使用 :first-child 偽類來選擇元素的第一個子元素。這個特定偽類很容易遭到誤解,所以有必要舉例來說明。考慮以下標(biāo)記:
<div> <p>These are the necessary steps:</p>
<ul> <li>Intert Key</li>
<li>Turn key <strong>clockwise</strong>
</li> <li>Push accelerator</li> </ul> <p>Do <em>not</em>
push the brake at the same time as the accelerator.</p> </div>
在上面的例子中,作為第一個元素的元素包括第一個 p、第一個 li 和 strong 和 em 元素。
給定以下規(guī)則:
p:first-child {font-weight: bold;} li:first-child {text-transform:uppercase;}
第一個規(guī)則將作為某元素第一個子元素的所有 p 元素設(shè)置為粗體。第二個規(guī)則將作為某個元素(在 HTML 中,這肯定是 ol 或 ul 元素)第一個子元素的所有 li 元素變成大寫。
請訪問該鏈接,來查看這個 :first-child 實例的效果。
提示:最常見的錯誤是認(rèn)為 p:first-child 之類的選擇器會選擇 p 元素的第一個子元素。
注釋:必須聲明 <!DOCTYPE>,這樣 :first-child 才能在 IE 中生效。
為了使您更透徹地理解 :first-child 偽類,我們另外提供了 3 個例子:
例子 1 - 匹配第一個 <p> 元素
在下面的例子中,選擇器匹配作為任何元素的第一個子元素的 p 元素:
<html> <head> <style type="text/css"> p:first-child
{ color: red; } </style> </head> <body> <p>some text</p>
<p>some text</p> </body> </html>
TIY
例子 2 - 匹配所有 <p> 元素中的第一個 <i> 元素
在下面的例子中,選擇器匹配所有 <p> 元素中的第一個 <i> 元素:
<html> <head> <style type="text/css"> p > i:first-child
{ font-weight:bold; } </style> </head> <body> <p>some <i>text</i>
. some <i>text</i>.</p> <p>some <i>text</i>
. some <i>text</i>.</p> </body> </html>
TIY
例子 3 - 匹配所有作為第一個子元素的 <p> 元素中的所有 <i> 元素
在下面的例子中,選擇器匹配所有作為元素的第一個子元素的 <p> 元素中的所有 <i> 元素:
<html> <head> <style type="text/css"> p:first-child i
{ color:blue; } </style> </head> <body> <p>some <i>text</i>
. some <i>text</i>
.</p> <p>some <i>text</i>. some <i>text</i>.</p> </body> </html>
TIY
CSS2 - :lang 偽類
:lang 偽類使你有能力為不同的語言定義特殊的規(guī)則。在下面的例子中,:lang 類為屬性值為 no 的 q 元素定義引號的類型:
<html> <head> <style type="text/css"> q:lang(no) { quotes: "~" "~" }
</style> </head> <body> <p>文字<q lang="no"
>段落中的引用的文字</q>文字</p> </body></html>