div上下左右居中几种方式


1、绝对定位(常用于登录模块)
备注:前提条件div需要有宽高

#html
<div class="box"></div>
#css
.box{
position:absolute/fixed;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}

 

2、margin负值
备注:前提条件div需要有宽高

#html
<div class="box"></div>
#css
.box{
width:200px;
height: 200px;
position: absolute;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-100px;
}

 

3、css3 transform
备注:用于不确定当前div的宽度和高度

#html
<div class="box"></div>
#css
.box{
position: absolute;
left:50%;
top:50%;
transform: translate(-50%, -50%);
}

4、flex 布局方式

#html
<div class="box">
<div class="child">child</div>
</div>
#css
.box{
display:flex;
align-items:center;
justify-content:center;
}

 

5、table-cell 方式

#html
<div class="box">
<div class="child">child</div>
</div>
#css
.box{
display: table-cell;
vertical-align: middle;
text-align: center;
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM