實現圖標Icon+文字在div里自動中心居中(水平垂直居中)


已知div行高設置text-align:center文字會自動居中。

通過:before來設置icon的地址和高寬。

需要設置圖片默認的垂直居中條件,與文字一致,為text-bottom。

設置圖片行級顯示。

<!Doctype html>
<html>
<head>
<style>
body{margin:0;padding:0;}
.con{position:absolute;width:500px;height:50px;border:1px red solid;
line-height:50px;color:green;text-align:center;
}
.txt{font-size:20px;}
.txt:before{
content:'111';
background-image:url('icon.png');
background-size:20px 20px;
vertical-align:text-bottom;
display:inline-block;
width:20px;
height:20px;
border:0;
top:0;
left:0;
}
</style>
</head>
<body>
<div class="con">
<span class="txt">我是三哥四哥五哥一個字</span>
</div>
</body>
</html>

實際效果,圖標和文字會自動在div里中心居中


免責聲明!

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



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