設置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>
效果: