描述
请给html模块的div元素加一个后伪元素,且后伪元素的宽度和高度都是20px,背景颜色为"rgb(255, 0, 0)"。
题解:
<html>
<style type="text/css">
div{
content="";
width:20px;
height:20px;
display:block;
background-color:rgb(255,0,0);
}
</style>
<body>
<div></div>
</body>
</html>
什么是伪元素?
CSS 伪元素用于设置元素指定部分的样式。
例如,它可用于:
- 设置元素的首字母、首行的样式
- 在元素的内容之前或之后插入内容
语法
伪元素的语法:
selector::pseudo-element {
property: value;
}
::first-line 伪元素
::first-line 伪元素用于向文本的首行添加特殊样式。
下面的例子为所有 <p> 元素中的首行添加样式:
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
注意: ::first-line伪元素只能应用于块级元素。
以下属性适用于 ::first-line 伪元素:
- 字体属性
- 颜色属性
- 背景属性
- word-spacing
- letter-spacing
- text-decoration
- vertical-align
- text-transform
- line-height
- clear
请注意双冒号表示法 - ::first-line 对比 :first-line
在