Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为主体提供100%的浏览器高度_Javascript_Jquery_Html_Css_Dom - Fatal编程技术网

Javascript 为主体提供100%的浏览器高度

Javascript 为主体提供100%的浏览器高度,javascript,jquery,html,css,dom,Javascript,Jquery,Html,Css,Dom,我有这个: html, body { height: 100%; //and this -> 100vh } 我想要这个: html, body { height: 100%; //and this -> 100vh } 我试过这个: html, body { height: 100%; //and this -> 100vh } 但它不起作用 这是我的代码: html, body { height: 100%; //and this

我有这个:

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

我想要这个:

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

我试过这个:

html, body
{
    height: 100%; //and this -> 100vh
}
但它不起作用

这是我的代码:

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


有什么解决方案吗?

不理想,但您可以使用绝对定位:

.content {
  position: absolute;
  top: 20px;
  bottom: 0;
}
或者,如果您对支持ie9+很感兴趣,可以使用视口百分比:

.content {
  height: 100vh;
}
样式应该在内容部分,而不是html/正文中


编辑:

我不知道这是否是您想要的,但请看一下:

“使用vw/vh,我们可以根据产品的尺寸来确定元件的尺寸 视口。vw/vh单位很有趣,因为1个单位反映了视口的1/100>宽度。要使元素成为视口的全宽 例如,您可以将其设置为宽度:100vw。“

--Jonathan Snook,与CSS3的大众和VH单元一起确定尺寸

CSS:

HTML:


标题
菜单
内容
页脚

此问题很适合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>

标题
主要
页脚
结果:

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

  • Flexbox是一个IE10+解决方案
我使用flexbox属性解决了您的问题

.container, 
.row {
    display: flex;
    flex-direction: column;
    height: 100%;
}
#title, 
#footer {
    flex: none;
}
#content {
    flex: 1 0 auto;
}

您可以看到解决方案。

您是否尝试过在HTML中不知从何处为容器指定高度:100%-
。。。可能是复制粘贴错误。你的问题很有道理。你要的是
身体
100%的高度!请参阅CSS:您的代码实际上按照预期工作——但您的身体中没有任何内容。你的风格是这样的:“内容部分应该填充尽可能多的高度空间。”。添加一些内容,身体高度会增加。我想你所问的是,如果内容部分被固定到窗口的大小,不管该部分是否有任何内容。是吗?是的,我希望内容部分填充屏幕高度,即使其中没有任何内容。我希望标题+内容+页脚占据页面的100%,而不仅仅是内容部分。我不想要滚动条。我想要标题+内容+页脚占据100%的页面,而不仅仅是内容部分。我不想要滚动条。请查看下面@Michael P.Bazos的答案——如果您不需要传统浏览器支持,flexbox将是一个很好的解决方案。