CSS 文字垂直置中

來源:互聯網
上載者:User

標籤:style   blog   http   color   ar   strong   2014   div   問題   

原始 demo

html

  <div class ="box">    <div class="text">      我是單行測試文字    </div>  </div>  <div class="box">    <div class="text">       我是多行文字我是多行文字是多行文字我是多行文字我是多行文字我是多行文字我是多行文字我是多行文字我是多行文字我是多行文字我是>多行文字我是多行文字    </div>  </div>

css

  .box{ border:1px solid #f00; background-color:#eee; width:500px; height:100px;}  .text{}

:

一、單行文字的垂直置中

解決方案:line-height方法

css

   .box{ border:1px solid #f00; background-color:#eee; width:500px; height:100px;line-height:100px;}

說明:可以看到,這種方法會使得單行文字置中,而多行文字因為被設定了與容器等大的 line-height,導致文字溢出。所以該方法只適合單行文字的置中。

 

二、多行文字的垂直置中

解決方案:vertical-align 方法

css

  .box{ border:1px solid #f00; background-color:#eee; width:500px; height:100px;vertical-align:middle;display:table-cell;}  .text{}

:

說明:可以看到,這種方法能夠使得單行文字、多行文字都置中。但是因為 table-cell 是 inline 類型,所以會導致原來的區塊層級元素每個 div 一行移動到了同一行。如果需要分列兩行,需要在外面額外添加容器對位置進行控制。

 

 

相關閱讀:

大小不固定的圖片、多行文字的水平垂直置中

line-height、vertical-align與display:inline-block的一些問題  

CSS 文字垂直置中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.