本文轉載於:猿2048網站居中的幾種方法php
一:經過margin負值css
.one{ position: absolute; width: 200px; height: 200px; top: 50%; left: 50%; margin-left: -100px;}
兼容性好,可是必需要定寬高css3
二:經過margin:auto瀏覽器
.two{ position: absolute; width: 200px; height:200px; margin: auto 0;}
以上兩種方法均可以把absolute換成fixed,注意,fixed在ie下不支持佈局
三:行內元素居中flex
.three{ width: 100px; height: 100px; line-height: 100px; text-align: center;}
這種方法只能居中行內元素。經常使用於文字對其居中網站
四:transform居中spa
.four{ position: absolute; top: 50%; height: 50%; transform: translate(-50%, -50%);}
存在css3瀏覽器兼容問題orm
五:flex佈局blog
.five{ display: flex; align-items: center; justify-content: center;}
存在css3瀏覽器兼容問題