CSS:水平置中、垂直置中

來源:互聯網
上載者:User

標籤:內聯   ext   translate   ddl   div   line   orm   table   絕對位置   

水平置中 
  1. margin:0 auto大法
.child{ width:100px;margin:0 auto }注意:子項目要有寬度才可以使用 
  1. display:table大法
.child{ display:table; margin:0 auto; } 
  1. text-align:center大法
普通子項目在父元素中.parent{ text-align:center }.child{ display:inline-block } 若父元素內是文字,可以不用以內聯塊狀元素形式展示,因為我覺得文字就帶有內聯屬性.parent{ text-align:center } 
  1. 絕對位置大法
.parent{ position:relative }.child{ position:absolute; left:50%; transform:translate(-50%) }太神奇了!!是呀!!我怎麼沒想到!!(1)定位要是absolute(2)移動為-50%,負值另:.parent{ position:relative }.child{ position:absolute; left:50%;margin-left:-元素寬度 }  // 必須要知道子項目的寬度 
  1. display:flex大法
css3待續 垂直置中 
  1. vertical-align大法
.parent{ display:inline-block; vertical-align:middle; line-height:200px; }.parent{ display:table-cell; vertical-align:middle; height:200px; }vertical-align和line-height(height)更配哦~~有趣了,控制的是父元素,置中的是子項目 
  1. 絕對位置大法
 .parent{ position:relative }.child{ position:absolute; top:50%; transform:translateY(-50%); }  flex大法 水平垂直置中幾種方式可供參考方式一:.parent{display:table-cell;vertical-align:middle;text-align:center;height: 100px;width: 200px;}.child{display:inline-block;}方式二:.parent{ position:relative; }.child{ position:absolute; top:50%; left:50%; transform:translate(-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.