6种CSS居中的方式

99 阅读1分钟

前言

我们在日常的开发中,会遇到各种盒子居中的问题,而解决这些问题,是一个前端工程师必须会的东西。所以,我在这里总结了6总盒子居中的方式。用了flex布局,position方式,calc方式,table-cell的方式。

盒子居中的方式1

<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>盒子居中的方式1</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100vw;
            height: 100vh;
            background-color: green;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

盒子居中的方式2

<!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>盒子居中的方式2</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            position: relative;
            width: 100vw;
            height: 100vh;
            background-color: green;
        }
        .box1{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body></html>

盒子的居中方式3

<!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>盒子居中的方式3</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            position: relative;
            width: 100vw;
            height: 100vh;
            background-color: green;
        }
        .box1{
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body></html>

盒子居中方式4

<!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>盒子居中的方式4</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            display: table-cell;
            vertical-align: middle;
            width: 100vw;
            height: 100vh;
            background-color: green;
        }
        .box1{
            margin: 0 auto;
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body></html>

盒子居中方式5

<!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>盒子居中的方式5</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            display: grid;
            align-items: center;
            justify-items: center;  
            width: 100vw;
            height: 100vh;
            background-color: green;
        }
        .box1{
            
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body></html>

盒子居中方式6

<!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>盒子居中的方式6</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
​
        .box {
            width: 100vw;
            height: 100vh;
            background-color: green;
            padding: calc((100vh - 100px)/2) 0;
            box-sizing: border-box;
        }
​
        .box1 {
            width: 100px;
            height: 100px;
            margin: 0 auto;
            background-color: red;
        }
    </style>
</head><body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

如果有什么不对的地方请大家指正。