javascript - 给 body 100% 的浏览器高度

标签 javascript jquery html css dom

我有这个:

enter image description here

我想要这个:

enter image description here

我试过这个:

html, body
{
    height: 100%; //and this -> 100vh
}

但是没用。

这是我的代码:

https://jsfiddle.net/zbjaaxe6/9/

有什么解决办法吗?

最佳答案

这个问题很适合flexbox:

CSS

body {
    display: flex;
    flex-direction: column;
    margin: 0;
    min-height: 100vh;   // set min-height instead of height, otherwise body won't
                         // grow with content, if content is bigger than viewport
}

header {
    height: 50px;  // adjust to what you want
}

main {
    flex: 1;       // This will make the 'main' block to expand !
}

footer {
    height: 30px;  // adjust to what you want
}

HTML

<body>
    <header>HEADER</header>
    <main>MAIN</main>
    <footer>FOOTER</footer>
</body>

结果:

enter image description here

关于javascript - 给 body 100% 的浏览器高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33067777/

相关文章:

javascript - 获取已解析的 XML 并将其放入 javascript 数组中?

javascript - 在精细 uploader 中动态设置端点

javascript - 在 Javascript 中编辑城市、州或邮政编码组合字段

javascript - 使用 jquery 重置 html 元素

javascript - 打印后清除网页

html - 是否可以通过css和html实现如下文字效果?

javascript - 汉堡菜单仅适用于一个链接

javascript - 在多维数组中查找条目,其中属性的一部分与给定文本匹配

javascript - jQuery ajaxComplete() 是否检测到 prototypejs ajax 调用?

javascript - jQuery - 链接*仅*在一段时间后才工作