flex box父包装器不填充页面的高度

时间:2016-12-28 07:43:57

标签: css flexbox

这是我的JSFiddle ... https://jsfiddle.net/5xt7pt7f/

我知道这里有很多事情,我会尽量把我的问题隔离开来。

有两个父包装...

<div class="video-section">
...
<div class="chat-section">

正如你在小提琴中看到的,它们的高度不是100%,它似乎有某种最大高度,因为它们并不总是填满页面。我的html结构就像......

 <div id="app">
     ...
     <div class="chat-navbar">....</div>

     <div class="chat-wrapper">

         <div class="video-section">

         <div class="chat-section">
     ...

#app是主要容器。我希望.chat-navbar位于顶部,.chat-wrapper位于顶部。我通过......完成了这个。

 div#app {
     display: flex;
     flex: 1;
     flex-direction: column; }

接下来我希望.video-section.chat-section并排,我通过以下方式实现了这一目标......

.chat-wrapper {
    display: flex;
    flex: 1;
    flex-direction: row;
}

但是,这两个包装器不会填充页面的高度。我不在乎内部的canvas元素是否填充页面,但我确实需要父包装器来填充页面。

这就是我当地的样子......

enter image description here

正如您所看到的,底部有很多空白区域,因为元素没有全高。我以为flex:1会解决这个问题。

3 个答案:

答案 0 :(得分:1)

尝试在包含display:flex;chat-navbar的div上使用chat-wrapper(我给它id="chatcon"),然后在flex-grow:1;上使用chat-wrapper。我评论了其中一幅画布,并将导航栏颜色更改为红色,以帮助注意区别。

这是一个codepen

body, html {
  margin: 0;
  font-family: 'Libre Baskerville', serif; }
#chatcon{
  min-height:100vh;
  display:flex;
  flex-direction:column;
}
a {
  text-decoration: none;
  margin: 5px; }

a:active, a:visited {
  color: inherit; }

.chat-wrapper {
  display: flex;
  flex: 1;
  flex-direction: row;
}

.chat-section {
  border: none;
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-left: 0.5em;
  display: flex;
  flex: 1;
  flex-direction: column;
  margin-left: 5px; }

div#app {
  display: flex;
  flex: 1;
  flex-direction: column; }

.chat-section ul {
  overflow-y: auto;
  flex: 1;
  margin-bottom: 0;
  padding-left: 0.5em; }

.chat-section ul li {
  text-decoration: none;
  list-style-type: none; }

.chat-section ul li + li {
  margin-top: 7px; }

.chat-section textarea {
  resize: none;
  border: none;
  border-top: 1px solid #ccc;
  box-sizing: border-box;
  margin-left: -0.5em; }

.chat-section textarea:focus {
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none; }

.video-section {
  display: flex;
  flex: 1;
  text-align: center; }

.videos {
  flex: 1;
  display: flex;
  max-width: 800px;
  flex-direction: column;
  align-content: stretch; }

.chat-navbar {
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-top: 1em;
  display: flex;
  flex: 1; }

.localCanvas, .remoteCanvas {
  background-color: black;
  margin-left: 5px;
  margin-right: 5px;
  -webkit-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75); }

.localCanvas {
  margin-top: 5px;
  margin-bottom: 5px; }

canvas.remoteCanvas {
  margin-top: 5px; }

.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  flex-direction: column;
  background: #004FF9;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, #FFF94C, #004FF9);
  /* Standard syntax */
  background-size: cover; }

.loginForm, .registerForm {
  width: 500px;
  height: 600px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  position: absolute;
  top: 130px;
  left: 0;
  right: 0; }

.loginWrap {
  width: 500px;
  float: left;
  height: 100%;
  background: rgba(255, 255, 255, 0.75);
  -webkit-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75); }

.bodyLogin {
  overflow: hidden;
  height: 100%; }

.bg_image {
  height: 1000px;
  width: 100%; }

h1 {
  font-family: 'Roboto', sans-serif;
  color: #87909c;
  font-weight: bolder;
  text-align: center;
  font-size: 1.5em;
  margin-top: 20px; }

img.logo_image {
  width: 500px; }

img.title_image {
  width: 200px;
  margin-left: auto;
  display: block;
  margin-right: auto;
  margin-top: -90px; }

label {
  font-size: 12px;
  font-weight: bold;
  color: #87909c;
  float: left;
  clear: both;
  font-family: 'Roboto', sans-serif;
  margin-left: 30px;
  margin-top: 15px; }

input[type="text"], input[type="password"] {
  float: left;
  margin-top: 15px;
  clear: both;
  margin-left: 30px;
  font-size: 1.3em;
  width: 80%;
  outline: none;
  border: none;
  padding: 10px;
  padding-left: 10px; }

.login_btn, .register_btn {
  float: left;
  clear: left;
  font-family: 'Roboto', sans-serif;
  background: #7289da;
  border: solid 1px #7289da;
  width: 84%;
  margin-left: 30px;
  margin-top: 50px;
  padding: 20px;
  border-radius: 5px;
  font-size: 1em;
  color: white;
  cursor: pointer; }

.registerLinkWrap, .loginLinkWrap {
  float: left;
  clear: left;
  margin-left: 30px; }

a.register_link, a.login_link {
  float: left;
  margin-top: 11px;
  font-size: 13px;
  color: black;
  font-family: 'Roboto', sans-serif; }

a.register_link:hover, a.login_link:hover {
  text-decoration: underline; }

p.register_sentence, p.login_sentence {
  clear: left;
  float: left;
  font-size: 12px;
  color: #3e4956;
  font-family: 'Roboto', sans-serif; }

.errUsername, .errPassword, .errRegister {
  float: left;
  margin-left: 30px;
  margin-top: 3px;
  color: red;
  font-size: 0.8em;
  font-family: 'Roboto', sans-serif; }

.reg_success {
  position: absolute;
  background: #f2fae3;
  width: 100%;
  text-align: center;
  padding: 10px;
  font-family: 'Roboto', sans-serif;
  color: #94b639;
  border-bottom: solid 3px #94b639; }

.reg_neutral {
  display: none; }

/*# sourceMappingURL=styles.css.map*/
.chat-navbar{
  flex-shrink:0;
  flex-grow:0;
  height:70px;
  background-color:red;
}
<div id="app">
  <div id="chatcon" data-reactroot="">
    <div class="chat-navbar">
      <a href="/">Logout</a>
    </div>
    <div class="chat-wrapper">
      <div class="video-section">
        <div class="videos">
          <canvas class="remoteCanvas" width="320" height="240"></canvas>
<!--           <canvas class="localCanvas" width="320" height="240"></canvas> -->
        </div>
      </div>

      <div class="chat-section">
        <ul>No messages</ul>
        <textarea placeholder="Type a message" rows="2"></textarea>
      </div>
</div></div></div>

答案 1 :(得分:1)

我发现您div#appdisplay: flexflex: 1了。请注意flex: 1在这里没有做任何事情,它是一个flex子属性。

因此,div#app填充页面需要一个高度,此处为min-height: 100vh。现在,您可以举例<div data-reactroot>元素flex: 1来填充其父级(淡蓝色)。

任何拥有display: flex; flex-direction: column父母的孩子只需要flex: 1来填充垂直空间,如果flex-direction: row它需要height: 100%,那么div#app { display: flex; flex-direction: column; min-height: 100vh; } div[data-reactroot] { flex: 1; background: lightblue; } 通常可以正常工作。< / p>

这两条规则显示了如何

body,
html {
  margin: 0;
  font-family: 'Libre Baskerville', serif;
}

a {
  text-decoration: none;
  margin: 5px;
}

a:active,
a:visited {
  color: inherit;
}

.chat-wrapper {
  display: flex;
  flex: 1;
  flex-direction: row;
}

.chat-section {
  border: none;
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-left: 0.5em;
  display: flex;
  flex: 1;
  flex-direction: column;
  margin-left: 5px;
}

div#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
div[data-reactroot] {
  flex: 1;
  background: lightblue;
}

.chat-section ul {
  overflow-y: auto;
  flex: 1;
  margin-bottom: 0;
  padding-left: 0.5em;
}

.chat-section ul li {
  text-decoration: none;
  list-style-type: none;
}

.chat-section ul li + li {
  margin-top: 7px;
}

.chat-section textarea {
  resize: none;
  border: none;
  border-top: 1px solid #ccc;
  box-sizing: border-box;
  margin-left: -0.5em;
}

.chat-section textarea:focus {
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}

.video-section {
  display: flex;
  flex: 1;
  text-align: center;
}

.videos {
  flex: 1;
  display: flex;
  max-width: 800px;
  flex-direction: column;
  align-content: stretch;
}

.chat-navbar {
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-top: 1em;
  display: flex;
  flex: 1;
}

.localCanvas,
.remoteCanvas {
  background-color: black;
  margin-left: 5px;
  margin-right: 5px;
  -webkit-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
}

.localCanvas {
  margin-top: 5px;
  margin-bottom: 5px;
}

canvas.remoteCanvas {
  margin-top: 5px;
}


.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  flex-direction: column;
  background: #004FF9;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, #FFF94C, #004FF9);
  /* Standard syntax */
  background-size: cover;
}

.loginForm,
.registerForm {
  width: 500px;
  height: 600px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  position: absolute;
  top: 130px;
  left: 0;
  right: 0;
}

.loginWrap {
  width: 500px;
  float: left;
  height: 100%;
  background: rgba(255, 255, 255, 0.75);
  -webkit-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
}

.bodyLogin {
  overflow: hidden;
  height: 100%;
}

.bg_image {
  height: 1000px;
  width: 100%;
}

h1 {
  font-family: 'Roboto', sans-serif;
  color: #87909c;
  font-weight: bolder;
  text-align: center;
  font-size: 1.5em;
  margin-top: 20px;
}

img.logo_image {
  width: 500px;
}

img.title_image {
  width: 200px;
  margin-left: auto;
  display: block;
  margin-right: auto;
  margin-top: -90px;
}

label {
  font-size: 12px;
  font-weight: bold;
  color: #87909c;
  float: left;
  clear: both;
  font-family: 'Roboto', sans-serif;
  margin-left: 30px;
  margin-top: 15px;
}

input[type="text"],
input[type="password"] {
  float: left;
  margin-top: 15px;
  clear: both;
  margin-left: 30px;
  font-size: 1.3em;
  width: 80%;
  outline: none;
  border: none;
  padding: 10px;
  padding-left: 10px;
}

.login_btn,
.register_btn {
  float: left;
  clear: left;
  font-family: 'Roboto', sans-serif;
  background: #7289da;
  border: solid 1px #7289da;
  width: 84%;
  margin-left: 30px;
  margin-top: 50px;
  padding: 20px;
  border-radius: 5px;
  font-size: 1em;
  color: white;
  cursor: pointer;
}

.registerLinkWrap,
.loginLinkWrap {
  float: left;
  clear: left;
  margin-left: 30px;
}

a.register_link,
a.login_link {
  float: left;
  margin-top: 11px;
  font-size: 13px;
  color: black;
  font-family: 'Roboto', sans-serif;
}

a.register_link:hover,
a.login_link:hover {
  text-decoration: underline;
}

p.register_sentence,
p.login_sentence {
  clear: left;
  float: left;
  font-size: 12px;
  color: #3e4956;
  font-family: 'Roboto', sans-serif;
}

.errUsername,
.errPassword,
.errRegister {
  float: left;
  margin-left: 30px;
  margin-top: 3px;
  color: red;
  font-size: 0.8em;
  font-family: 'Roboto', sans-serif;
}

.reg_success {
  position: absolute;
  background: #f2fae3;
  width: 100%;
  text-align: center;
  padding: 10px;
  font-family: 'Roboto', sans-serif;
  color: #94b639;
  border-bottom: solid 3px #94b639;
}

.reg_neutral {
  display: none;
}


/*# sourceMappingURL=styles.css.map*/

希望这清楚地表明它是如何工作的,从这里你可以在标记中应用相同的逻辑。

<div id="app">
  <div data-reactroot="">
    <div class="chat-navbar">
      <a href="/">Logout</a>
    </div>
    <div class="chat-wrapper">
      <div class="video-section">
        <div class="videos">
          <canvas class="remoteCanvas" width="320" height="240"></canvas>
          <canvas class="localCanvas" width="320" height="240"></canvas>
        </div>
      </div>

      <div class="chat-section">
        <ul>No messages</ul>
        <textarea placeholder="Type a message" rows="2"></textarea>
      </div>
    </div>
  </div>
</div>
import std.process;
auto workerId = spawnProcess(<path to worker as string>);
scope(exit)wait(workerId);
auto serverId = spawnProcess(<path to server as string>);
scope(exit)wait(serverId);

答案 2 :(得分:1)

为了简化您的要求,请在需要的地方添加相对高度,以便您的包装占据整个页面。

html, body { height: 100%; }解决大部分问题,然后再进一步解决。

为你创造了一个小提琴:https://jsfiddle.net/nashcheez/u7bmL0nm/

body,
html {
  margin: 0;
  font-family: 'Libre Baskerville', serif;
  height: 100%;
}
a {
  text-decoration: none;
  margin: 5px;
}
a:active,
a:visited {
  color: inherit;
}
.chat-wrapper {
  display: flex;
  flex: 1;
  flex-direction: row;
  height: 100%;
}
.chat-section {
  border: none;
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-left: 0.5em;
  display: flex;
  flex: 1;
  flex-direction: column;
  margin-left: 5px;
}
div#app {
  display: flex;
  flex: 1;
  flex-direction: column;
  height: 100%;
}
.chat-section ul {
  overflow-y: auto;
  flex: 1;
  margin-bottom: 0;
  padding-left: 0.5em;
}
.chat-section ul li {
  text-decoration: none;
  list-style-type: none;
}
.chat-section ul li + li {
  margin-top: 7px;
}
.chat-section textarea {
  resize: none;
  border: none;
  border-top: 1px solid #ccc;
  box-sizing: border-box;
  margin-left: -0.5em;
}
.chat-section textarea:focus {
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}
.video-section {
  display: flex;
  flex: 1;
  text-align: center;
}
.videos {
  flex: 1;
  display: flex;
  max-width: 800px;
  flex-direction: column;
  align-content: stretch;
}
.chat-navbar {
  border-left: 1px solid #ccc;
  box-sizing: border-box;
  padding-top: 1em;
  display: flex;
  flex: 1;
}
.localCanvas,
.remoteCanvas {
  background-color: black;
  margin-left: 5px;
  margin-right: 5px;
  -webkit-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  box-shadow: 9px 5px 17px -6px rgba(0, 0, 0, 0.75);
  height: 50%;
}
.localCanvas {
  margin-top: 5px;
  margin-bottom: 5px;
}
canvas.remoteCanvas {
  margin-top: 5px;
}
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  flex-direction: column;
  background: #004FF9;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(to right, #FFF94C, #004FF9);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, #FFF94C, #004FF9);
  /* Standard syntax */
  background-size: cover;
}
.loginForm,
.registerForm {
  width: 500px;
  height: 600px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  position: absolute;
  top: 130px;
  left: 0;
  right: 0;
}
.loginWrap {
  width: 500px;
  float: left;
  height: 100%;
  background: rgba(255, 255, 255, 0.75);
  -webkit-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
  box-shadow: 4px 3px 14px -5px rgba(0, 0, 0, 0.75);
}
.bodyLogin {
  overflow: hidden;
  height: 100%;
}
.bg_image {
  height: 1000px;
  width: 100%;
}
h1 {
  font-family: 'Roboto', sans-serif;
  color: #87909c;
  font-weight: bolder;
  text-align: center;
  font-size: 1.5em;
  margin-top: 20px;
}
img.logo_image {
  width: 500px;
}
img.title_image {
  width: 200px;
  margin-left: auto;
  display: block;
  margin-right: auto;
  margin-top: -90px;
}
label {
  font-size: 12px;
  font-weight: bold;
  color: #87909c;
  float: left;
  clear: both;
  font-family: 'Roboto', sans-serif;
  margin-left: 30px;
  margin-top: 15px;
}
input[type="text"],
input[type="password"] {
  float: left;
  margin-top: 15px;
  clear: both;
  margin-left: 30px;
  font-size: 1.3em;
  width: 80%;
  outline: none;
  border: none;
  padding: 10px;
  padding-left: 10px;
}
.login_btn,
.register_btn {
  float: left;
  clear: left;
  font-family: 'Roboto', sans-serif;
  background: #7289da;
  border: solid 1px #7289da;
  width: 84%;
  margin-left: 30px;
  margin-top: 50px;
  padding: 20px;
  border-radius: 5px;
  font-size: 1em;
  color: white;
  cursor: pointer;
}
.registerLinkWrap,
.loginLinkWrap {
  float: left;
  clear: left;
  margin-left: 30px;
}
a.register_link,
a.login_link {
  float: left;
  margin-top: 11px;
  font-size: 13px;
  color: black;
  font-family: 'Roboto', sans-serif;
}
a.register_link:hover,
a.login_link:hover {
  text-decoration: underline;
}
p.register_sentence,
p.login_sentence {
  clear: left;
  float: left;
  font-size: 12px;
  color: #3e4956;
  font-family: 'Roboto', sans-serif;
}
.errUsername,
.errPassword,
.errRegister {
  float: left;
  margin-left: 30px;
  margin-top: 3px;
  color: red;
  font-size: 0.8em;
  font-family: 'Roboto', sans-serif;
}
.reg_success {
  position: absolute;
  background: #f2fae3;
  width: 100%;
  text-align: center;
  padding: 10px;
  font-family: 'Roboto', sans-serif;
  color: #94b639;
  border-bottom: solid 3px #94b639;
}
.reg_neutral {
  display: none;
}
<div id="app">
  <div style="height: 100%;" data-reactroot="">
    <div class="chat-navbar">
      <a href="/">Logout</a>
    </div>
    <div class="chat-wrapper">
      <div class="video-section">
        <div class="videos">
          <canvas class="remoteCanvas" width="320" height="240"></canvas>
          <canvas class="localCanvas" width="320" height="240"></canvas>
        </div>
      </div>
      <div class="chat-section">
        <ul>No messages</ul>
        <textarea placeholder="Type a message" rows="2"></textarea>
      </div>
    </div>
  </div>
</div>