移动端页面如何获取当前页面高度并赋值给div最小高度?

如果不设置一个最小高度,那如果内容不够,那底部的footer会上去,留下片空白。

阅读 3k
2 个回答

在移动端,可以使用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>
        * {
            padding: 0;
            margin: 0;
        }

        html,
        body {
            height: 100%;
        }

        .container {
            height: 100%;
            display: flex;
            flex-direction: column;
        }
        .body {
            flex: 1;
            background: red;
        }
        .footer {
            height: 100px;
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="body"></div>
        <div class="footer"></div>
    </div>
</body>

</html>
//css代码
html, body {
    height: 100%;
}
body {
    overflow-y: auto;
}
body div {
    min-height: 100%;
}
推荐问题