如何使孩子的Flexbox父级高度为100%,而不给每个孩子增加100%的身高

时间:2018-08-30 09:55:12

标签: html css twitter-bootstrap-3

我的网站上有一行带有嵌套列的行,该行具有引导程序3。.column类具有背景。我希望背景色在不更改HTML的情况下能填满整个高度。

如果我将height: 100%;添加到嵌套行,bootstrap col及其列类中,我会得到想要的结果,但是写起来更容易吗?

这是我的代码

https://codepen.io/Insane415/pen/rZWYOE

<div class="container">
   <div class="row sub-heading">
      <div class="col-xs-12 col-sm-12 col-md-6">
         <div class="column">
            Lorem Ipsum Lorem Ipsum <br>Break
         </div>
      </div>
      <div class="col-xs-12 col-sm-12 col-md-6">
         <div class="row sub-heading full-height">
            <div class="col-xs-4 full-height">
               <div class="column full-height"></div>
            </div>
            <div class="col-xs-4">
               <div class="column"></div>
            </div>
            <div class="col-xs-4">
               <div class="column"></div>
            </div>
         </div>
      </div>
   </div>
</div>

.sub-heading {
  display: flex;
}

.column {
  background: #fce6cc;
  position: relative;
  margin: 0 -13px;
  min-height: 30px;
  padding: 0 15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.full-height {
  height: 100%;
}

2 个答案:

答案 0 :(得分:1)

不更改HTML:

.sub-heading {
  height: 100%;
}

.sub-heading > div {
  background: pink;
}

.column {
  position: relative;
  margin: 0 -13px;
  min-height: 30px;
  padding: 0 15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

@media screen and (min-width: 768px) {
  .sub-heading {
    display: flex;
  }
  
  .sub-heading > div {
    border-left: 5px solid #fff;
    border-right: 15px solid #fff;
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
   <div class="row sub-heading">
      <div class="col-xs-12 col-sm-12 col-md-6">
         <div class="column">
            Lorem Ipsum Lorem Ipsum <br>Break
         </div>
      </div>
      
     <div class="col-xs-12 col-sm-12 col-md-6">
         <div class="row sub-heading full-height">
            <div class="col-xs-4 full-height">
               <div class="column full-height"></div>
            </div>
            <div class="col-xs-4">
               <div class="column"></div>
            </div>
            <div class="col-xs-4">
               <div class="column"></div>
            </div>
         </div>
      </div>
   </div>
</div>

答案 1 :(得分:-1)

据我了解,我分享了摘录,希望对您有所帮助

  .sub-heading {
                display: flex;
                    position: relative;
                    min-height: 100%;
                    border: 1px solid #eee;
              }
              
              .column {
                background: #fce6cc;
                position: relative;
                border:1px solid #333;
                margin: 0 -13px;
                min-height: 30px;
                padding: 0 15px;
                display: -webkit-box;
                display: -ms-flexbox;
                display: flex;
                -webkit-box-align: center;
                -ms-flex-align: center;
                align-items: center;
                -ms-flex-wrap: wrap;
                flex-wrap: wrap;
              }
              
              .sub-heading .column{
                min-height: 100%;
              }
<div class="container">
<div class="row sub-heading">
        <div class="col-xs-12 col-sm-12 col-md-6">
          <div class="column">
            Lorem Ipsum Lorem Ipsum <br>Break
          </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6">
          <div class="row sub-heading">
            <div class="col-xs-4">
              <div class="column"></div>
            </div>
            <div class="col-xs-4">
              <div class="column"></div>
            </div>
            <div class="col-xs-4">
              <div class="column"> </div>
            </div>
          </div>
        </div>
      </div>
</div>