1、水平居中 1.1、行內(nèi)元素水平居中
.box { text-align: center; background-color: #f6e5dd; } <div class="box"> 水平居中 </div>
1.2、塊級(jí)元素水平居中 1.2.1、固定寬度塊級(jí)元素水平居中
.box { width: 200px; margin: 0 auto; /*左右margin值auto自動(dòng),不能為0*/ background-color: #f6e5dd; } <div class="box">水平居中</div> ? 1.2.2、不固定寬度塊級(jí)元素水平居中
.box { display: table; margin: 0 auto; background-color: #f6e5dd; } <div class="box">水平居中</div>
.box { text-align: center; background-color: #f6e5dd; } .inlineblock-div { display: inline-block; /*不能設(shè)置為block或其他*/ text-align: center; background-color: #d5eeeb; } <div class="box"> <div class="inlineblock-div">子元素1</div> <div class="inlineblock-div">子元素2</div> </div>
.box { display: flex; justify-content: center; background-color: #f6e5dd; } .flex-div { background-color: #d5eeeb; border: 2px solid red; } <div class="box"> <div class="flex-div">子元素1</div> <div class="flex-div">子元素2</div> </div>
2、垂直居中 2.1、單行文本垂直居中
.box { height: 100px; line-height: 100px; background-color: #f6e5dd; } <div class="box">垂直居中的文本</div>
2.2、 多行文本垂直居中
.box { display: table; width: 300px; height: 200px; background-color: #f6e5dd; } .table-div { display: table-cell; vertical-align: middle; border: 5px solid blue; } <div class="box"> <div class="table-div"> 垂直居中的文本<br/> 另一行文本 </div> </div>
2.3、塊級(jí)元素垂直居中
.box { height: 100px; width: 300px; display: flex; align-items: center; background-color: #f6e5dd; } .item { background-color: #d5eeeb; } <div class="box"> <div class="item">垂直居中的塊級(jí)元素</div> </div>
.parent { position: relative; height: 200px; width: 200px; background-color: #f6e5dd; } .child { position: absolute; height: 100px; width: 150px; top: 50%; margin-top: -50px; /*高度的一半*/ line-height: 100px; background-color: #d5eeeb; } <div class="parent"> <div class="child">已知高度垂直居中</div> </div>
.parent { position: relative; height: 200px; width: 200px; background-color: #f6e5dd; } .child { position: absolute; top: 50%; transform: translate(0, -50%); background-color: #d5eeeb; } <div class="parent"> <div class="child">已知高度垂直居中</div> </div>
3、水平垂直居中 3.1、絕對(duì)定位 + 負(fù) Margin
.container { /* ---容器盒子水平、垂直居中對(duì)齊--- */ width: 200px; height: 100px; position: absolute; left: 50%; top: 50%; margin-left: -100px; /* 寬度的一半 */ margin-top: -50px; /* 高度的一半 */ /* ---內(nèi)容居中對(duì)齊--- */ line-height:100px; /* 同容器盒子的高度一致 */ text-align: center; background: #f6e5dd; } <div class="container">水平、垂直居中布局</div>
3.2、絕對(duì)定位+Transform
.container { /* ---容器盒子水平、垂直居中對(duì)齊--- */ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 200px; height: 100px; /* ---內(nèi)容居中對(duì)齊--- */ line-height: 100px; /* 同容器盒子的高度一致 */ text-align: center; background: #e6f4f5; } <div class="container">水平、垂直居中布局</div
3.3、絕對(duì)定位 + 自動(dòng) Margin
.container { /* ---容器盒子水平、垂直居中對(duì)齊--- */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; width: 200px; height: 100px; /* ---內(nèi)容居中對(duì)齊--- */ line-height: 100px; /* 同容器盒子的高度一致 */ text-align: center; background: #d5eeeb; } <div class="container">水平、垂直居中布局</div>
3.4、Flex布局
.container { /* ---容器盒子水平、垂直居中對(duì)齊--- */ display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; justify-content: center; align-items: center; width: 200px; height: 100px; background: #d5eeeb; } .child { background: #f6e5dd; height: 50px; width: 100px; } <div class="container"> <div class="child">水平、垂直居中布局</div> </div>
|
|