伪类实现中间文字两边画横线
原创
©著作权归作者所有:来自51CTO博客作者wx5864b4ab50c8a的原创作品,请联系作者获取转载授权,否则将追究法律责任
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>伪类实现中间文字两边画横线</title>
<style>
* {
margin: 0;
padding: 0;
}
.tip {
height: 40px;
line-height: 40px;
width: 120px;
margin: 0 auto;
text-align: center;
font-size: 24px;
color: #ccc;
position: relative;
}
.tip::before,
.tip::after {
position: absolute;
content: "";
border-top: 2px solid #ccc;
width: 100px;
top: 50%;
}
.tip::before {
left: -120px;
}
.tip::after {
right: -120px;
}
</style>
</head>
<body>
<div class="tip">没有更多了</div>
</body>
</html>
注意事项:
- 给父类定宽,即上面class="tip"的div元素加固定宽度,即上面代码中width:120px;
- 给父类position:relative;
- 伪类加position:absolute;
- 为了是左右伪类定位在左右两边,即left:-120px;和right:-120px;很关键;