元素的居中問題是每一個初學者碰到的第一個大問題,在此我總結了下各類塊級 行級 水平 垂直 的居中方法,並儘可能給出代碼實例。css
首先請先明白塊級元素和行級元素的區別html
element { margin: 0 auto; }
.outside { width:500px; height:200px; background-color: red; margin:100px auto; position: relative; } .inner { width: 100px; height:50px; background-color: yellow; position: absolute; // 絕對定位 left:50%; // + margin-left: -50px; // 負邊距
}
.outside { width:500px; height:200px; background-color: red; margin:100px auto; position: relative; display: flex; // 父元素display:flex; justify-content: center; // 主軸上居中對齊 } .inner { background-color: yellow; height:100px; width: 50px; }
.outside { width:720px; height: 720px; margin: 0 auto ; position: relative; /*祖先元素的非static定位*/ } .inner { width: 350px; height: 350px; position: absolute; top: 50%; /*元素相對其最近的position屬性不爲static的元素(祖先元素)移動50%,、、 和 屬性指定定位元素的位置。*/ margin-top: -175px; /*元素向上移動自身的50%,此時,正好垂直居中*/ }toprightbottomleft
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test</title> <style> .outside { width:300px; height: 200px; margin:100px auto; background-color: red; display: flex; // 彈性盒子 align-items: center; // 彈性盒子 } .inner { width:50px; height: 30px; background-color: yellow; } </style> </head> <body > <div class="outside"> <div class="inner"> inner </div> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test</title> <style> .outside { width:300px; height: 200px; margin:100px auto; background-color: red; position: relative; // 父元素非static } .inner { width:50px; height: 30px; background-color: yellow; position: absolute; top:50%; transform: translateY(-50%); // 相對於自身高度,向上移動50% } </style> </head> <body > <div class="outside"> <div class="inner"> inner </div> </div> </body> </html>
利用transform的translate()或translateY()方法ide
1 <!--Html 代碼--> 2 <div class="outside"> 3 <div class="inner"> 4 </div> 5 </div>
1 /*Css 代碼*/ 2 .outside { 3 background-color: #5555ee; 4 width:720px; 5 height: 720px; 6 margin: 0 auto ; 7 position:relative; 8 9 } 10 .inner { 11 width: 350px; 12 height: 350px; 13 background-color: #ee5555; 14 position: absolute; 15 top:50%;/*相對祖先元素向下移動50%*/ 16 transform: translateY(-50%);/*元素y軸移動自身的一半*/ 17 transform: translate(,-50%);/*元素y軸移動自身的一半,與上一行效果相同*/ 18 }
利用上邊距,下邊距在垂直方向的autoflex
<head> <style type="text/css"> .outside { width: 300px; height: 300px; background-color: red; margin: 60px auto; position: relative; } .inside { width: 150px; height: 150px; background-color: black; position: absolute; 首先是父元素與子元素的position屬性值要「配套」好,這樣子元素的top,bottom才能夠發揮應有的做用 top:30px; bottom:30px; 當垂直方向上的margin爲auto時,不管是margin-left,margin-right,仍是margin:auto, auto老是會試圖充滿整個父元素, margin: auto 4px; 30px,30px,auto的「配合」是此方法有效果的關鍵所在。子元素被要求拉伸到top:30px;bottom:30px;(兩個值必須同樣,這樣才居中),但由於高度被固定而作不到這一點,因此margin:auto;就讓它居中了。 } </style> </head> <body> <div class="outside"> <div class="inside"></div> </div> </body>
<!--Html 代碼--> <div class="outside"> <div class="inner"> </div> </div>
3.1 設置direction:row或是direction:columnsflexbox
.outside { display: flex; flex-direction: column; /*主軸方向設爲垂直方向*/ justify-content: center; /*主軸方向上的元素的對齊方式*/ } .outside { display: flex; flex-direction: row; /*默認主軸方向就是row*/ align-items: center; /*交叉軸上的元素的對齊方式*/ }
3.2 目前來講最簡單的方法:spa
.outside { display: flex; } .inner { margin: auto; }
{
text-align:
}
垂直居中:code