Flexbox item with overflowing content only works on Chrome

拟墨画扇 提交于 2019-12-01 16:49:14

Three things to consider:

  1. An initial setting on flex items is flex-shrink: 1. This means that items are permitted to shrink in order to create more space in the container. To disable this feature use flex-shrink: 0.

    See this post for a full explanation: What are the differences between flex-basis and width?

  2. An initial setting on flex items is min-height: auto. This means that items cannot be smaller than the height of their content. To override this behavior use min-height: 0.

    See this post for a full explanation: Why doesn't flex item shrink past content size?

  3. In your code, Firefox and Edge are adhering strictly to the spec. Chrome, it appears, considers the spec a foundation, but factors in common sense scenarios and expected user behavior.

For your layout to work across browsers, make the following adjustments:

body{ margin: 0;}

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.top-bar {
  background-color: darksalmon;
  height: 50px;
  flex-shrink: 0; /* NEW */
  /* Or remove height and flex-shrink and just use this: 
     flex: 0 0 50px; */
}

.inner-container {
  flex: 1;
  background-color: chocolate;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0; /* NEW */
}

.top {
  background-color: blueviolet;
  flex: 1;
  overflow: auto;
  font-size: 40px;
  line-height: 5rem;
}

.bottom {
  background-color: darkkhaki;
  /* height: 200px; */
  flex: 0 0 200px; /* NEW */
  
}
<div class="container">
  <div class="top-bar">Top bar</div>
  <div class="inner-container">
    <div class="top">
O<br>v<br>e<br>r<br>f<br>l<br>o<br>w<br>i<br>n<br>g<br>C<br>o<br>n<br>t<br>e<br>n<br>t
    </div>
    <div class="bottom">Bottom part</div>
  </div>
</div>

revised pen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!