本文轉載於:猿2048網站➽css+div水平居中php
實現div內容水平居中css
實現方案一:margin:0 auto;html
div{ height:100px; width:100px; background:red; margin:0 auto; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>div水平居中</title> </head> <body> <div></div> </body> </html>
實現div水平居中方案二:position:absolute;絕對定位
css3
div{ height:100px; width:100px; background:red; position:absolute; left:50%; right:50%; margin: auto; }
實現div水平垂直居中web
實現方案一:position:fixed;固定定位瀏覽器
div{ height:100px; width:100px; background:red; position:fixed; left:0; top:0; bottom:0; right:0; margin:auto; }
實現方案二:position:absolute;絕對定位網站
div{ height:100px; width:100px; background:red; position:absolute; left:0; top:0; bottom:0; right:0; margin:auto; }
實現方案二:css3+position;spa
div{ height:100px; width:100px; background:red; position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); }
transform爲css3的新增屬性,所以須要加上前綴,兼容手機和其餘的瀏覽器。如code
-ms-transform:translate(-50%,-50%); /* IE 9 */ -moz-transform:translate(-50%,-50%); /* Firefox */ -webkit-transform:translate(-50%,-50%);/* Safari and Chrome */ -o-transform:translate(-50%,-50%); /* Opera */