CSS的选择器除了根据id、class、属性等从DOM中获取元素的以外,还有很重要的一类,用来获取元素的特别内容或特别状态,这就是 伪元素(Pseudo-elements) 和 伪类(Pseudo-classes)
伪元素是对元素中的特定内容进行操作,选取诸如元素内容第一个字(母)、第一行,选取某些内容前面或后面这种普通的选择器无法完成的工作。它控制的内容实际上和元素是相同的,但是它本身只是基于元素的抽象,并不存在于文档中,所以叫伪元素
::before
在元素内容之前插入额外生成的内容::after
在元素内容之后插入额外生成的内容::first–letter
选取元素的首个字符::first–line
选取元素的第一行::selection
对用鼠标键盘等已选取的文字部分应用样式::spelling-error
::grammar-error
相比于其他伪元素大都是对文档中已有部分的选择,::before 和 ::after 则是向文档树中加入内容,这些内容并不存在于HTML源代码中,但确是可见的,并且可以当作元素的子对象对待(正常的样式继承等)
::before
< 内容 < ::after
<input />
、 <iframe />
等不能包含子元素的标签,不支持 ::before 和 ::after<img />
正常情况下也符合上一条规则,但加载失败后其 ::before 和 ::after 就可生效
a:visited:before { content: "\2713 "; /*显示一个对勾 √ */ }
@media print { a[href]:after { content: " (" attr(href) ") "; } }
:required::after {
content: ' (Required) ';
color: #c00;
font-size: .8em;
}
.clearfix::after {
content: " ";
display: table;
clear: both;
}
h3::after {
display: inline-block;
content: "";
position: absolute;
left: 50%;
top: -41px;
width: 6px;
height: 6px;
background-color: #fb5211;
border-radius: 50%;
border: 2px solid #fff;
box-shadow: 0 0 0 3px #fb5211;
transform: translateX(-5px);
}
a[data-tooltip] {
display: inline-block;
position: relative;
text-decoration: none;
}
a[data-tooltip]::after {
content: attr(data-tooltip) "@_@";
position: absolute;
bottom: 130%;
left: 20%;
background: #ffcb66;
padding: 5px 15px;
white-space: nowrap;
opacity: 0;
transition: all 0.5s ease;
}
a[data-tooltip]:hover::after{
opacity: 1;
}
.button {
border-top: 1px solid #96d1f8;
background: #65a9d7;
background: -webkit-gradient(linear, left top, left bottom, from(#3e779d), to(#65a9d7));
padding: 5px 10px;
border-radius: 8px;
box-shadow: rgba(0,0,0,1) 0 1px 0;
text-shadow: rgba(0,0,0,.4) 0 1px 0;
color: white;
font-size: 14px;
text-decoration: none;
vertical-align: middle;
position: relative;
overflow: hidden;
display: inline-block;
}
.button::before {
content: "";
width: 200%;
height: 200%;
position: absolute;
top: -200%;
left: -225%;
background-image: -webkit-linear-gradient(135deg, rgba(255,255,255,0), rgba(255,255,255,0.6), rgba(255,255,255,0));
transition: all 0.5s ease-out;
}
.button:hover::before {
top: 200%;
left: 200%;
}
::first-letter
::before
也会参与到::first-letter
的规则中,::before
如果存在,其首字符会被应用样式换句话说,如果尝试使用visibility:hidden或者display:none隐藏::first-letter伪元素,是不会生效的
同样以上例的HTML结构说明,因为 ::first-letter 是伪元素,相当于 ::first-letter 选择的“第”字成为了 span 的子元素,故其权重大于span样式权重
p::first-letter {
color: red; /*红色会对首字符生效*/
}
p > span {
color: blue!important;
}
p::first-letter {
font: bold italic 3em / .5 serif;
color: #607d8b;
float: left;
margin: 0.2em 0.25em .01em 0;
}
.price::first-letter {
margin-right: 5px;
font-size: xx-large;
vertical-align: -2px;
}
<p>¥399</p>
::first-line
font-*
, background-*
, line-height
, color
, word-spacing
, letter-spacing
, text-decoration
, text-transform
::before
也会参与到::first-line
的规则,即便在样式上并不和第一行的内容连接(比如由一个diaplay:block的div子元素开头的内容)::selection
也被称为“高亮”伪元素,因为只是用来高亮鼠标选择部分的
window.getComputedStyle(
document.querySelector('.element'), ':before'
).getPropertyValue('color')
由于元素状态是动态变化的,所以一个元素特定状态改变时,它可能得到或失去某个样式。因为功能和class有些类似,但它是基于文档之外的抽象,所以叫伪类
:target
:link
:hover
:active
:visited
:focus
:not
:lang
:enabled
:disabled
:required
:optional
:checked
:in-range
:out-of-range
:valid
:invalid
:first-child
:last-child
:only-child
:nth-child()
:nth-last-child()
:empty
:first-of-type
:last-of-type
:only-of-type
:nth-of-type()
:nth-last-of-type()
:target
选中页面局部如果URL中的hash部分和页面中的锚点元素匹配,则对应页面元素可应用到由
:target
定义的样式
<h2 id="rotnruin">Rot & Ruin</h2>
<h2 id="romero">Romero Zombies</h2>
<ul>
<li><a href="#rotnruin">Rot & Ruin</a></li>
<li><a href="#romero">Romero Zombies</a></li>
...
</ul>
h2:target {
background: rgb(125,104,99);
border: 0;
color: rgb(255, 255, 255);
padding-left: 10px;
}
:target
实现了:not
过滤掉不符合的元素:not(x)
括号中可以应用几乎所有的选择器语法:not
可以搭配其他伪类使用,如用 p:not(:empty)
选择非空元素<p id="p1">aaa</p>
<p id="p2">bbbb</p>
<p id="p3">ccccc</p>
<p id="p4">dddddd</p>
p:not(#p1):not(#p3) {color: red;}
==在*-child
系列伪类中,索引是相对于所有同级兄弟元素计算的,而非特定类型==
:first-child
和 :last-child
<div>
<h1>Hi</h1>
<h2>Apple</h2>
<h2>Banana</h2>
<h2>ApplePen</h2>
<h2>Pen</h2>
</div>
h1:first-child {} /*Hi*/
h2:first-child {} /*匹配不到*/
h2:last-child {} /*Pen*/
:nth-child()
和 not() 一样,:nth-child() 和 :nth-last-child() 也是函数式的伪类选择器;接受一个单一参数,可取值为:
:nth-last-child()
和 :nth-child() 规则相同,唯一的区别在于从最后一个元素反向计算
:only-child
匹配相对于其父元素类型唯一的子元素
:empty
匹配空的元素
:first-of-type
,:last-of-type
,:only-of-type
,:nth-of-type()
,:nth-last-of-type()
*-child
一样*-child
不同的是, 索引只针对选择器指定的类型,而非同级的所有兄弟元素:enabled
和 :disabled
:required
和 :optional
:checked
:in-range
和 :out-of-range
:valid
和 :invalid
:checked
:focus
:disabled
:enabled
:empty
:first-child
:last-child
:nth-child
:nth-last-child
:only-child
:first-of-type
:last-of-type
:nth-of-type
:nth-last-of-type
:only-of-type
:not
:target
:lang
:root
[题图:凡尔赛宫园林](http://bbs.fengniao.com/forum/pic/slide_165_8734595_79759609.html)
* 原创文章转载请注明出处