示例:

利用after和before伪元素在文字两边写横线

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪元素after和before的用法</title>
    <style>
        .container{
            width: 1000px;
            margin: 10px auto;
            border: 1px solid red;
        }
        h3.title {
            color: #F2AE11;
            font-size: 1.3em;
            margin-bottom: 3em;
            text-align: center;
            font-weight: 500;
            line-height: 1.1;
        }
        h3.title span {
            display: block;     /*设置为块级元素会独占一行形成上下居中的效果*/
            font-size: 3em;
            color: #212121;
            position: relative;   /*定位横线(当横线的父元素)*/
        }
        h3.title span:before, h3.title span:after {
            content: '';                 /*CSS伪类用法*/
            position: absolute;         /*定位背景横线的位置*/
            top: 52%;
            background: #494949;       /*宽和高做出来的背景横线*/
            width: 9%;
            height: 2px;
        }
        h3.title span:before{
            left: 25%;        /*调整背景横线的左右距离*/
        }
        h3.title span:after {
            right: 25%;
        }
    </style>
</head>
<body>
<div class="container">
    <h3 class="title">I love you<span>Hello</span></h3>
</div>
    </body>
</html>

 我来分割线-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

新增渐变色,效果图类似下面

利用after和before伪元素在文字两边写横线

 

       h3.title span:before, h3.title span:after {
            content: '';                 /*CSS伪类用法*/
            position: absolute;         /*定位背景横线的位置*/
            top: 52%;
           /* background: #494949;       宽和高做出来的背景横线*/
            width: 9%;
            height: 2px;
        }
        h3.title span:before{
            left: 25%;        /*调整背景横线的左右距离*/
            background:linear-gradient(to left,#b6b6b6,#efefef);
        }
        h3.title span:after {
            right: 25%;
            background:linear-gradient(to left,#efefef,#b6b6b6);
        }    

可以试一下以下的代码:是一个渐变的效果

<div style="background:linear-gradient(to left,#efefef,#b6b6b6,#efefef);height:1px;"></div>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
  • 2021-07-07
  • 2021-10-23
  • 2021-07-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
  • 2021-11-07
  • 2021-06-04
  • 2021-09-18
  • 2021-07-16
相关资源
相似解决方案