利用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>

 我來分割線-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

新增漸變色,效果圖類似下面

 

       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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM