为什么我的div不占用其父容器的100%高度?

时间:2016-12-09 05:40:50

标签: html css css3 flexbox vertical-alignment

这里有点困惑。我很累,所以我确定我犯了一个愚蠢的错误,但这是我的HTML / CSS:

.CAContainer {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: center;
  margin: 0;
  padding: 0;
}
.CASideBorder {
  height: 100%;
  background: #000;
  width: 8px;
}
.CAMiddleContainer {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}
.CATopBorder {
  height: 8px;
  background: #000;
  width: 100%;
}
.CAMiddleTextContainer {
  padding: 40px 80px 40px 80px;
  font-size: 4em;
  color: #000;
}
.CABottomBorderContainer {
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 8px;
}
.CABottomBorderLeft,
.CABottomBorderRight {
  flex: 1;
  height: 100%;
  background: #000;
}
<div class="CAContainer">
  <div class="CASideBorder" id="CALeftBorder"></div>
  <div class="CAMiddleContainer">
    <div class="CATopBorder"></div>
    <div class="CAMiddleTextContainer">
      text
    </div>
    <div class="CABottomBorderContainer">
      <div class="CABottomBorderLeft"></div>
      <div class="CABottomBorderRight"></div>
    </div>
  </div>
  <div class="CASideBorder" id="CARightBorder"></div>
</div>

fiddle

CASideBorder不应占用其父容器的100%,其容器的高度是根据文本大小和CAMiddleTextContainer的填充量计算的?我错过了一些非常明显的东西吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

这是因为你对flexbox有align-items: center,在这种情况下它将默认为内容高度。

您需要做的是将其删除(让align-items: stretch作为默认设置发挥作用),同时从height:100%中移除CASideBorder

见下面的演示:

&#13;
&#13;
.CAContainer {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  margin: 0;
  padding: 0;
}

.CASideBorder {
  background: #000;
  width: 8px;
}

.CAMiddleContainer {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}

.CATopBorder {
  height: 8px;
  background: #000;
  width: 100%;
}

.CAMiddleTextContainer {
  padding: 40px 80px 40px 80px;
  font-size: 4em;
  color: #000;
}

.CABottomBorderContainer {
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 8px;
}

.CABottomBorderLeft, .CABottomBorderRight {
  flex: 1;
  height: 100%;
  background: #000;
}
&#13;
<div class="CAContainer">
  <div class="CASideBorder" id="CALeftBorder"></div>
  <div class="CAMiddleContainer">
    <div class="CATopBorder"></div>
    <div class="CAMiddleTextContainer">
      text
    </div>
    <div class="CABottomBorderContainer">
      <div class="CABottomBorderLeft"></div>
      <div class="CABottomBorderRight"></div>
    </div>
  </div>
  <div class="CASideBorder" id="CARightBorder"></div>
</div>
&#13;
&#13;
&#13;