vertical-align:middle的置中細節調整

來源:互聯網
上載者:User

標籤:style   blog   color   使用   div   html   line   htm   

  使用vertical-align:middle可以讓行級元素垂直置中,但這個置中是以文字的中線來計算的,而文字的中線在不同的字型上不同,甚至相同的字型在不同的瀏覽器上顯示的都不同。

  所以直接使用vertical-align:middle很難完美置中,因此需要其它調整。

<!DOCTYPE html><style>div {  border:1px solid red;  width:200px;  height:100px;  line-height:100px;  text-align:center;}span {vertical-align:middle;}#box {  display:inline-block;  width:100px;  height:98px;  background:#EEE;}</style><div>  abc<span id="box"></span>def</div>

  

  當容器記憶體在vertical-align:middle元素時,它們的中線會被對其到所在行的基準上。

  而所在行的基準未必是中線,基準與中線偏離的距離就是vertical-align:middle垂直置中時候出現的偏離距離。

  基準與中線的位置差異會受到字型、字形、字型大小,等影響。而且在不同瀏覽器上字型的渲染還存在差異,這樣就不容易計算具體的偏差。

  但我們可以取消掉基準和中線的位置差,做法是將字型大小改到0,也就是不在行中直接使用文本節點。

<!DOCTYPE html><style>div {  border:1px solid red;  width:200px;  height:100px;  line-height:100px;  text-align:center;  font-size:0px;}span {  vertical-align:middle;  display:inline-block;  font-size:1rem;}#box {  width:100px;  height:98px;  background:#EEE;}</style><div>  <span>abc</span><span id="box"></span><span>def</span></div>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.