<div class="Container"> <div class="Absolute-Center"> </div> </div> .Container { position: relative; } .Absolute-Center { position: absolute; width: 50%; height: 50%; overflow: auto; //防止內容越界溢出 margin: auto; top: 0; left: 0; bottom: 0; right: 0; }
優勢:html
兼容性好 Support IE8+web
支持百分比寬高瀏覽器
缺點:code
必須聲明高度orm
不適用Windows Phonehtm
.is-Negative { position: absolute; width: 100px; height: 200px; padding: 10px; top: 50%; left: 50%; margin-left: -60px; //(width + padding)/2 margin-top: -110px; //(height + padding)/2 }
優勢:it
兼容性好 Support All Browserio
缺點:table
定寬高且不支持百分比form
<div class="Container "> <div class="Table-Cell"> <div class="Child"> </div> </div> </div> .Container { display: table; } .Table-Cell { display: table-cell; vertical-align: middle; } .Child { width: 50%; margin: 0 auto; }
優勢:
兼容性好 Support IE8+
不定寬高
缺點:
html層級多
.Transform-Translate { position: absolute; width: 50%; margin: auto; top: 50%; left: 50%; -webkit-transform: translate(-50%,-50%); }
優勢:
不定寬高
缺點:
瀏覽器兼容性(適合移動端)
廠商前綴
可能與其餘transform屬性衝突