阅读 319

盒子垂直水平居中的几种方法

一、定位

1、盒子宽高已知

<!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>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第一种 宽高已知 子绝父相 margin负间距 */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #f0f;
            background-color: steelblue;
        }
        .demo {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            margin-left: -100px;
            margin-top: -100px;
            background-color: thistle;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第二种 宽高未知:盒子本身有宽高,自己不知 子绝父相 */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #ff0;
            background-color: tomato;
        }
        .demo {
            position: absolute;
            /* 上下左右均0位置定位 */
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            width: 200px;
            height: 200px;
            margin: auto;
            background-color: turquoise;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第三种 平移+定位 */
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            border: 10px solid #0ff;
            background-color: violet;
        }
        .demo {
            position: absolute;
            /* 以盒子的左上角为原点定位,是左上角的原点居中,
            但是元素本身并不居中 */
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            /* 分别向左向上移动自身长宽的50%,使其位于中心 */
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
复制代码

二、margin+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>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第四种 标准流 margin 居中:子盒子宽度低于父盒子宽度,
        可以设置子盒子的 margin 值,水平方向的值都设置为 auto。 */
        .box {
            width: 400px;
            /* 垂直居中 */
            padding: 100px 0;
            border: 10px solid #00f;
        }
        .demo {
            width: 200px;
            height: 200px;
            /* 水平居中 */
            margin: 0 auto;
            background-color: springgreen;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
复制代码

三、平移

<!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>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第五种 平移 */
        .box {
            width: 400px;
            height: 400px;
            border: 10px solid #0ff;
            background-color: violet;
        }
        .demo {
            width: 200px;
            height: 200px;
            margin-left: 50%;
            margin-top: 50%;
            background-color: yellowgreen;
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
复制代码

四、table-cell布局

<!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>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第六种 table-cell布局 */
        .box {
            display: table-cell;
            vertical-align: middle;
            width: 400px;
            height: 400px;
            border: 10px solid rgb(0, 255, 98);
            background-color: rgb(238, 130, 197);
        }
        .demo {
            width: 200px;
            height: 200px;
            margin: 0 auto;
            background-color: rgb(50, 187, 205);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></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>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第七种 flex布局 */
        .box {
            display: flex;
            /* 设置主轴方向 主轴为垂直从上往下 y轴 */
            flex-direction: column;
            /* 侧轴,从中间加载 垂直居中 */
            align-items: center;
            /* 在主轴居中对齐 水平居中 */
            justify-content: center;
            width: 400px;
            height: 400px;
            border: 10px solid rgb(200, 255, 0);
            background-color: rgb(234, 130, 238);
        }
        .demo {
            width: 200px;
            height: 200px;
            background-color: rgb(50, 205, 146);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="demo"></div>
    </div>
</body>
</html>
复制代码
文章分类
前端
文章标签