美文网首页
上下左右居中的三种方法

上下左右居中的三种方法

作者: subo1371 | 来源:发表于2016-11-03 21:36 被阅读0次

第2种,和第三种方法,都需要设置html,body,以及父元素的 的长宽为100%

1.  左右各百分之五十,左右边距设为负的宽高的一般,让图形缩为空。
width: 400px;

height: 200px;

background: #ccc;

position: absolute;

left:50%;

top:50%;

margin-left:-200px;

margin-top:-100px;

2 第二种方法 flex方法,设置父容器

display: -webkit-flex;

display: flex;

justify-content:center;

align-items:center;

3. 第三种方法   转换为table表格块

div1{

width: 100%;

height:100%;

display: table;

}

div2{

display: table-cell;

vertical-align: middle;

}

div3{

width: 400px;

height: 200px;

background: #ccc;

position: relative;

margin:0 auto;

overflow: hidden;

相关文章

网友评论

      本文标题:上下左右居中的三种方法

      本文链接:https://www.haomeiwen.com/subject/avriuttx.html