将div设置为剩余窗口高度

时间:2019-12-18 13:17:45

标签: javascript html css

我正在尝试创建一个简单的网站,该网站的标头包含徽标和其他信息,然后是一个丝带作为分隔符,然后是一个两列的部分。左列将有一个选择器。右栏将包含信息,具体取决于选择器上选择的选项。

到目前为止,我已经能够做到这一点,但是现在我想将其提升到一个新的水平。 我希望我的网站始终适合窗口,因此不需要垂直滚动条。 我对标头和功能区的大小感到满意,因此我希望两列部分占用窗口中的剩余空间。如果需要,滚动条将位于信息div上。 以防万一,我希望左半部分(选择器)占用所需的空间。右半部分(信息)应调整为剩余空间。

到目前为止,我有此代码:

CSS

.container {
    display: table;
    width: 100%;
}

.left-half {
    display: table-cell;
    background-color:#7ec0ee;
}
.right-half {
    display: table-cell;
    overflow: auto;
}

HTML

<div id="header" style="background-color:#7ec0ee; padding-top:20px; padding-left:5px; padding-bottom:20px" align="center">
    <div id="header-logo" align="left">
        <img src=".." alt="MDN" width="160" height="113">
    </div>
</div>
<div id="black-banner" style="background-color:black; padding:2px">&nbsp;</div>
<div id="container" class="container">
    <div class="left-half">
        <select>..</select>
    </div>
    <div id="content" class="right-half"></div>
</div>

3 个答案:

答案 0 :(得分:1)

在这里,我使用了一些引导程序类。我认为这会帮助您。

<!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>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
        <nav class="navbar navbar-dark bg-dark navbar-expand-lg">
                <a class="navbar-brand" href="#">Navbar w/ text</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                  <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarText">
                  <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Features</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Pricing</a>
                    </li>
                  </ul>
                  <span class="navbar-text">
                    Navbar text with an inline element
                  </span>
                </div>
              </nav>
              <div class="container-fluid">
                    <div class="row">
                      <div class="col-sm" style="background-color: lavender">
                        One of three columns
                      </div>
                      <div class="col-sm" style="background-color: #EAE7EE">
                        One of three columns
                      </div>
                      <div class="col-sm" style="background-color: #E6E2EB">
                        One of three columns
                      </div>
                    </div>
                  </div>
</body> 
</html>

答案 1 :(得分:1)

我建议使用flex。对于响应式设计而言,这太棒了。您可以在左侧或右侧半部的flex属性中设置宽度。

.container {
  display: flex;
  width: 100%;
  height: calc(100vh - 98px); //98px is the height of your header
}

.left-half {
  flex: 1;
  background: red;
}

.right-half {
  flex: 1;
  padding: 1em;
  overflow: auto;
  background: blue;
}
<div id="header" style="background-color:#7ec0ee; padding-top:20px; padding-left:5px; padding-bottom:20px" align="center">
  <div id="header-logo" align="left">
    <img src=".." alt="MDN" width="160" height="113">
  </div>
</div>
<div id="black-banner" style="background-color:black; padding:2px">&nbsp;</div>
<div id="container" class="container">
  <div class="left-half">
    <select>..</select>
  </div>
  <div id="content" class="right-half"></div>
</div>

答案 2 :(得分:0)

这可能对您有用。您可以在CSS中使用calc函数。

.container {
    display: flex;
    width: 100%;
    height: calc(100vh - 100px); //Replace this 100px with whatever value elements other than container are occupying
}

.left-half {
  width:50%;
  background-color:#7ec0ee;
}
.right-half {
    background: red;
    width: 50%;
}