CSS基础之伪类

35 篇文章 0 订阅
24 篇文章 0 订阅

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

伪类/伪元素属性

选择器示例示例说明
:linka:link选择所有未访问链接
:visiteda:visited选择所有访问过的链接
:activea:active选择正在活动链接
:hovera:hover把鼠标放在链接上的状态
:focusinput:focus选择元素输入后具有焦点
:first-letterp:first-letter选择每个p 元素的第一个字母
:first-linep:first-line选择每个p 元素的第一行
:first-childp:first-child 选择器匹配属于任意元素的第一个子元素的 p元素
:beforep:before在每个p元素之前插入内容
:afterp:after在每个p元素之后插入内容
:lang(language)p:lang(it)为p元素的lang属性选择一个开始值

语法

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

代码示例

anchor伪类

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<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 */

a.red:visited {color:#FF0000;}  
</style>
</head>

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

:first-child 伪类

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<style>
/* 选择器匹配作为任何元素的第一个子元素的 p 元素 
p:first-child 
{
	color:blue;
}
*/
/*选择相匹配的所有<p>元素的第一个 <i>元素
p > i:first-child
{
color:blue;javascript:void(0);
} 
*/

/* 选择器匹配所有作为元素的第一个子元素的<p> 元素中的所有 <i> 元素*/
 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>

:lang伪类

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<style>
q:lang(no)
{
	quotes: "~" "~";
}
</style>
</head>

<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
<p>在这个例子中,:lang定义了q元素的值为lang =“no”</p>
<p><b>注意:</b> 仅当 !DOCTYPE已经声明时 IE8支持 :lang.</p>
</body>
</html>

:focus伪类

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<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>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值