Flexbox-儿童高度不是100%

时间:2018-08-19 19:38:16

标签: css flexbox

我有一个基本的flex布局

.container {
  display:flex;
  width:100%;
      justify-content: space-between;
   height:100%;
}

.left {
  width: calc(61.8034% - ( 0.38196600790794 * 20px ) );
}

.right {
width: calc(38.1966% - ( 0.61803399209206 * 20px ) );  
}

.left, .right {
  height:100%;
   height:100%;
}

.image_container {
  background:red;
   height:100%;
}
.image_container img {
  display:none;
}

.image_16x9_container{
    background:green;
    display: block;
    height: 0;
    padding-bottom: 56.25%;
    overflow: hidden;
}
<div class="container">

  <div class="left">
  <div class="image_container">
This is some example text
    
      </div>
  </div>
  
  <div class="right">
      <div class="image_16x9_container">
        <img src="https://picsum.photos/500/500">
      </div>
  </div>
  
</div>  

我想不出为什么左手div的高度不是100%,我认为制作父div伸缩会自动解决这个问题。

我要去哪里错了?

1 个答案:

答案 0 :(得分:2)

您是对的,但不必在弹性项目because it won't work上设置height:100%,并且会破坏默认的拉伸行为,默认情况下该行为使元素的高度为100%:

.container {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.left {
  width: calc(61.8034% - ( 0.38196600790794 * 20px));
}

.right {
  width: calc(38.1966% - ( 0.61803399209206 * 20px));
}

.image_container {
  background: red;
  height: 100%;
}

.image_container img {
  display: none;
}

.image_16x9_container {
  background: green;
  display: block;
  height: 0;
  padding-bottom: 56.25%;
  overflow: hidden;
}
<div class="container">

  <div class="left">
    <div class="image_container">
      This is some example text

    </div>
  </div>

  <div class="right">
    <div class="image_16x9_container">
      <img src="https://picsum.photos/500/500">
    </div>
  </div>

</div>