前端垂直置中

來源:互聯網
上載者:User

標籤:重設   table   nbsp   blog   order   log   內容   idt   css   

 
基本html結構

1 <div class="out-box align-center">2 <div class="box">相對定位和絕對位置</div>3 </div>

基本css樣式

 .out-box{   background: #27AE60;    height: 200px;    border: 1px solid #555;     margin-bottom: 20px;   } .box{   width: 300px;   height: 100px;   background: #555; }

 

1. 定位布局
利用`relative`和`absolute`進行布局**
style

.align-center{  position: relative;}.align-center .box{  position: absolute;  margin-left: -150px;  margin-top: -50px;  left: 50%;  top: 50%; }

**優點:適合用於長寬固定的情況**
**缺點:長和寬一旦需要改變需要重設`marginleft`和`margin-top`**

2. css3屬性transform和定位進行布局
style

.align-center{   position: relative;} .align-center .box{  position: relative;  transform: translate(-50%,-50%);  left: 50%;  top: 50%;}

 

*優點:對盒子長寬沒有要求,屬於百分比布局**
*缺點:`transform`屬於css3屬性,低版本ie不支援**

3.table table-cell布局
style 

.align-center{   display: table;  width: 100%;} .align-center .box{  display: table-cell;  text-align: center;  vertical-align: middle; }


**優點:純table各瀏覽器不會有相容問題;內容可自適應;**
**缺點:不易於擴充,即將淘汰的技術**

4.flex布局
 style

.align-center4{   display: flex;  display: -webkit-flex;  align-items: center;  justify-content: center;} 


.align-center4 .box{}
**優點:簡單,快速。為了更簡單的布局而產生的一種布局技術**
**缺點:相容ie9+,需要加首碼**
 

前端垂直置中

聯繫我們

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