CSS3中的常用选择器使用示例整理

所属分类: 网页制作 / CSS 阅读数: 880
收藏 0 赞 0 分享

1. 根选择器 :root
:root{}就等同于html{}, 一般来说, 推荐使用:root{}.

CSS Code复制内容到剪贴板
  1. <style>   
  2. :root {   
  3.   background:green;   
  4. }   
  5. </style>   
  6.   
  7. <div>:root选择器的演示</div>  

2. 否定选择器 :not
否定选择器, 就是除此之外的

CSS Code复制内容到剪贴板
  1. <style>   
  2. input:not([type="submit"]) {   
  3.     border1px solid red;   
  4. }   
  5. </style>   
  6.   
  7. <form action="#">   
  8.     <div>   
  9.         <label for="name">账号:</label>   
  10.         <input type="text" name="name" id="name" placeholder="请填写账号" />   
  11.     </div>   
  12.     <div>   
  13.         <label for="password">密码:</label>   
  14.         <input type="password" name="password" id="password" placeholder="请填写密码" />   
  15.     </div>   
  16.     <div>   
  17.         <input type="submit" value="Submit" />   
  18.     </div>   
  19. </form>   

3. 空选择器 :empty
注意: :empty 只对一点内容都没有的元素生效, 哪怕有一个空格都不行.

CSS Code复制内容到剪贴板
  1. <style>   
  2. div:empty {   
  3.     border1px solid green;   
  4. }   
  5. </style>   
  6.   
  7. <div>我这里有内容</div>   
  8. <div> <!-- 我这里有一个空格 --></div>   
  9. <div></div><!-- 我这里任何内容都没有 -->  

4.目标选择器 :target
超链接地址, 与id对应

CSS Code复制内容到剪贴板
  1. <style>   
  2. .not_show{   
  3.     displaynone;   
  4. }   
  5.   
  6. #test:target{   
  7.     display:block;   
  8. }   
  9. </style>   
  10.   
  11.   
  12. <h2><a href="#test">test</a></h2>   
  13. <div class="not_show" id="test">   
  14.     这是一个测试   
  15. </div>   
  16. <style>   
  17.     #pipi:target {   
  18.       background: orange;   
  19.       color#fff;   
  20.     }   
  21.     #ruby:target {   
  22.       backgroundblue;   
  23.       color#fff;   
  24.     }   
  25.     #aaron:target {   
  26.       backgroundred;   
  27.       color#fff;   
  28.     }   
  29. </style>   
  30.   
  31. <h2><a href="#pipi">pipi</a></h2>   
  32. <div id="pipi">   
  33.     content for pipi   
  34. </div>   
  35.   
  36. <h2><a href="#ruby">ruby</a></h2>   
  37. <div id="ruby">   
  38.     content for ruby   
  39. </div>   
  40.   
  41. <h2><a href="#aaron">Brand</a></h2>   
  42. <div id="aaron">   
  43.     content for aaron   
  44. </div>  

5. 第一个与最后一个子元素 :first-child :last-child

CSS Code复制内容到剪贴板
  1. <style>   
  2. ul li:first-child a {   
  3.     color:green;   
  4. }   
  5.   
  6. ul li:last-child a {   
  7.     color:red;   
  8. }   
  9.   
  10. </style>   
  11. <ul>   
  12.   <li><a href="##">Link1</a></li>   
  13.   <li><a href="##">Link2</a></li>   
  14.   <li><a href="##">Link3</a></li>   
  15.   <li><a href="##">Link4</a></li>   
  16.   <li><a href="##">Link5</a></li>   
  17. </ul>  

6. 指定子元素选择器/奇偶选择器 :nth-child(n) :nth-last-child(n)

CSS Code复制内容到剪贴板
  1. <style>   
  2.     /*2n 偶数*/  
  3.     ul li:nth-child(2n) {   
  4.         color:green;   
  5.     }   
  6.   
  7.     /* 用关键词 odd, 表示偶数, 效果同上  
  8.     ul li:nth-child(odd) {  
  9.         color:green;  
  10.     }  
  11.     */  
  12.   
  13.     /*2n+1 奇数*/  
  14.     ul li:nth-child(2n+1) {   
  15.         color:red;   
  16.     }   
  17.   
  18.     /* 用关键词 even, 表示奇数, 效果同上  
  19.     ul li:nth-child(even) {  
  20.         color:red;  
  21.     }  
  22.     */  
  23.   
  24.     /* 指定子元素索引 */  
  25.     ul li:nth-child(5) {   
  26.         background#08c;   
  27.     }   
  28.   
  29.     /* 倒数第五个 */  
  30.     ul li:nth-last-child(5){   
  31.         backgroundyellow;   
  32.     }   
  33. </style>   
  34. <ul>   
  35.     <li>item1</li>   
  36.     <li>item2</li>   
  37.     <li>item3</li>   
  38.     <li>item4</li>   
  39.     <li>item5</li>   
  40.     <li>item6</li>   
  41.     <li>item7</li>   
  42.     <li>item8</li>   
  43.     <li>item9</li>   
  44.     <li>item10</li>   
  45. </ul>  


7. 第一个与最后一个匹配类型的子元素 first-of-type last-of-type

CSS Code复制内容到剪贴板
  1. <style>   
  2.     .wrapper > p:first-of-type {   
  3.         backgroundgreen;   
  4.     }   
  5.   
  6.     .wrapper > p:last-of-type {   
  7.         background: orange;   
  8.     }   
  9. </style>   
  10.   
  11. <div class="wrapper">   
  12.     <div>我是一个块元素,我是.wrapper的第一个子元素</div>   
  13.     <p>我是一个段落元素,我是不是.wrapper的第一个子元素,但是他的第一个段落元素</p>   
  14.     <p>我是一个段落元素</p>   
  15.     <div>我是一个块元素</div>   
  16. </div>  

8. 指定匹配类型子元素选择器/匹配类型奇偶选择器 :nth-of-type(n) :nth-last-of-type(n)

CSS Code复制内容到剪贴板
  1. <style>   
  2.     .wrapper > p:nth-of-type(2n){   
  3.         background: orange;   
  4.     }   
  5. </style>   
  6.   
  7. <div class="wrapper">   
  8.     <div>我是一个Div元素</div>   
  9.     <p>我是一个段落元素</p>   
  10.   
  11.     <div>我是一个Div元素</div>   
  12.     <p>我是一个段落</p>   
  13.   
  14.     <div>我是一个Div元素</div>   
  15.     <p>我是一个段落</p>   
  16.   
  17.     <div>我是一个Div元素</div>   
  18.     <p>我是一个段落</p>   
  19.   
  20.     <div>我是一个Div元素</div>   
  21.     <p>我是一个段落</p>   
  22.   
  23.     <div>我是一个Div元素</div>   
  24.     <p>我是一个段落</p>   
  25.   
  26.     <div>我是一个Div元素</div>   
  27.     <p>我是一个段落</p>   
  28.   
  29.     <div>我是一个Div元素</div>   
  30.     <p>我是一个段落</p>   
  31. </div>  

9. 唯一子元素选择器 only-child
匹配的元素的父元素中仅有一个子元素,而且是一个唯一的子元素

CSS Code复制内容到剪贴板
  1. <style>   
  2.     .post p:only-child {   
  3.       background: orange;   
  4.     }   
  5. </style>   
  6.   
  7. <div class="post">   
  8.     <p>我是一个段落</p>   
  9.     <p>我是一个段落</p>   
  10. </div>   
  11. <div class="post">   
  12.     <p>我是一个段落</p>   
  13. </div>  

10. 唯一匹配类型的子元素 only-of-type

CSS Code复制内容到剪贴板
  1. <style>   
  2.     .wrapper > div:only-of-type {   
  3.         background: orange;   
  4.     }   
  5. </style>   
  6.   
  7. <div class="wrapper">   
  8.     <p>我是一个段落</p>   
  9.     <p>我是一个段落</p>   
  10.     <p>我是一个段落</p>   
  11.     <div>我是一个Div元素</div>   
  12. </div>   
  13. <div class="wrapper">   
  14.     <div>我是一个Div</div>   
  15.     <ul>   
  16.         <li>我是一个列表项</li>   
  17.     </ul>   
  18.     <p>我是一个段落</p>   
  19. </div>  

11. 可用选择器 :enabled

CSS Code复制内容到剪贴板
  1. <style>   
  2.     div{   
  3.         margin20px;   
  4.     }   
  5.     input[type="text"]:enabled {   
  6.         background#ccc;   
  7.         border2px solid red;   
  8.     }   
  9. </style>   
  10.   
  11. <form action="#">   
  12.     <div>   
  13.         <label for="name">Text Input:</label>   
  14.         <input type="text" name="name" id="name" placeholder="可用输入框"  />   
  15.     </div>   
  16.     <div>   
  17.         <label for="name">Text Input:</label>   
  18.         <input type="text" name="name" id="name" placeholder="禁用输入框"  disabled="disabled" />   
  19.     </div>   
  20. </form>  

12. 不可用选择器 :disabled

CSS Code复制内容到剪贴板
  1. <style>   
  2. form {   
  3.     margin50px;   
  4. }   
  5.   
  6. div {   
  7.     margin-bottom20px;   
  8. }   
  9.   
  10. input {   
  11.     background#fff;   
  12.     padding10px;   
  13.     border1px solid orange;   
  14.     border-radius: 3px;   
  15. }   
  16.   
  17. input[type="text"]:disabled {   
  18.     background: rgba(0,0,0,.15);   
  19.     border1px solid rgba(0,0,0,.15);   
  20.     color: rgba(0,0,0,.15);   
  21. }   
  22. </style>   
  23. <form action="#">   
  24.     <div>   
  25.         <input type="text" name="name" id="name" placeholder="我是可用输入框" />   
  26.     </div>   
  27.     <div>   
  28.         <input type="text" name="name" id="name" placeholder="我是不可用输入框" disabled />   
  29.     </div>   
  30. </form>  

13. 被选中选择器 :checked

CSS Code复制内容到剪贴板
  1. <style>   
  2.     form {   
  3.       border1px solid #ccc;   
  4.       padding20px;   
  5.       width300px;   
  6.       margin30px auto;   
  7.     }   
  8.   
  9.     .wrapper {   
  10.       margin-bottom10px;   
  11.     }   
  12.   
  13.     .box {   
  14.       displayinline-block;   
  15.       width20px;   
  16.       height20px;   
  17.       margin-right10px;   
  18.       positionrelative;   
  19.       border2px solid orange;   
  20.       vertical-alignmiddle;   
  21.     }   
  22.   
  23.     .box input {   
  24.       opacity: 0;   
  25.       positon: absolute;   
  26.       top:0;   
  27.       left:0;   
  28.     }   
  29.   
  30.     .box span {   
  31.       positionabsolute;   
  32.       top: -10px;   
  33.       rightright3px;   
  34.       font-size30px;   
  35.       font-weightbold;   
  36.       font-familyArial;   
  37.       -webkit-transform: rotate(30deg);   
  38.       transform: rotate(30deg);   
  39.       color: orange;   
  40.     }   
  41.   
  42.     input[type="checkbox"] + span {   
  43.       opacity: 0;   
  44.     }   
  45.   
  46.     input[type="checkbox"]:checked + span {   
  47.       opacity: 1;   
  48.     }   
  49. </style>   
  50.   
  51. <form action="#">   
  52.     <div class="wrapper">   
  53.         <div class="box">   
  54.           <input type="checkbox" checked="checked" id="usename" /><span>√</span>   
  55.         </div>   
  56.         <lable for="usename">我是选中状态</lable>   
  57.     </div>   
  58.   
  59.     <div class="wrapper">   
  60.         <div class="box">   
  61.           <input type="checkbox"  id="usepwd" /><span>√</span>   
  62.         </div>   
  63.         <label for="usepwd">我是未选中状态</label>   
  64.     </div>   
  65. </form>  

14. 被鼠标选中, 高亮选择器 ::selection

CSS Code复制内容到剪贴板
  1. <style>   
  2. ::-moz-selection {   
  3.     backgroundred;   
  4.     colorgreen;   
  5. }   
  6. ::selection {   
  7.     backgroundred;   
  8.     colorgreen;   
  9. }   
  10. </style>   
  11. <p>拿鼠标选中我, 试试看!</p>  

15. 只读选择器 :read-only

CSS Code复制内容到剪贴板
  1. <style>   
  2. form {   
  3.     width300px;   
  4.     padding10px;   
  5.     border1px solid #ccc;   
  6.     margin50px auto;   
  7. }   
  8. form > div {   
  9.     margin-bottom10px;   
  10. }   
  11.   
  12. input[type="text"]{   
  13.     border1px solid orange;   
  14.     padding5px;   
  15.     background#fff;   
  16.     border-radius: 5px;   
  17. }   
  18.   
  19. input[type="text"]:-moz-read-only{   
  20.     border-color#ccc;   
  21. }   
  22. input[type="text"]:read-only{   
  23.     border-color#ccc;   
  24. }   
  25. </style>   
  26.   
  27. <form action="#">   
  28.     <div>   
  29.         <label for="name">姓名:</label>   
  30.         <input type="text" name="name" id="name" placeholder="大漠" />   
  31.     </div>   
  32.     <div>   
  33.         <label for="address">地址:</label>   
  34.         <input type="text" name="address" id="address" value="中国上海" readonly />   
  35.     </div>   
  36. </form>  

16. 非只读选择器 :read-write

CSS Code复制内容到剪贴板
  1. <style>   
  2. form {   
  3.     width300px;   
  4.     padding10px;   
  5.     border1px solid #ccc;   
  6.     margin50px auto;   
  7. }   
  8. form > div {   
  9.     margin-bottom10px;   
  10. }   
  11.   
  12. input[type="text"]{   
  13.     border1px solid orange;   
  14.     padding5px;   
  15.     background#fff;   
  16.     border-radius: 5px;   
  17. }   
  18.   
  19. input[type="text"]:-moz-read-only{   
  20.     border-color#ccc;   
  21. }   
  22. input[type="text"]:read-only{   
  23.     border-color#ccc;   
  24. }   
  25.   
  26. input[type="text"]:-moz-read-write{   
  27.     border-color#f36;   
  28. }   
  29. input[type="text"]:read-write{   
  30.     border-color#f36;   
  31. }   
  32. </style>   
  33.   
  34. <form action="#">   
  35.     <div>   
  36.         <label for="name">姓名:</label>   
  37.         <input type="text" name="name" id="name" placeholder="大漠" />   
  38.     </div>   
  39.     <div>   
  40.         <label for="address">地址:</label>   
  41.         <input type="text" name="address" id="address" placeholder="中国上海" readonly="readonly" />   
  42.     </div>   
  43. </form>  
更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多