:before僞類content實現垂直居中

 

設置display: inline-block;的標籤i與span標籤一起會導致元素垂直不對齊,通過給i標籤設置僞類before,content的不可爲空,從而實現垂直居中。

 

 未設置僞類before

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .head-left .icon-wodeditu{
            background: #5daaff;
            width: 30px;
            height: 30px;
            border-radius: 100%;
            color: #fff;
            line-height: 30px;
            display: inline-block;
            text-align: center;
        }
        .head-left .icon-wodeditu:before{
            /*content: "111";*/
        }
         .head-left .title{
            color: #3c3d3f;
            margin: 0 10px;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div class="head-left">
    <h2>
        <i class="icon-wodeditu"></i>
        <span class="title">我的可視化地圖</span>
    </h2>

</div>
</body>
</html>

效果:

設置僞類before 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .head-left .icon-wodeditu{
            background: #5daaff;
            width: 30px;
            height: 30px;
            border-radius: 100%;
            color: #fff;
            line-height: 30px;
            display: inline-block;
            text-align: center;
        }
        .head-left .icon-wodeditu:before{
            content: "111";
        }
         .head-left .title{
            color: #3c3d3f;
            margin: 0 10px;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div class="head-left">
    <h2>
        <i class="icon-wodeditu"></i>
        <span class="title">我的可視化地圖</span>
    </h2>

</div>
</body>
</html>

效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章