伪类实现中间文字两边画横线

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36070288/article/details/102519515

代码如下:

<!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>

注意事项:

  1. 给父类定宽,即上面class="tip"的div元素加固定宽度,即上面代码中width:120px;
  2. 给父类position:relative;
  3. 伪类加position:absolute;
  4. 为了是左右伪类定位在左右两边,即left:-120px;和right:-120px;很关键;

猜你喜欢

转载自blog.csdn.net/qq_36070288/article/details/102519515