设置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>

效果:

更多推荐

:before伪类content实现垂直居中