css实现垂直居中(面试的时候面试官经常会问的)

本文介绍了六种在网页设计中实现元素居中的方法,包括使用绝对定位加负margin、绝对定位加calc、绝对定位加transform、Flex布局、CSS Table布局及各自的优缺点对比,适用于不同场景和设备。

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

1.absolute + margin auto

<style>
    .wrap {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        margin:auto;
        bottom: 0;
        right: 0;
        left: 0;
        top: 0;
        position: absolute;
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

2.absolute + 负margin

<style>
    .wrap {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        margin-left: -50px;
        margin-top: -50px;
        top: 50%;
        left: 50%;
        position: absolute;
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

3.absolute + calc

<style>
    .wrap {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        top:calc(50% - 50px);
        left:calc(50% - 50px);
        position: absolute;
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

4.absolute + transform

<style>
    .wrap {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        top:50%;
        left:50%;
        position: absolute;
        transform: translate(-50%,-50%);
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

5.flex

<style>
    .wrap {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

6.css-table

<style>
    .wrap {
        display: table-cell;
        text-align: center;
        vertical-align: middle;
        width: 200px;
        height: 200px;
        border: 1px solid blueviolet;
    }
    .children {
        display: inline-block;
        width: 100px;
        height: 100px;
        background: beige;
    }
</style>
<body>
<div class="wrap">
    <div class="children">子元素</div>
</div>
</body>

总结:

这些是我经常使用的,如有新的方法,欢迎交流

对比优缺点:

1.pc端宽高固定建议使用:absolute + 负margin;

2.pc端无宽高:css-table;

3.无兼容性:flex;

4.移动端建议使用flex,真的超级好用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值