Css选择器的分类

这次实验主要熟悉css中的后代选择器:nth-child,nth-of-type ,  >, (空格), + 之类的用法:

复合选择器:

后代选择器: p span {font-size14px;} 选择p元素的所有子孙元素中的span元素
子女选择器: 
p>span {font-size14px;} 选择p元素的所有子女元素中的span元素
组合选择器: 
p#start {font-size14px;} 选择idstartp元素(交集)
群组选择器: em, .even {font-size14px;} 选择em元素或者类名为even的元素
相邻兄弟选择器: 
h1 + p {color:red;} 选择h1之后的相邻兄弟元素(必须为p元素
后续兄弟选择器: h1 ~ p {color:red;} 选择在 h1之后所有兄弟元素中的p元素

否定选择器: p:not(#start)color:red;} 选择所有id不是startp元素 

nth-child选择器(结构伪类)
p:first-child {color:red;} p的双亲的第一个子女
p:last-child {color:red;} p的双亲的最后一个子女
p:only-child {color:red;} p的双亲的唯一一个子女
p:nth-child(5) {color:red;} p的双亲的第5个子女
p:nth-child(even) color:red;} p的双亲的第偶数个子女 (奇数:odd)
p:nth-child(3n+1) {color:red;} p的双亲的选择第1、 4、 7、 ...个子女
p:nth-last-child(5) {color:red;} p的双亲的倒数第5个子女 


nth-of-type选择器(结构伪类
p:first-of-type {color:red;} p的双亲的第一个子女(只计算p元素)
p:last-of-type {color:red;} p的双亲的最后一个子女
p:only-of-type {color:red;} p的双亲的唯一一个子女
p:nth-of-type(5) {color:red;} p的双亲的第5个子女
p:nth-of-type(odd) {color:red;} p的双亲的第奇数个子女 (even,odd)
p:nth-of-type(3n) {color:red;} p的双亲的选择第3、 6、 ...个子女
p:nth-last-of-type(5) {color:red;} p的双亲的倒数第5个子女 


nth-child的其它用法: 
:nth-child(n+6) 选中从第6个开始的子元素
:nth-child(-n+9) 选中从第1个到第9个子元素
:nth-child(n+4):nth-child(-n+8) 选中第4-8个子元素
:nth-child(n+2):nth-child(-n+9):nth-child(odd) 选中的子元素是从第2位到第9位,并
且只包含奇数位。

实验CSS代码如下:


[html]  view plain  copy
  1. <style type="text/css">  
  2.         a:link{text-decoration:none;color:black;} <!--伪类-->  
  3.         li:nth-child(even) a:link{text-decoration:none;color:green;} <!-- li双亲的第偶数个子女元素  未被访问的链接 -->  
  4.         a:visited{text-decoration:none;color:black;} <!-- 访问过的链接 -->  
  5.         li:nth-child(even) a:visited{text-decoration:none;color:green;} <!-- li双亲的第偶数个子女元素中 被访问过的链接  -->  
  6.         a:hover{text-decoration:underline;color:blue;}   
  7.         li:nth-child(even) a:hover{text-decoration:underline;color:blue;}   
  8.         a:active{text-decoration:underline;color:red;}   
  9.         li:nth-child(even) a:active{text-decoration:none;color:green;}  
  10. <style>     
  11.   
  12. <style type="text/css">         
  13.         input:focus,textarea:focus{background-color:yellow;} <!--选择获得焦点的 input 或者 textarea元素 -->  
  14.         button:disabled,input:disabled{color:#CCC;} <!--禁用的button或者input元素 -->  
  15.         input:checked+span{color:red;} <!-- 被选中的input元素的相邻兄弟span-->  
  16.         textarea::selection{color:white;background-color:blue;}<!-- 选中的文本-->  
  17. <style>     
  18.       
  19. <style type="text/css">  
  20.         a{color:blue;text-decoration:underline;} <!-- -->  
  21.         li:nth-child(even) a{color:green;}<!-- li的双亲的第偶数个子女中的后代a元素-->  
  22.         li:nth-child(n+3):nth-child(-n+9):nth-child(odd) a{color:red;}<!--选中的子元素是从第3位到第9位,并且只包含奇数位 -->  
  23. <style>  
  24.   
  25. <style type="text/css">         
  26.         a:link,a:visited{color:blue;text-decoration:none;}  
  27.         a:active{color:#532301;text-decoration:none;}  
  28.         p{text-align:left;text-indent:2em;}  
  29.         p:first-letter{font-size:1.5em} <!--选择每个 <p> 元素的首字母-->  
  30.         p:first-line{font-style:italic;} <!--选择每个 <p> 元素的首行 -->  
  31.         p:hover{color:green;}   
  32.         p:nth-of-type(even){font-weight:bold;} <!--p的双亲的第偶数个子女 -->  
  33.         .ref{vertical-align:super;font-size:10px;} <!-- 选择ref类 -->  
  34.         <!-- 该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。super垂直对齐文本的上标-->  
  35. <style>  
  36.   
  37. <style type="text/css">     
  38.     p:nth-child(n+3){text-indent:2em;} <!-- 从第3个开始的子女元素-->  
  39.     p:first-letter{color:blue;font-size:1.5em;}  <!-- p元素的第一个字母 -->  
  40.     span{display:none;} <!--display属性用于定义建立布局时元素生成的显示框类型 ,none此元素不会被显示。-->  
  41.     span:first-child{display:inline;} <!-- span的双亲的第一个子女,显示为内联元素,元素前后没有换行符-->  
  42.     p:hover span{display:inline;} <!--鼠标置于上方的p元素中的子孙span元素 -->  
  43.     p:nth-child(n+3){text-align:left;} <!-- p的第3个开始的子元素-->  
  44. <style>  
  45.   
  46. <style type="text/css">     
  47.   tr:first-child > td{background-color:#DDD}<!-- tr双亲的第一个子女元素 的 子孙td元素-->  
  48.   td:nth-child(even){background-color:rgb(200,200,255);}<!-- td的双亲的第偶数个子女元素-->  
  49.   tr:nth-of-type(2) td:nth-child(-n+3){color:grey;}<!--tr双亲的第2个子女 的 子孙td元素 中的 第1个到第3个子元素 -->  
  50.   tr:nth-of-type(2) td:nth-child(n+4):nth-child(-n+7):after,tr:nth-of-type(3) td:nth-child(-n+4):after{content:"*";}   
  51.   <!-- tr双亲的第2个子女的 子孙中  的td元素第4个到第7个的子元素的内容之后插入“*”-->  
  52.   <!-- tr双亲的第3个子女的 子孙中  的td元素第4个到第7个的子元素的内容之后插入“*”-->  
  53. <style>  
  54.   
  55. <style type="text/css">     
  56.     tr:nth-of-type(odd)>td:nth-of-type(odd),tr:nth-of-type(even)>td:nth-of-type(even){background-color:rgb(200,200,200);}  
  57.     <!--tr的双亲的第奇数个子女 的后代 td元素中为基数的, 同。。 -->  
  58.   tr:nth-of-type(odd) >td:nth-of-type(even),tr:nth-of-type(even) >td:nth-of-type(odd){background-color:rgb(100,100,100);}  
  59.   tr:nth-of-type(odd)>td:nth-of-type(odd):hover,tr:nth-of-type(even)>td:nth-of-type(even):hover{background-color:rgb(200,200,240);}  
  60.   <!-- tr的双亲的第奇数个子女 的后代 td元素中为基数的 并且鼠标放置其中的-->  
  61.   tr:nth-of-type(odd) >td:nth-of-type(even):hover,tr:nth-of-type(even) >td:nth-of-type(odd):hover{background-color:rgb(100,100,160);}  
  62.   tr:nth-child(n+7) td>img:hover{border:dashed 1px yellow;margin:auto}<!-- tr的第7个以后的子女元素中的td元素 的img子女元素 hover时 -->  
  63.   tr:nth-child(-n+2) td>img:hover{border:dashed 1px red;margin:auto}  
  64. <style>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值