选择器 | 类型 | 功能描述 |
---|---|---|
* | 通配选择器 | 选择文档中全部的HTML元素 |
E | 元素选择器 | 选择指定类型的HTML元素 |
#id | ID选择器 | 选择指定ID属性值为“id”的任意类型元素 |
.class | 类选择器 | 选择指定class属性值为“class”的任意类型的任意多个元素 |
selector1,selectorN | 群组选择器 | 将每个选择器匹配的元素集合并 |
选择器 | 类型 | 功能描述 |
---|---|---|
E F | 后代选择器(包含选择器) | 选择匹配的F元素,且匹配的F元素被包含在匹配的E元素内 |
E>F | 子选择器 | 选择匹配的F元素,且匹配的F元素所匹配的E元素的子元素,注意是直接子元素,不包括子元素里的子元素 |
E+F | 相邻兄弟选择器 | 选择匹配的F元素,且匹配的F元素紧位于匹配的E元素的后面 |
E~F | 通用选择器 | 选择匹配的F元素,且位于匹配的E元素后的全部匹配的F元素 |
选择器 | 类型 | 功能描述 |
---|---|---|
E:link | 连接伪类选择器 | 选择匹配的E元素,并且匹配元素被定义了超连接并未被访问过。经常使用于连接描点上 |
E:visited | 连接伪类选择器 | 选择匹配的E元素,并且匹配元素被定义了超连接并已被访问过。经常使用于连接描点上 |
E:active | 用户行为选择器 | 选择匹配的E元素,且匹配元素被激活。经常使用于连接描点和按钮上 |
E:hover | 用户行为选择器 | 选择匹配的E元素,且用户鼠标停留在元素E上。IE6及如下浏览器仅支持a:hover |
E:focus | 用户行为选择器 | 选择匹配的E元素,并且匹配元素获取焦点 |
选择器 | 功能描述 |
---|---|
E:target | 选择匹配E的全部元素,且匹配元素被相关URL指向 |
选择器 | 类型 | 功能描述 |
---|---|---|
E:checked | 选中状态伪类选择器 | 匹配选中的复选按钮或者单选按钮表单元素 |
E:enabled | 启用状态伪类选择器 | 匹配全部启用的表单元素 |
E:disabled | 不可用状态伪类选择器 | 匹配全部禁用的表单元素 |
选择器 | 功能描述 |
---|---|
E:fisrt-child | 做为父元素的第一个子元素的元素E。与E:nth-child(1)等同 |
E:last-child | 做为父元素的最后一个子元素的元素E。与E:nth-last-child(1)等同 |
E:root | 选择匹配元素E所在文档的根元素。在HTML文档中,根元素始终是html,此时该选择器与html类型选择器匹配的内容相同 |
E F:nth-child(n) | 选择父元素E的第n个子元素F。其中n能够是整数(1,2,3)、关键字(even,odd)、能够是公式(2n+1),并且n值起始值为1,而不是0. |
E F:nth-last-child(n) | 选择父元素E的倒数第n个子元素F。此选择器与E:nth-child(n)选择器计算顺序恰好相反,但使用方法都是同样的,其中:nth-last-child(1)始终匹配最后一个元素,与last-child等同 |
E:nth-of-type(n) | 选择父元素内具备指定类型的第n个E元素 |
E:nth-last-of-type(n) | 选择父元素内具备指定类型的倒数第n个E元素 |
E:first-of-type | 选择父元素内具备指定类型的第一个E元素,与E:nth-of-type(1)等同 |
E:last-of-tye | 选择父元素内具备指定类型的最后一个E元素,与E:nth-last-of-type(1)等同 |
E:only-child | 选择父元素只包含一个子元素,且该子元素匹配E元素 |
E:only-of-type | 选择父元素只包含一个同类型子元素,且该子元素匹配E元素 |
E:empty | 选择没有子元素的元素,并且该元素也不包含任何文本节点 |
注:(1),“ul>li:nth-child(3)”表达的并非必定选择列表ul元素中的第3个子元素li,仅有列表ul中第3个li元素前不存在其余的元素,命题才有意义,不然不会改变列表第3个li元素的样式。css
(2),:nth-child(n) 中参数只能是n,不能够用其余字母代替。html
(3),:nth-child(odd) 选择的是奇数项,而使用:nth-last-child(odd) 选择的倒是偶数项浏览器
选择器 | 功能描述 |
---|---|
E:not(F) | 匹配全部除元素F外的E元素 |
选择器 | 功能描述 | |
---|---|---|
[attribute] | 用于选取带有指定属性的元素。 | |
[attribute=value] | 用于选取带有指定属性和值的元素。 | |
[attribute~=value] | 用于选取属性值中包含指定词汇的元素。 | |
[[attribute\ | =value]](http://www.w3school.com.cn/cs... | 用于选取带有以指定值开头的属性值的元素,该值必须是整个单词。 |
[attribute^=value] | 匹配属性值以指定值开头的每一个元素。 | |
[attribute$=value] | 匹配属性值以指定值结尾的每一个元素。 | |
[attribute=value*] | 匹配属性值中包含指定值的每一个元素。 |
下面选择几个咱们通常不经常使用的选择器来说讲ide
选择紧接在另外一个元素以后的第一个元素(同级元素)spa
<style> h1 + p { color:red; } </style> <h1>彭</h1> <p>于晏</p>
看一个该选择器应用的典型例子ssr
<style> body,div{ margin: 0; padding: 0; } .box{ width:400px; } .box > div{ width:100px; height: 35px; line-height: 35px; text-align: center; float:left; border: 3px solid #dedede; box-sizing: border-box; } </style> </head> <body> <div class="box"> <div>HTML</div> <div>CSS</div> <div>JavaScript</div> <div>Node.js</div> </div>
能够看到后面几个元素的左边框明显增大了3px,这里咱们只须要用+号选择器把除第一个元素之外的左边框去掉就能够l3d
.box > div + div{ border-left: none; }
a:active - 连接被点击的那一刻code
当设置为若干链路状态的样式,也有一些顺序规则:orm
body,div{ margin: 0; padding: 0; } ul{ list-style: none; //让列表前的标记再也不显示 } .box{ width:450px; margin: 100px; } .box > ul li{ width: 120px; height:35px; line-height: 35px; text-align: center; float: left; border: 1px solid #dedede; box-sizing: border-box; } .box > ul >li li{ border-top:none; } .box > ul >li +li{ border-left: none; } .box > ul >li >ul{ display: none; } .box > ul >li:hover ul{ display: block; //主要代码 } </style> <div class="box"> <ul> <li> 追梦人 <ul> <li>博客动态</li> </ul> </li> <li> CSS3 <ul> <li>CSS3文章</li> <li>CSS3技巧</li> </ul> </li> <li> HTML5 <ul> <li>HTML5最新动态</li> <li>HTML5使用教程</li> </ul> </li> </ul> </div>
<style> body,div{ margin: 0; padding: 0; } .box{ width:120px; height:100px; line-height: 100px; text-align: center; margin: 100px auto; background-color: pink; transition: all 1s; } .box:active{ padding:100px; } </style> <div class="box"> 点击获取能量... </div>
选中第一个字htm
选中首行文字
当没有子元素,而且没有文本节点的时候,:empty会被触发,能够利用:empty给默认提示
<style> body,div,ul{ margin: 0; padding: 0; } .box{ margin:100px; } ul{ margin-top;10px; } ul{ margin-top: 10px; } ul:empty::after{ content:'留言空空如也' } </style> <div class="box"> <input type="text"> <ul></ul> </div>
当ul没有内容的时候,会显示一条默认的信息。一旦加上内容,默认信息就会隐藏,不过须要注意的是,ul不能换行,必须这样写。
获取当前描点的那个元素
实现换肤功能
<style> body,div{ margin: 0; padding: 0; } .box > div{ position: absolute; left:0; top:0; width:100%; height: 100%; z-index: -1; } #bg1:target{ background-color: #61dafb; } //当#bg1元素为target(目标元素)是的style #bg2:target{ background-color: #e9203d; } #bg3:target{ background-color: #017fff; } </style> <div class="box"> <nav> <a href="#bg1">春</a> <a href="#bg2">夏</a> <a href="#bg3">秋</a> </nav> <div id="bg1"></div> <div id="bg2"></div> <div id="bg3"></div> </div>
当id为bg1,bg2和bg3的元素为目标元素时的style相同时,能够直接设置
<style> body,div{ margin: 0; padding: 0; } .box > div{ position: absolute; left:0; top:0; width:100%; height: 100%; z-index: -1; } target{ background-color: #61dafb; } </style> <div class="box"> <nav> <a href="#bg1">春</a> <a href="#bg2">夏</a> <a href="#bg3">秋</a> </nav> <div id="bg1"></div> <div id="bg2"></div> <div id="bg3"></div> </div>
利用target实现遮罩层
<style> body,div{ margin: 0; padding: 0; } #show{ text-decoration: none; color: #e9203d; } #show div{ position: fixed; left: 50%; top:50%; width:200px; height: 200px; transform: translate(-50%,-50%);//实现垂直水平居中 border:10000px solid rgba(0,0,0,0.3);//灰色背景是border,所以设置的很大 display: none; } #show:target > div{ display: block; } #hide:target{ display: none; } </style> <div class="box"> <a href="#show">弹出层</a> <a href="#hide" id="show"> <div id="hide">弹弹弹</div> </a> </div>
实现tab栏切换
<style> body,div{ margin: 0; padding: 0; } .box{ width: 300px; height: 300px; text-align: center; margin: 100px auto; } .title{ overflow: hidden; } .title > a{ float: left; width:100px; height: 35px; line-height: 35px; border:1px solid #dedede; box-sizing: border-box; text-decoration: none; } .title > a:not(:first-of-type){ border-left: none; } .content{ position: relative; } .content > div{ position: absolute; left:0; top:0; width:100%; height: 200px; line-height: 200px; border:1px solid #dedede; background-color: #61dafb; border-top:none; box-sizing: border-box; } #content1{ z-index: 1; } #content1:target,#content2:target,#content3:target{ z-index: 999; } </style> <div class="box"> <nav class="title"> <a href="#content1">CSS</a> <a href="#content2">HTML</a> <a href="#content3">JavaScript</a> </nav> <div class="content"> <div id="content1">CSS</div> <div id="content2">HTML</div> <div id="content3">JavaScript</div> </div> </div>