我有一个包含两列的布局-左div和右div。

右侧的div具有灰色背景色,我需要它根据用户浏览器窗口的高度垂直展开。现在,背景色结束于该分区中的最后一段内容。

我尝试过高度:100%,最小高度:100%;,等


当前回答

尝试此操作-已测试:

body {
  min-height: 100%;
}

#right, #left {
  height: 100%;
}

在以后的版本中,您可以使用vh:

#right, #left {
  height: 100vh
}

其他回答

你需要做两件事,一是将高度设置为你已经做到的100%。第二个是将位置设置为绝对位置。这应该会奏效。

html,
body {
  height: 100%;
  min-height: 100%;
  position: absolute;
}

来源

这对我有用:

html, body {
    height: 100%; /* IMPORTANT!!! Stretches viewport to 100% */
}

#wrapper {
    min-height: 100%; /* Minimum height for a modern browser */
    height:auto !important; /* Important rule for a modern browser */
    height:100%; /* Minimum height for Internet Explorer */
    overflow: hidden !important; /* Firefox scroll-bar */
}

摘自本页。

100vw=视口宽度的100%。

100vh=视口高度的100%。

如果要将div宽度或高度设置为浏览器窗口大小的100%,应使用:

宽度:100vw

高度:100vh

或者,如果您想将其设置得更小,请使用CSS calc函数。例子:

#example {
    width: calc(100vw - 32px)
}

现在使用高度:100vh;对于固定的窗口高度:

<style>
    .header-top {
        height: 100vh;
        background: #000;
        color: #FFF;
        display: flex;
        align-items: center;
        padding: 10px;
        justify-content: space-around;
    }

    .header-top ul {
        list-style: none;
        padding: 0;
        margin: 0;
        display: flex;
        align-items: center;
    }

    .header-top ul li {
        padding:0px 10px;
    }
</style>

<div class="header-top">
    <div class="logo">Hello</div>
    <ul>
        <li>Menu</li>
        <li>About Us</li>
        <li>Contact US</li>
        <li>Login</li>
    </ul>
</div>

只需使用“vh”单位而不是“px”,这意味着查看端口高度。

height: 100vh;