幻想编程

CSS 伪类

时间:14-12-25 12:02:53点击:375

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;} /* 已选中的链接 */

注意: 在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>

如果在上面的例子的链接已被访问,它会显示为红色。

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>

匹配所有<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>

<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>