容器垂直居中的方式

本文介绍了七种不同的方法来实现HTML/CSS中的容器垂直居中效果,包括绝对定位结合负外边距、transform:translateY、margin自动、padding、flex布局、flex改变主轴方向以及display:table-cell。每种方法都有其适用场景和特点,如需在网页设计中实现元素的垂直居中,这些技巧非常实用。

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

容器垂直居中的方式

方式一:绝对定位+负外边距(需要知道居中容器的高度)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            height: 300px;
            background-color: aqua;
            position: relative;
        }
        .child{
            width: 150px;
            height: 100px;
            background-color: greenyellow;
            position: absolute;
            top: 50%;
            margin-top: -50px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child"></div>
    </div>
</body>
</html>

方式二:绝对定位+transform: translateY(-50%);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            height: 300px;
            background-color: aqua;
            position: relative;
        }
        .child{
            font-size: 100px;
            background-color: greenyellow;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child">WM</div>
    </div>
</body>
</html>

方式三:绝对定位+margin:auto 0;

需要居中容器的top和bottom值相等,如果居中容器没有设置高度,则高度会铺满父容器剩余高度空间。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            height: 300px;
            background-color: aqua;
            position: relative;
        }
        .child{
            font-size: 100px;
            background-color: greenyellow;
            position: absolute;
            top: 11px;
            bottom: 11px;
            margin: auto 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child">WM</div>
    </div>
</body>
</html>

方式四:padding

父容器高度不用设置,需知道居中容器高度

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            background-color: aqua;
            padding: 100px 0;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="child">WM</div>
    </div>
</body>
</html>

方式五:flex布局

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background-color: aqua;
            display: flex;
            align-items: center;
        }

        .child {
            font-size: 100px;
            background-color: greenyellow;
        }
    </style>
</head>

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

</html>

方式六:flex布局改变主轴方向

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background-color: aqua;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .child {
            font-size: 100px;
            background-color: greenyellow;
        }
    </style>
</head>

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

</html>

方式七:使用display: table-cell;vertical-align: middle;对容器内文本垂直居中

需要给父容器设置display: table;

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background-color: aqua;
            display: table;
        }

        .child {
            font-size: 50px;
            display: table-cell;
            vertical-align: middle;
        }
    </style>
</head>

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

</html>

方式八:设置line-height等于容器高度使容器内文本垂直居中

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 100px;
            background-color: aqua;
            line-height: 100px;
        }
    </style>
</head>

<body>
    <div class="box">
        上下上下
    </div>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值