伪元素::after和::before
最近伪元素用的多,总结一下它们绝对定位后的相对于主元素的最原始的位置:
图片示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
.line {
height: 30px;
width: 0;
margin: 50px auto auto;
border: 1px solid red;
padding: 1px;
position: relative;
}
.line::before, .line::after {
content: ' ';
position: absolute;
}
.line::before {
width: 10px;
height: 4px;
background-color: orchid;
}
.line::after {
width: 20px;
height: 2px;
background-color: orange;
}
</style>
<div class="line"></div>
</body>
</html>
总结
before
和after
的绝对定位后的原始位置其实都在同一个地方,并且top
和left
的使用无区别。