1、绝对定位(常用于登录模块)
备注:前提条件div需要有宽高
1 <div class="box"></div> 2 #css 3 .box{ 4 position:absolute/fixed; 5 left:0; 6 right:0; 7 top:0; 8 bottom:0; 9 margin:auto; 10 }
2、margin负值
备注:前提条件div需要有宽高
1 <div class="box"></div> 2 #css 3 .box{ 4 width:200px; 5 height: 200px; 6 position: absolute; 7 left:50%; 8 top:50%; 9 margin-left:-100px; 10 margin-top:-100px; 11 }
3、css3 transform
备注:用于不确定当前div的宽度和高度
1 <div class="box"></div> 2 #css 3 .box{ 4 position: absolute; 5 left:50%; 6 top:50%; 7 transform: translate(-50%, -50%); 8 }
4、flex 布局方式
1 <div class="box"> 2 <div class="child">child</div> 3 </div> 4 #css 5 .box{ 6 display:flex; 7 align-items:center; 8 justify-content:center; 9 }
5、table-cell 方式
<div class="box"> <div class="child">child</div> </div> #css .box{ display: table-cell; vertical-align: middle; text-align: center; }
标签:box,margin,top,50%,CSS,div,上下左右,css From: https://www.cnblogs.com/lgx5/p/17357205.html