元素的高度可以改变,只要它在弹性盒内吗?

时间:2016-11-28 14:13:03

标签: html css flexbox

在下面的代码片段中,我希望红色区域与黄色区域具有相同的高度:



html,
body {
  height: 100%;
  margin: 0
}

.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.box .row {
  border: 1px dotted grey;
}

.box .row.header {
  flex: 0 1 auto;
  /* The above is shorthand for:
  flex-grow: 0,
  flex-shrink: 1,
  flex-basis: auto
  */
}

.box .row.content {
  flex: 1 1 auto;
  background-color:yellow;
}

.box .row.footer {
  flex: 0 1 40px;
}

<div class="box">
  <div class="row header">
    <p><b>header</b>
      <br />
      <br />(sized to content)</p>
  </div>
  <div class="row content">
    <div style="height:100%; width:90%; background-color:red;">
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>
&#13;
&#13;
&#13;

它有style="height:100%但它的行为与height:auto相似,可能是因为flexbox。我能以某种方式调整它的高度吗?

3 个答案:

答案 0 :(得分:1)

在.row.content div上设置display:flex以使其成为弹性容器

由于align-items的默认值为stretch - 项目将占据全高。

html,
body {
  height: 100%;
  margin: 0
}

.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.box .row {
  border: 1px dotted grey;
}

.box .row.header {
  flex: 0 1 auto;
  /* The above is shorthand for:
  flex-grow: 0,
  flex-shrink: 1,
  flex-basis: auto
  */
}

.box .row.content {
  flex: 1 1 auto;
  background-color:yellow;
  display: flex; /* <----- added */
}

.box .row.footer {
  flex: 0 1 40px;
}
<div class="box">
  <div class="row header">
    <p><b>header</b>
      <br />
      <br />(sized to content)</p>
  </div>
  <div class="row content">
    <div style="width:90%; background-color:red;">
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>

答案 1 :(得分:1)

您还可以将子元素设置为使用position: absolute(并在父级上使用position: relative。)

html,
body {
  height: 100%;
  margin: 0
}

.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.box .row {
  border: 1px dotted grey;
}

.box .row.header {
  flex: 0 1 auto;
  /* The above is shorthand for:
  flex-grow: 0,
  flex-shrink: 1,
  flex-basis: auto
  */
}

.box .row.content {
  flex: 1 1 auto;
  background-color:yellow;
  position: relative;
}

.box .row.content div {
  height: 100%;
  width: 90%;
  background-color:red;
  position: absolute;
}

.box .row.footer {
  flex: 0 1 40px;
}
<div class="box">
  <div class="row header">
    <p><b>header</b>
      <br />
      <br />(sized to content)</p>
  </div>
  <div class="row content">
    <div>
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>

答案 2 :(得分:1)

您的内容基于灵活性为100%

html,
body {
  height: 100%;
  margin: 0
}
.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}
.box .row {
  border: 1px dotted grey;
}
.box .row.header {
  flex: 0 1 auto;
  /* The above is shorthand for:
  flex-grow: 0,
  flex-shrink: 1,
  flex-basis: auto
  */
}
.box .row.content {
  flex: 1 1 100%;
  background-color: yellow;
}
.box .row.footer {
  flex: 0 1 40px;
}
<div class="box">
  <div class="row header">
    <p><b>header</b>
      <br />
      <br />(sized to content)</p>
  </div>
  <div class="row content">
    <div style="height:100%; width:90%; background-color:red;">
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>

希望有所帮助