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

用CSS实现居中的几种方式

2017-06-01 18:06 169 查看

用CSS实现居中的几种方式

HTML


<div class="box">

<div class="content"></div>

</div>


绝对定位居中(知道宽高)

<style>

.box{

position:relative;

width:500px;

height:500px;

}

.content{

position:absolute;

left:50%;

right:50%;

width:100px;

height:100px;

margin-left:-50px;

margin-top:-50px;

}

</style>


flex布局实现

<style>

.box{

display:flex;

justify-content: center;

align-items: center;

width:500px;

height:500px;

background-color: red;

}

.content{

/*长度随意*/

width:100px;

/*宽度随意*/

height:100px;

background-color: #fff;

}

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