英文:
Elements in flex container with predefined size
问题
我有一个包含两个容器的flex布局容器,如下所示:
.flex {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex: 1;
}
.msger {
  display: flex;
  flex-flow: column wrap;
  justify-content: space-between;
  width: 80%;
  max-width: 80%;
  margin: 25px 10px;
  height: calc(100% - 50px);
  border: var(--border);
  border-radius: 5px;
  background: var(--msger-bg);
  box-shadow: 0 15px 15px -5px rgba(0, 0, 0, 0.2);
  min-width: 80%;
}
.chart-container {
  width: 80%;
  min-width: 80%;
  max-width: 80%;
}
<div class="flex" id="flex">
  <section class="msger" id="msger_id">
    .....
  </section>
  <div class="chart-container" id="chart">
    ....
  </div>
</div>
现在的问题是所有元素都挤在一起。我希望msger占满整个屏幕高度,然后chart-container应该添加在下面,并且应该占据所需的空间(总共两个容器将占据超过屏幕高度的空间)。为此,我已经将msger添加了height: calc(100% - 50px);,但不知何故它没有占满整个屏幕高度。
如何实现这个目标?
英文:
I have a flex box container that contains two containers as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.flex {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex: 1;
}
.msger {
  display: flex;
  flex-flow: column wrap;
  justify-content: space-between;
  width: 80%;
  max-width: 80%;
  margin: 25px 10px;
  height: calc(100% - 50px);
  border: var(--border);
  border-radius: 5px;
  background: var(--msger-bg);
  box-shadow: 0 15px 15px -5px rgba(0, 0, 0, 0.2);
  min-width: 80%;
}
.chart-container {
  width: 80%;
  min-width: 80%;
  max-width: 80%;
}
<!-- language: lang-html -->
<div class="flex" id="flex">
  <section class="msger" id="msger_id">
    .....
  </section>
  <div class="chart-container" id="chart">
    ....
  </div>
</div>
<!-- end snippet -->
The problem now is that everything is squeezed together. I would like that msger fills the full height of the screen and then chart-container should be added below and should take as much space as it needs (in total both containers will take more than the height of the screen). For this purpose I have added height: calc(100% - 50px); to msger but somehow it does not take the full screen height.
How can this be done?
答案1
得分: 1
一切挤在一起的原因是 'display: flex' 在 flex 类中。由于 'flex' 元素的高度未设置,它只占用内容的高度。
要解决此问题,您可以在 'flex' 元素中添加 'height: 100vh;'。这将使 flex 元素使用屏幕的高度。
.flex {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex: 1;
  height: 100vh;
}
英文:
The reason everything is squeezed together is because of 'display: flex' in flex class. Since the height of the 'flex' element is not set, it is taking only the height of the content.
To solve this, you can add 'height: 100vh;' to the 'flex' element. this would make the flex element use the height of the screen.
.flex {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex: 1;
  height: 100vh;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论