您的位置:首页 > Web前端 > CSS

CSS水平垂直居中方法总结

2018-09-13 10:35 399 查看

一、已经元素宽高(1)定位布局

[code]<div class="box">
<div class="contain"></div>
</div>
[code].box{
width:200px;
height: 200px;
background: red;
margin: 0 auto;
position: relative;

}
.contain{
width: 50px;
height: 50px;
background: gray;
position: absolute;
left: 50%;
top: 50%;
margin: -25px 0 0 -25px;
}

二、已经元素宽高(2)定位布局

[code].box{
width:200px;
height: 200px;
background: red;
margin: 0 auto;
position: relative;

}
.contain{
width: 50px;
height: 50px;
background: gray;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%)
}

三、已经元素宽高(3) flex布局

[code].box{
width:200px;
height: 200px;
background: red;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
}
.contain{
width: 50px;
height: 50px;
background: gray;
}

四、未知元素宽度(这种是宽度自适应)

[code].box{
width:200px;
height: 200px;
background: red;
margin: 0 auto;
position: relative;
}
.contain{
height: 50px;
background: gray;
position: absolute;
margin: auto;
left: 0;
top: 0;
right: 0;
bottom: 0;
}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: