首页 > 其他 > 详细

div 居中

时间:2018-11-13 01:48:46      阅读:170      评论:0      收藏:0      [点我收藏+]

方法1 margin

.box{
    width: 100px;
    height: 100px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -50px;
    margin-top: -50px;
}           

方法2 CSS3 transform-translate

.box {
    width: 100px;
    height: 100px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

方法3 flex

.box {
    display: flex;
    background-color: red;
    height: 400px;
    width: 400px;
    align-items: center;
    justify-content: center;
}

.box2 {
    height: 100px;
    width: 100px;
    background-color: pink;
}

<div class="box">
    <div class="box2">
    </div>
</div>

方法4 转换为inline

.box {
    background-color: red;
    height: 400px;
    width: 400px;
    text-align: center;
    line-height: 400px;
}

.box2 {
    display: inline;
    background-color: pink;
}
<div class="box">
    <div class="box2">
        内容内容
    </div>
</div>

div 居中

原文:https://www.cnblogs.com/studysuper/p/9949608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!