首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未指定高度的父级中的高度百分比

未指定高度的父级中的高度百分比
EN

Stack Overflow用户
提问于 2019-03-29 23:32:50
回答 1查看 40关注 0票数 0

我有一个非常简单的HTML和CSS:

代码语言:javascript
复制
html,
body {
  min-height: 100%;
  margin: 0;
  paddding: 0;
}

.container {
  background: grey;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.inner-container {
  background: red;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.box-1 {
  background: blue;
  margin-bottom: 10px;
  height: 150px;
}

.box-2 {
  background: green;
  min-height: 50%;
}
代码语言:javascript
复制
<div class="container">
  <div>HEADER</div>
  <div class="inner-container">
    <section class="box-1">
    </section>
    <section class="box-2">
    </section>
  </div>
  <div>FOOTER</div>
</div>

这是它的CodePen:https://codepen.io/Loreno/pen/VNZeGw

如您所见,"box-2“的层次结构中没有指定高度的元素。因此,box-2的"min- height : 50%“不起作用--高度仅为0。

如何解决这一问题并获得最小身高行为?

EN

回答 1

Stack Overflow用户

发布于 2019-03-30 00:03:28

只需添加编辑容器css代码-而不是高度: 100%使用高度: 100vw;

简短的解释:

vh (viewport height (vw是viewport width))指的是最小设备高度填充(如果你想让填充50%的设备宽度,你可以只使用-height: 50vw)。

%表示父。如果你写的宽度: 50%,该将填充50%的父目录。

代码语言:javascript
复制
html,
body {
  min-height: 100%;
  margin: 0;
  paddding: 0;
}

.container {
  background: grey;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.inner-container {
  background: red;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.box-1 {
  background: blue;
  margin-bottom: 10px;
  height: 150px;
}

.box-2 {
  background: green;
  min-height: 50%;
}
代码语言:javascript
复制
<div class="container">
  <div>HEADER</div>
  <div class="inner-container">
    <section class="box-1">
    </section>
    <section class="box-2">
    </section>
  </div>
  <div>FOOTER</div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55420818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档