經典場景:<div> 內含 <img> + <span>,需雙向置中。關鍵在於理解 line-height、vertical-align、行內框與行框的關係。
<div class="treeTitle">
<img src="images/app.png" />
<span>導航菜單</span>
</div>
#treeTitle {
height: 30px; line-height: 30px; /* 行高等於容器高 */
text-align: center; /* 水平置中 */
color: white; background: url(../images/bj.png);
}
#treeTitle img { vertical-align: middle; } /* 圖片垂直置中於行內框 */

line-height 決定行內框高度,行內框垂直置中於行框。vertical-align: middle 將圖片/文字的垂直基線對齊於行內框中心線。line-height 計算的其他屬性。
.treeTitle { display:flex; align-items:center; justify-content:center; height:30px; }
Flex 一行解決,無需理解行框模型,強烈推薦新專案使用。