div:nth-of-type(2)::before{
/*必须添加content属性,不然后期不可见*/
content: "";
/*默认是行级元素,若是想设置宽高,就必须转换为块级元素*/
position: absolute;
width: 20px;
height: 20px;
border-radius: 10px;
left: -10px;
top: -10px;
}
div:nth-of-type(2)::after{
/*必须添加content属性,不然后期不可见*/
content: "";
/*默认是行级元素,若是想设置宽高,就必须转换为块级元素*/
position: absolute;
width: 20px;
height: 20px;
border-radius: 10px;
left: -10px;
bottom: -10px;
}
/*获取第一个字符:实现首字下沉*/
p::first-letter{
color: red;
font-size: 30px;
float: left;/*文本环绕*/
}
/*获取第一行内容:若是设置了::first-letter,那么没法同时设置它的样式*/
p::first-line{
text-decoration: underline;
}
/*设置当前选中内容的样式*/
p::selection{
color: red;
/*它只能设置显示的样式,而不能设置内容大小*/
/*font-size: 30px;*/
}
在CSS中总有一些你不用不知道,用到才知道的“坑”。好比今天要谈的,把 before, after 伪类用在 <img> 标签上。css
嗯,实际上你用你会发现,在大多数浏览器这是无效的,dom中并不会出现你想要的结果。html
为何会这样呢?
让咱们回归到 W3C 标准中寻觅一下,在标准中,before, after 伪类的定义如:css3
As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content.
来自 https://www.w3.org/TR/CSS21/generate.html#before-after-contentweb
咱们应该注意到所谓 document tree content,对于 img 这种自闭和标签,彷佛不存在 content (内容或后代元素)在标签中,因此选择器没有生效。但这样的解释还不够清晰,实际上标准中还有一行注释:浏览器
Note. This specification does not fully define the interaction of :before and :after with replaced elements (such as IMG in HTML). This will be defined in more detail in a future specification.dom
嗯,这回清楚了,对于可替换元素(如 img、input、select 等),标准并无清晰定义,这也致使了浏览器实现的差别性。post