阅读(1920)

CSS 伪类

最后一次修改 2017年08月03日

CSS 伪类(Pseudo-classes)


CSS伪类是用来添加一些选择器的特殊效果。


语法

伪类的语法:

selector:pseudo-class {property:value;}

CSS类也可以使用伪类:

selector.class:pseudo-class {property:value;}


anchor伪类

在支持 CSS 的浏览器中,链接的不同状态都可以以不同的方式显示

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style>
a:link {color:#FF0000;}    /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;}   /* mouse over link */
a:active {color:#0000FF;}  /* selected link */
</style>
</head>

<body>
<p><b><a href="/css/" target="_blank">这是一个链接</a></b></p>
<p><b>注意:</b> a:hover 必须在 a:link 和 a:visited 之后,需要严格按顺序才能看到效果。</p>
<p><b>注意:</b> a:active 必须在 a:hover 之后。</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style>
p:first-child
{
	color:blue;
} 
</style>
</head>

<body>
<p>This is some text.</p>
<p>This is some text.</p>
<p><b>注意:</b>对于 :first-child 工作于 IE8以及更早版本的浏览器, DOCTYPE必须已经声明.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


匹配所有<p> 元素中的第一个 <i> 元素

在下面的例子中,选择相匹配的所有<p>元素的第一个 <i>元素:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当 :first-child 作用于 IE8以及更早版本的浏览器,  DOCTYPE必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


匹配所有作为第一个子元素的<p> 元素中的所有 <i> 元素

在下面的例子中,选择器匹配所有作为元素的第一个子元素的<p> 元素中的所有 <i> 元素:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当 :first-child 作用于 IE8以及更早版本的浏览器,  DOCTYPE必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


CSS - :lang 伪类

:lang 伪类使你有能力为不同的语言定义特殊的规则

注意:IE8必须声明<!DOCTYPE>才能支持;lang伪类。

在下面的例子中,:lang 类为属性值为 no 的q元素定义引号的类型:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当:first-child 作用于 IE8及更早版本的浏览器, DOCTYPE 必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



更多实例

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>W3Cschool教程(w3cschool.cn)</title> 
<style>
a.one:link {color:#ff0000;}
a.one:visited {color:#0000ff;}
a.one:hover {color:#ffcc00;}

a.two:link {color:#ff0000;}
a.two:visited {color:#0000ff;}
a.two:hover {font-size:150%;}

a.three:link {color:#ff0000;}
a.three:visited {color:#0000ff;}
a.three:hover {background:#66ff66;}

a.four:link {color:#ff0000;}
a.four:visited {color:#0000ff;}
a.four:hover {font-family:monospace;}

a.five:link {color:#ff0000;text-decoration:none;}
a.five:visited {color:#0000ff;text-decoration:none;}
a.five:hover {text-decoration:underline;}
</style>
</head>

<body>
<p>将鼠标移至链接上改变样式.</p>

<p><b><a class="one" href="/css/" target="_blank">This link changes color</a></b></p>
<p><b><a class="two" href="/css/" target="_blank">This link changes font-size</a></b></p>
<p><b><a class="three" href="/css/" target="_blank">This link changes background-color</a></b></p>
<p><b><a class="four" href="/css/" target="_blank">This link changes font-family</a></b></p>
<p><b><a class="five" href="/css/" target="_blank">This link changes text-decoration</a></b></p>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

这个例子演示了如何为超链接添加其他样式。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>W3Cschool教程(w3cschool.cn)</title> 
<style>
input:focus
{
	background-color:yellow;
}
</style>
</head>

<body>
<form action="demo-form" method="get">
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" /><br>
<input type="submit" value="Submit" />
</form>

<p><b>注意:</b>仅当 !DOCTYPE已经声明时 IE8支持 :focus.</p>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

这个例子演示了如何使用 :focus伪类。


所有CSS伪类/元素

选择器 示例 示例说明
: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 Insert content before every <p> element
:after p:after 在每个<p>元素之后插入内容
:lang(language) p:lang(it) 为<p>元素的lang属性选择一个开始值

CSS 伪类(Pseudo-classes)


CSS伪类是用来添加一些选择器的特殊效果。


语法

伪类的语法:

selector:pseudo-class {property:value;}

CSS类也可以使用伪类:

selector.class:pseudo-class {property:value;}


anchor伪类

在支持 CSS 的浏览器中,链接的不同状态都可以以不同的方式显示

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style>
a:link {color:#FF0000;}    /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;}   /* mouse over link */
a:active {color:#0000FF;}  /* selected link */
</style>
</head>

<body>
<p><b><a href="/css/" target="_blank">这是一个链接</a></b></p>
<p><b>注意:</b> a:hover 必须在 a:link 和 a:visited 之后,需要严格按顺序才能看到效果。</p>
<p><b>注意:</b> a:active 必须在 a:hover 之后。</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style>
p:first-child
{
	color:blue;
} 
</style>
</head>

<body>
<p>This is some text.</p>
<p>This is some text.</p>
<p><b>注意:</b>对于 :first-child 工作于 IE8以及更早版本的浏览器, DOCTYPE必须已经声明.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


匹配所有<p> 元素中的第一个 <i> 元素

在下面的例子中,选择相匹配的所有<p>元素的第一个 <i>元素:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当 :first-child 作用于 IE8以及更早版本的浏览器,  DOCTYPE必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


匹配所有作为第一个子元素的<p> 元素中的所有 <i> 元素

在下面的例子中,选择器匹配所有作为元素的第一个子元素的<p> 元素中的所有 <i> 元素:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当 :first-child 作用于 IE8以及更早版本的浏览器,  DOCTYPE必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


CSS - :lang 伪类

:lang 伪类使你有能力为不同的语言定义特殊的规则

注意:IE8必须声明<!DOCTYPE>才能支持;lang伪类。

在下面的例子中,:lang 类为属性值为 no 的q元素定义引号的类型:

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<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>
<p><b>注意:</b> 当:first-child 作用于 IE8及更早版本的浏览器, DOCTYPE 必须已经定义.</p>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



更多实例

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>W3Cschool教程(w3cschool.cn)</title> 
<style>
a.one:link {color:#ff0000;}
a.one:visited {color:#0000ff;}
a.one:hover {color:#ffcc00;}

a.two:link {color:#ff0000;}
a.two:visited {color:#0000ff;}
a.two:hover {font-size:150%;}

a.three:link {color:#ff0000;}
a.three:visited {color:#0000ff;}
a.three:hover {background:#66ff66;}

a.four:link {color:#ff0000;}
a.four:visited {color:#0000ff;}
a.four:hover {font-family:monospace;}

a.five:link {color:#ff0000;text-decoration:none;}
a.five:visited {color:#0000ff;text-decoration:none;}
a.five:hover {text-decoration:underline;}
</style>
</head>

<body>
<p>将鼠标移至链接上改变样式.</p>

<p><b><a class="one" href="/css/" target="_blank">This link changes color</a></b></p>
<p><b><a class="two" href="/css/" target="_blank">This link changes font-size</a></b></p>
<p><b><a class="three" href="/css/" target="_blank">This link changes background-color</a></b></p>
<p><b><a class="four" href="/css/" target="_blank">This link changes font-family</a></b></p>
<p><b><a class="five" href="/css/" target="_blank">This link changes text-decoration</a></b></p>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

这个例子演示了如何为超链接添加其他样式。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>W3Cschool教程(w3cschool.cn)</title> 
<style>
input:focus
{
	background-color:yellow;
}
</style>
</head>

<body>
<form action="demo-form" method="get">
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" /><br>
<input type="submit" value="Submit" />
</form>

<p><b>注意:</b>仅当 !DOCTYPE已经声明时 IE8支持 :focus.</p>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

这个例子演示了如何使用 :focus伪类。


所有CSS伪类/元素

选择器 示例 示例说明
: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 Insert content before every <p> element
:after p:after 在每个<p>元素之后插入内容
:lang(language) p:lang(it) 为<p>元素的lang属性选择一个开始值