子div在父div中水平垂直居中的问题

本文详细介绍了使用CSS实现元素在父容器中水平垂直居中的多种方法,包括利用margin、绝对定位、Flex布局、表格布局等,适用于不同场景下的布局需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结:

div如何在另一个div中水平垂直居中?
          1)利用父绝子相对,子div   margin:auto; 上下左右都为0
          2)利用父绝子相对,子div   margin:auto;  left:50%;top:50%;margin-top:-子元素高度的一半;margin-left:-子元素宽度的一半;
         3)伸缩盒布局:父元素display:flex;justify-content:center; align-items:center;
         4)利用表格布局:父元素 display:table-cell;  vertical-align:middle;   text-align: center;  子元素:display:inline-block;              

文字如何在div中水平垂直居中?

                 水平:text-align:center;

                 垂直:line-height:父div高度;

方法一、 CSS3 transform和position(元素宽高未知的情况)

<div class="box">
    <span>宽高未知</span>
</div>

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    position:relative;
}
.box span {
    background: red;
    position: absolute;
    left: 50%;
    top: 50%;
    color: #fff;
    transform: translate(-50%, -50%);
}

方法二、 绝对定位和margin-left:-自身宽度一半,margin-top: -自身高度的一半 (已知宽高)

<div class="box">
    <span>宽高已知</span>
</div>

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    position: relative;
}
.box span {
    display: inline-block;
    width: 100px;
    height: 100px;
    background: red;
    text-align: center;
    line-height: 100px;
    position: absolute;
    left: 50%;
    top: 50%;
    color: #fff;
    margin-left: -50px;
    margin-top: -50px;
}

方法三、 css3 flex布局

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

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    display:flex;
    justify-content:center;
    align-items: center;
}
.box .child {
    display: inline-block;
    width: 100px;
    height: 100px;
    background: red;
}

方法四、 被居中的元素是inline或者inline-block元素

<div class="box">
    <span class="child">inline和inline-block元素</span>
</div>

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.box .child {
    display: inline-block;
    width: 100px;
    height: 100px;
    background: red;
}

方法五、绝对定位和margin:auto

<div class="box">
    <div class="child">定高</div>
</div>

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    position: relative;
}
.box .child {
    width: 100px;
    height: 100px;
    background: red;
    text-align: center;
    color: #fff;
    line-height: 100px;
    position: absolute;
    top:0;
    right:0;
    bottom:0;
    left:0;
    margin: auto;
}

方法六、line-height: 父级高度和text-align: center

<div class="box">
    <span class="child">line-height</span>
</div>

.box {
    width: 400px;
    height: 400px;
    background: #ccc;
    line-height: 400px;
    text-align: center;
    font-size: 0;
}
.box .child {
    background: red;
    font-size: 14px;
    color: #fff;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值