CSS3选择器
选择器事例结构

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .wrap{ font-size: 30px; } li{ width: 100px; height: 100px; text-align: center; font-size: 50px; line-height: 100px; color: white; background: black; border-radius: 50%; list-style: none; float: left; } .ul1,.ul2{overflow: hidden;} .wrap .other{width: 100%;border-radius: 0;background: transparent;} </style> </head> <body> <div class="wrap"> <ul class="ul2"> <li class="one">1</li> <li class="two">2</li> <li class="three">3</li> <li class="four">4</li> <li class="five">5</li> <li class="six">6</li> <li class="seven">7</li> <li class="eight">8</li> <li class="nine">9</li> <li class="ten">10</li> <li class="other"> <ul class="ul2"> <li>11</li> <li>12</li> </ul> </li> </ul> </div> </body> </html>
|
E~F通用选择器

E~F:选择在E元素之后的同级F元素
1 2 3
| .three ~ li{ background: blue; }
|
属性选择器

E[attr^=”val”]:选择属性attr的值以”val”开头的元素
1 2 3
| li[class^=t]{ background: blue; }
|

E[attr$=”val”]:选择属性attr的值以”val”结尾的元素

1 2 3
| li[class$=e]{ background: blue; }
|
E[attr*=”val”]:选择属性att的值包含”val”字符串的元素

1 2 3
| li[class*=o]{ background: blue; }
|
结构伪类选择器
选择器 |
描述 |
E:root |
选择匹配元素E所在文档的根元素 |
E:nth-child(n) |
选择E元素的父元素的第n个子元素 |
E:nth-last-child(n) |
选择E元素的父元素的倒数第n个子元素 |
E:fisrt-child |
选择E元素的父元素的第一个子元素的元素。与E:nth-child(1)等同 |
E:last-child |
选择E元素的父元素的最后一个子元素的元素。与E:nth-last-child(1)等同 |
E:nth-of-type(n) |
选择E元素的父元素内与E同类型的第n个元素 |
E:nth-last-of-type(n) |
选择E元素的父元素内与F同类型的倒数第n个元素 |
E:first-of-type |
选择E元素的父元素内与E同类型的第一个元素,与E:nth-of-type(1)等同 |
E:last-of-tye |
选择E元素的父元素内与E同类型的最后一个元素,与E:nth-last-of-type(1)等同 |
E: only-child |
如果E元素是唯一个子元素则选中 |
E: only-of-type |
如果E元素是唯一个是E类型的子元素则选中 |
E:empty |
选择没有子元素的元素,而且该元素也不包含任何文本节点 |
:fisrt-child
1 2 3
| li:nth-child(1){ background: blue; }
|

1 2 3
| li:nth-child(2n+1){ background: blue; }
|

1 2 3
| li:nth-child(3n+2){ background: blue; }
|

其他伪类选择器
选择器 |
描述 |
E:not(F) |
匹配所有除元素F外的E元素 |
E:target |
匹配E的所有元素,且匹配元素被相关URL指向 |
E:checked |
匹配选中的复选按钮或者单选按钮表单元素 |
E:enabled |
匹配所有启用的表单元素 |
E:disabled |
匹配所有禁用的表单元素 |
E:: selection |
选择被用户选取的元素部分。 |
not(F) 例子
1 2 3
| li:not(.six){ background: blue; }
|

E:target 例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div{ width: 100px; height: 100px; font-size: 30px; color:white; background: red; display: none; } div:target{ display: block; background: blue; } </style> </head> <body> <a href="#a1">显示一</a> <a href="#a2">显示二</a> <a href="#a3">显示三</a> <div id="a1">1</div> <div id="a2">2</div> <div id="a3">3</div> </body> </html>
|