马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一、伪类选择器
作用:根据某一元素不同的状态来进行样式声明
语法:
选择器:伪类{}
伪类分类:
1、链接伪类
:link,适用于尚未访问的链接
:visited,访问过后的链接
- <!DOCTYPE html>
- <html>
- <head>
- <title>伪类选择器</title>
- <style>
- a:link{
- color:red;
- text-decoration:none;
- font-size:20px;
- }
- a:visited{ color:black;
复制代码
2、动态伪类
:hover,适用于鼠标悬停在元素时
- <!DOCTYPE html>
- <html>
- <head>
- <title>伪类选择器</title>
- <style>
- a:hover{
- color:red;
- text-decoration:none;
- font-size:20px;
- }
- </style>
- </head>
- <body>
- <div>
- <a href="#">鼠标未悬停的链接</a>
- </div>
- </body></html>
复制代码
:active,元素被激活时
- <!DOCTYPE html>
- <html>
- <head>
- <title>伪类选择器</title>
- <style>
- a:active{
- color:green;
- text-decoration:none;
- font-size:20px;
- }
- </style>
- </head>
- <body>
- <div>
- <a href="#">未激活的链接</a>
- </div>
- </body></html>
复制代码
:focus,元素获取焦点时(多数用于文本框,密码框等表单元素)
- <!DOCTYPE html>
- <html>
- <head>
- <title>伪类选择器</title>
- <style>
- .txt{
- color:red;
- font-size:16px;
- }
- .txt:focus{
- color:green;
- font-size:25px;
- }
- </style>
- </head>
- <body>
- <div>
- <input class="txt" type="text" value="用户名"><br/><br/>
- <input class="txt" type="text" value="地址">
- </div>
- </body></html>
复制代码
3、目标伪类(target)
用于匹配 当前活动的 锚点元素
语法::target
- <!DOCTYPE html>
- <html>
- <head>
- <title> 目标伪类 </title>
- <meta charset="utf-8" />
- <style>
- .empty{
- width:200px;
- height:50px;
- border:1px solid red;
- }
- a:target{
- background:red;
- }
- div:target{
- background:gray;
- }
- </style>
- </head>
- <body>
- <p><a href="#China">跳转至中国</a></p>
- <p><a href="#America">跳转至美国</a></p>
- <p><a href="#Japan">跳转至日本</a></p>
- <a name="China">中国:地大物博!</a>
- <div class="empty"></div>
- <a name="America">美国:到处欺负人!</a>
- <div class="empty"></div>
- <div id="Japan">日本:岛国!</div> </body>
复制代码
4、元素状态伪类
应用场合:表单元素居多
:enable 匹配每个已启用的元素(大多数应用在表单元素上)
:disable 匹配每个被禁用的元素(大多数应用在表单元素上)
:checked 匹配每个已被选中的input元素(只用于单选按钮和复选框)
- <!DOCTYPE html>
- <html>
- <head>
- <title> 元素状态伪类 </title>
- <meta charset="utf-8" />
- <style>
- /*1、已启用的input元素 字体加粗*/
- input:enabled{
- font-weight:900;
- }
- /*2、被禁用的input元素 背景颜色为 红色*/
- input:disabled{
- background:red;
- }
- /*3、被选中的按钮,背景颜色为黄色*/
- input:checked{
- background:yellow;
- }
- </style>
- </head>
- <body>
- ID:<input type="text" disabled value="Wuji" /><br />
- Name:<input type="text" /><br />
- Status:
- <input type="radio" name="state" value="1" />Enable
- <input type="radio" name="state" checked value="0"/>Disable
- <br />
- </body></html>
复制代码
5、结构伪类
1.:first-child,匹配属于其父元素中的首个子元素
table tr:first-child{}
2.:last-child, 匹配属于其父元素中的最后一个子元素
table tr:last-child{}
3.:empty,匹配没有子元素(包含文本)的元素
可以匹配:<p></p>
以下两种情况匹配不出来:
<p>
<a>百度</a>
</p>
<p>百度</p>
4.  nly-child, 匹配是其父元素中的唯一子元素
<p>
<a>百度</a>
</p>
a  nly-child: 可以匹配
<p>
<a>百度</a>
<a>谷歌</a>
</p>
a:only-child:无法匹配
- <!DOCTYPE html>
- <head>
- <title> 伪类选择器</title>
- <meta charset="utf-8"/>
- <style>
- p:first-child{
- height:50px;
- width:100px;
- background-color:red;
- }
- p:last-child{
- height:50px;
- width:100px;
- background-color:green;
- }
- p:empty{
- height:50px;
- width:100px;
- background-color:yellow;
- }
- div
nly-child{
- border:1px solid black;
- width:200px;
- }
- </style>
- </head>
- <body>
- <div>
- <p>这是段落1</p>
- <p></p>
- <p>这是段落2</p>
- <p>这是最后一个段落</p>
- </div>
- </body></html>
复制代码
6.否定伪类:匹配非指定选择器的每个元素(即不是指定选择器的其他元素)
语法: :not(selector)
input:not([type=text])
tr:not(:first-child) :not(:last-child)
- <!DOCTYPE html>
- <head>
- <title> 否定选择器 </title>
- <meta charset="utf-8" />
- <style>
- input:not([type="text"]){
- font-size:16px;
- color:red;
- font-weight:bold;
- }
- </style>
- </head>
- <body>
- <input type="text"/><br/>
- <input type="button" value="普通按钮"/><br/>
- <input type="submit" value="提交"/><br/>
- </body></html>
复制代码
|