css垂直水平置中方案

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   on   c   line   ad   

1. 水平置中

  • 如果是inline元素:在父元素上面設定text-align:center;
  • 如果是block元素:設定寬度和margin:0 auto;
  • 如果是多區塊層級元素:在父元素上面設定text-align:center;所有的子項目上面設定display:inline-block;

2. 垂直置中相對於水平置中要複雜些,結合自己的需求自己對待

  • 不知道元素具體的高:可以根據padding-top和padding-bottom來
  • 不知道元素具體的高:父元素設定成display:table,子項目設定成display:table-cell,vertical-align:middle;
  • 不知道元素具體的高:父元素設定position:relative;子項目設定position:absolut;trandform:translateY(-50%)
  • 知道元素具體的高是單行:如果是單行的可以根據line-height=height
  • 知道具體的高:position:absolute;top:50%;margin-top:-width/2

3. 不知道元素具體的寬和高的置中情況:父元素position:relative;子項目position:absolute;top:50%;left:50%;trandform:translateY(-50% -50%)

   

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.