css3伪类选择器使用总结

伪类选择器分为结构性、子元素、 UI、动态和其它伪类选择器

备注:为了更好的区分伪类和伪元素,书写上CSS做了区分。例如,伪类 :first-child;伪元素 ::after

大部分伪类选择器如下图所示




:root返回<html>


在下面的示例中:
div > p:only-child{
    color:red;             //不变红
}
div > p:only-of-type{
    color:red;             //变红
}
<div>
   <p>test1</p>
   <span>test2</span>
</div>


ul > li:nth-child(2){
    color:red;           //选择该元素所有子元素的第二个子元素
}
ul > li:nth-last-child(2){
    color:red;           //选择该元素所有子元素的倒数第二个子元素
}
ul > li:nth-of-type(2){
    color:red;           //选择该元素所有该类型子元素的第二个子元素
}
ul > li:nth-last-of-type(2){
    color:red;           //选择该元素所有该类型子元素的倒数第二个子元素
}


:target{
    color:red;           //定位到锚点,选择此元素
}
//锚点的定位:
首先在HTML元素中定义一个ID属性值,例如<p id="test">asd</p>
然后在浏览器地址栏,在URL最后加上#test,便可以定位到该锚点了。
锚点的使用:
可以用来将一篇很长的文章分段,
eg.<a href="#02">跳转到</a>
<p id="02">……</p>
其实锚点只需name就可以可,加id是为了让它兼容性更好


所谓UI选择器:就是指定的样式只有当元素处于某种状态下时,才起作用,在默认状态下不起作用!

浏览器兼容性:

E:hover                 支持firefox、safari、Opera、ie8、chrome            ------------
E:active                 支持firefox、safari、Opera、chrome                      不支持ie8
E:focus                 支持firefox、safari、Opera、ie8、chrome            -------------
E:enabled             支持firefox、safari、Opera、chrome                    不支持ie8
E:disabled            支持firefox、safari、Opera、chrome                    不支持ie8
E:read-only          支持firefox、Opera                             不支持ie8、safari、chrome
E:read-write         支持firefox、Opera                             不支持ie8、safari、chrome
E:checked           支持firefox、safari、Opera、chrome                    不支持ie8
E::selection           支持firefox、safari、Opera、chrome                  不支持ie8
E:default              只支持firefox                                                          ------------
E:indeterminate    只支持chrome                                                      ------------
E:invalid               支持firefox、safari、Opera、chrome                 不支持ie8
E:valid                  支持firefox、safari、Opera、chrome                  不支持ie8
E:required            支持firefox、safari、Opera、chrome                  不支持ie8
E:optional             支持firefox、safari、Opera、chrome                 不支持ie8
E:in-range            支持firefox、safari、Opera、chrome                 不支持ie8
E:out-of-rang        支持firefox、safari、Opera、chrome                 不支持ie8
下面就其使用做详细的说明;

1、选择器E:hover、E:active和E:focus
  1)、E:hover选择器被用来指定当鼠标指针移动到元素上时元素所使用的样式
 使用方法:
 <元素>:hover{
 CSS样式
 }
 我们可以在“<元素>”中添加元素的type属性。
 例:
 input[type="text"]:hover{
 CSS样式
 }
 2)、E:active选择器被用来指定元素被激活时使用的样式
 3)、E:focus选择器被用来指定元素获得光标聚焦点使用的样式,主要是在文本框控件获得聚焦点并进行文字输入时使用。

例如:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>选择器E:hover、E:active和E:focus</title>  
  6.     <style>  
  7.         input[type="text"]:hover{  
  8.             background: green;  
  9.         }  
  10.         input[type="text"]:focus{  
  11.             background: #ff6600;  
  12.             color: #fff;  
  13.         }  
  14.         input[type="text"]:active{  
  15.             background: blue;  
  16.         }  
  17.         input[type="password"]:hover{  
  18.             background: red;  
  19.         }  
  20.     </style>  
  21. </head>  
  22. <body>  
  23. <h1>选择器E:hover、E:active和E:focus</h1>  
  24. <form>  
  25.     姓名:<input type="text" placeholder="请输入姓名">  
  26.     <br/>  
  27.     <br/>  
  28.     密码:<input type="password" placeholder="请输入密码">  
  29. </form>  
  30. </body>  
  31. </html>  
2、E:enabled伪类选择器与E:disabled伪类选择器
 1)、E:enabled选择器被用来指定当元素处于可用状态时的样式。
 2)、E:disabled选择器被用来指定当元素处于不可用状态时的样式。

例如:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>E:enabled伪类选择器与E:disabled伪类选择器</title>  
  6.     <style>  
  7.         input[type="text"]:enabled{  
  8.             background: green;  
  9.             color: #ffffff;  
  10.         }  
  11.         input[type="text"]:disabled{  
  12.             background: #727272;  
  13.         }  
  14.     </style>  
  15. </head>  
  16. <body>  
  17. <h1>E:enabled伪类选择器与E:disabled伪类选择器</h1>  
  18. <form>  
  19.     姓名:<input type="text" placeholder="请输入姓名" disabled>  
  20.     <br/>  
  21.     <br/>  
  22.     学校:<input type="text" placeholder="请输入学校">  
  23. </form>  
  24. </body>  
  25. </html>  
3、E:read-only伪类选择器与E:read-write伪类选择器
 1)、E:read-only选择器被用来指定当元素处于只读状态时的样式。
 2)、E:read-write选择器被用来指定当元素处于非只读状态时的样式。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>read-only伪类选择器与E:read-write伪类选择器</title>  
  6.     <style>  
  7.         input[type="text"]:read-only{  
  8.             background: #000;  
  9.             color: green;  
  10.         }  
  11.         input[type="text"]:read-write{  
  12.             color: #ff6600;  
  13.         }  
  14.     </style>  
  15. </head>  
  16. <body>  
  17. <h1>read-only伪类选择器与E:read-write伪类选择器</h1>  
  18. <form>  
  19.     姓名:<input type="text" placeholder="请输入姓名" value="winson" readonly>  
  20.     <br/>  
  21.     <br/>  
  22.     学校:<input type="text" placeholder="请输入学校">  
  23. </form>  
  24. </body>  
  25. </html>  
4、伪类选择器E:checked、E:default和indeterminate
 1)、E:cehcked伪类选择器用来指定当表单中的radio单选框或者是checkbox复选框处于选取状态时的样式。
 2)、E:default选择器用来指定当页面打开时默认处于选取状态的单选框或复选框的控件的样式。
 3)、E:indeterminate选择器用来指定当页面打开时,一组单选框中没有任何一个单选框被设定为选中状态时,整组单选框的样式。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>checked伪类选择器</title>  
  6.     <style>  
  7.         input[type="checkbox"]:checked{  
  8.             outline: 2px solid green;  
  9.         }  
  10.     </style>  
  11. </head>  
  12. <body>  
  13. <h1>checked伪类选择器</h1>  
  14. <form>  
  15.     房屋状态:  
  16.     <input type="checkbox">水  
  17.     <input type="checkbox">电  
  18.     <input type="checkbox">天然气  
  19.     <input type="checkbox">宽带  
  20. </form>  
  21. </body>  
  22. </html>  
默认的选择项

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>default伪类选择器</title>  
  6.     <style>  
  7.         input[type="checkbox"]:default{  
  8.             outline: 2px solid green;  
  9.         }  
  10.     </style>  
  11. </head>  
  12. <body>  
  13. <h1>default伪类选择器</h1>  
  14. <form>  
  15.     房屋状态:  
  16.     <input type="checkbox" checked>水  
  17.     <input type="checkbox">电  
  18.     <input type="checkbox">天然气  
  19.     <input type="checkbox">宽带  
  20. </form>  
  21. </body>  
  22. </html>  
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <h1 style="color: rgb(0, 0, 0); font-family: Simsun; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;">indeterminate伪类选择器</h1><!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>indeterminate伪类选择器</title>  
  6.     <style>  
  7.         input[type="radio"]:indeterminate{  
  8.             outline: 2px solid green;  
  9.         }  
  10.     </style>  
  11. </head>  
  12. <body>  
  13. <h1>indeterminate伪类选择器</h1>  
  14. <form>  
  15.     性别:  
  16.     <input type="radio">男  
  17.     <input type="radio">女  
  18. </form>  
  19. </body>  
  20. </html>  
5、伪类选择器E::selection
 1)、E:selection伪类选择器用来指定当元素处于选中状态时的样式。

例如

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>伪类选择器E::selection</title>  
  6.     <style>  
  7.         ::selection{  
  8.             background: green;  
  9.             color: #ffffff;  
  10.         }  
  11.         input[type="text"]::selection{  
  12.             background: #ff6600;  
  13.             color: #ffffff;  
  14.         }  
  15.     </style>  
  16. </head>  
  17. <body>  
  18. <h1>伪类选择器E::selection</h1>  
  19. <p>今天,开发搜索框,出现了bug,现在没有找到原因!今天,开发搜索框,出现了bug,现在没有找到原因!今天,开发搜索框,出现了bug,现在没有找到原因!今天,开发搜索框,出现了bug,现在没有找到原因!今天,开发搜索框,出现了bug,现在没有找到原因!</p>  
  20. <input type="text" placeholder="文本">  
  21. </body>  
  22. </html>  

6、E:invalid伪类选择器与E:valid伪类选择器
 1)、E:invalid伪类选择器用来指定,当元素内容不能通过HTML5通过使用的元素的诸如requirde等属性所指定的检查或元素内容不符合元素规定的格式时的样式。
 2)、E:valid伪类选择器用来指定,当元素内容能通过HTML5通过使用的元素的诸如requirde等属性所指定的检查或元素内容符合元素规定的格式时的样式。

例如

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>E:invalid伪类选择器与E:valid伪类选择器</title>  
  6.     <style>  
  7.         input[type="email"]:invalid{  
  8.             color: red;  
  9.         }  
  10.         input[type="email"]:valid{  
  11.             color: green;  
  12.         }  
  13.     </style>  
  14. </head>  
  15. <body>  
  16. <h1>E:invalid伪类选择器与E:valid伪类选择器</h1>  
  17. <form>  
  18.     <input type="email" placeholder="请输入邮箱">  
  19. </form>  
  20. </body>  
  21. </html>  

7、E:required伪类选择器与E:optional伪类选择器
 1)、E:required伪类选择器用来指定允许使用required属性,而且已经指定了required属性的input元素、select元素以及textarea元素的样式。
 2)、E:optional伪类选择器用来指定允许使用required属性,而且未指定了required属性的input元素、select元素以及textarea元素的样式。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>E:required伪类选择器与E:optional伪类选择器</title>  
  6.     <style>  
  7.     input[type="text"]:required{  
  8.         background: red;  
  9.         color: #ffffff;  
  10.     }  
  11.         input[type="text"]:optional{  
  12.             background: green;  
  13.             color: #ffffff;  
  14.         }  
  15.     </style>  
  16. </head>  
  17. <body>  
  18. <h1>E:required伪类选择器与E:optional伪类选择器</h1>  
  19. <form>  
  20.     姓名:<input type="text" placeholder="请输入姓名" required>  
  21.     <br/>  
  22.     <br/>  
  23.     学校:<input type="text" placeholder="请输入学校">  
  24. </form>  
  25. </body>  
  26. </html>  

8、E:in-range伪类选择器与E:out-of-range伪类选择器
 1)、E:in-range伪类选择器用来指定当元素的有效值被限定在一段范围之内,且实际的输入值在该范围之内时的样式。
 2)、E:out-of-range伪类选择器用来指定当元素的有效值被限定在一段范围之内,但实际输入值在超过时使用的样式。

例如

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title>E:in-range伪类选择器与E:out-of-range伪类选择器</title>  
  6.     <style>  
  7.         input[type="number"]:in-range{  
  8.             color: #ffffff;  
  9.             background: green;  
  10.   
  11.         }  
  12.         input[type="number"]:out-of-range{  
  13.             background: red;  
  14.             color: #ffffff;  
  15.         }  
  16.     </style>  
  17. </head>  
  18. <body>  
  19. <h1>E:in-range伪类选择器与E:out-of-range伪类选择器</h1>  
  20. <input type="number" min="0" max="100" value="0">  
  21. </body>  
  22. </html>  
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值