盒子模型垂直居中的几种方法(记录一下)

174 阅读1分钟

1.使用JavaScript

<!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>
</head>
<style>
    body {
        height: 100%;
        overflow: hidden;
    }

    .box {
        box-sizing: border-box;
        width: 100px;
        height: 50px;
        line-height: 48px;
        font-size: 16px;
        text-align: center;
        border: 1px solid lightblue;
        background: lightcyan;
    }
</style>

<body>
    <div class="box" id="box">垂直居中</div>
</body>
<script>
    let HTML = document.documentElement,
        winW = HTML.clientWidth,
        winH = HTML.clientHeight,
        boxW = box.offsetWidth,
        boxH = box.offsetHeight;

    box.style.position = 'absolute';
    box.style.left = (winW - boxW) / 2 + 'px';
    box.style.top = (winH - boxH) / 2 + 'px';
</script>

</html>
   

======End======

2.核心是利用定位(使用css)

<!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>
</head>
<style>
    body {
        height: 100%;
        overflow: hidden;
    }

    .box {
        box-sizing: border-box;
        width: 100px;
        height: 50px;
        line-height: 48px;
        font-size: 16px;
        text-align: center;
        border: 1px solid lightblue;
        background: lightcyan;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
</style>

<body>
    <div class="box" id="box">垂直居中</div>
</body>

</html>

======End======

另外一种同样利用定位,不过有局限性(box的宽和高必须写,不能写百分比)

<!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>
</head>
<style>
    body {
        height: 100%;
        overflow: hidden;
    }

    .box {
        box-sizing: border-box;
        width: 100px;
        height: 50px;
        line-height: 48px;
        font-size: 16px;
        text-align: center;
        border: 1px solid lightblue;
        background: lightcyan;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
</style>

<body>
    <div class="box" id="box">垂直居中</div>
</body>

</html>

======End======

3.利用flex(有浏览器兼容问题),如果不考虑兼容性问题推荐使用flex

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            border: 1px solid #6d7973;
            background-color: #2a2a2a;
            margin: 150px auto;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>

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

</html>

ps:希望有所帮助!