Skip to content

水平垂直居中

用 CSS 实现水平垂直居中有很多方法,具体选择取决于你想要居中的元素类型和你支持的浏览器。以下是几种常见的方法及其对应的 CSS 代码:

1. 弹性布局

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹性布局</title>
    <style>
        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

2. 网格布局

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网格布局</title>
    <style>
        .container {
            display: grid;
            place-items: center;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

3. 定位+margin

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位+margin</title>
    <style>
        .container {
            position: relative;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

4. 绝对布局

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绝对布局</title>
    <style>
        .container {
            position: relative;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

5. 表格单元格

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表格单元格</title>
    <style>
        .container {
            display: table;
            width: 100%;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .box {
            display: table-cell;
            vertical-align: middle;
            text-align: center;
            background-color: lightgoldenrodyellow;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

6. 带有伪元素的内联块

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>带有伪元素的内联块</title>
    <style>
        .container {
            text-align: center;
            height: 100vh;
            border: 1px solid #ccc;
        }
        .container::before {
            content: '';
            display: inline-block;
            height: 100%;
            vertical-align: middle;
        }
        .box {
            display: inline-block;
            vertical-align: middle;
            width: 100px;
            height: 100px;
            background-color: lightseagreen;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">中心</div>
    </div>
</body>
</html>

Released under the MIT License.