如何使嵌套的
占用可用高度?

huangapple go评论64阅读模式
英文:

How to have nested div take up available height?

问题

我想让.expandToFill div扩展以填充剩余的屏幕高度。我使用了flexbox构建了一些东西,因为这似乎是最直接的解决方案。必须嵌套在这种方式中,请不要更改HTML。

假设我不知道.menu div的高度值。

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}
<div class="wrapper">
  <div class="router">
    <div class="container">
      <div class="menu">
      </div>
      <div class="expandToFill">
      </div>
    </div>
  </div>
</div>

你可以在这个链接中查看示例:jsfiddle链接

编辑:
我曾接受了一个答案,但意识到它没有完全解决这个问题。我认为这是一个非常常见的问题,应该有一个完全可行的解决方案。

英文:

I want to have the .expandToFill div expand to fill the remaining screen height. I built something with flexbox as this seems like the most straightforward solution. It has to be nested in this way, please don't change the html.

Say I don't know the height value of the .menu div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;router&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;menu&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;expandToFill&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

https://jsfiddle.net/BAR24/a2m4cjsw/17/

There are similar questions on SO but they don't ask about nested expandable divs.

Edit:
I had accepted an answer but realized it didn't fully resolve this problem. I think its a very common issue and it deserves a fully working solution.

答案1

得分: 1

Sure, here is the translated code:

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%; /* &lt;&lt;------------ HERE */
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}
<div class="wrapper">
  <div class="router">
    <div class="container">
      <div class="menu">
      </div>
      <div class="expandToFill">
      </div>
    </div>
  </div>
</div>

Please note that I have retained the code structure and only translated the comments in the CSS part.

英文:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%; /* &lt;&lt;------------ HERE */
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;router&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;menu&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;expandToFill&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

以下是您要翻译的内容:

"解决问题的最简单方法似乎是添加:

.wrapper * {
height: 100%;
}

/* 开始代码片段:js 隐藏:false 控制台:true babel:false */
/* 语言:lang-css */
*,::before,::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.wrapper * {
  height: 100%;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}
/* 语言:lang-html */
<div class="wrapper">
  <div class="router">
    <div class="container">
      <div class="menu">
      </div>
      <div class="expandToFill">
      </div>
    </div>
  </div>
</div>
/* 结束代码片段 */

JS Fiddle演示

这种方法的原因是,它确保所有元素占用了可用的完整垂直空间(除了.menu,其高度已经明确设置),这允许规则:

.expandToFill {
flex-grow: 1;
}

生效,因为现在元素已经有了一个可以扩展的定义空间。"

英文:

The easiest way that seems to solve the problem, is to add:

.wrapper * {
  height: 100%;
}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

*,::before,::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.wrapper * {
  height: 100%;
}

.router {
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: column;
}

.menu {
  background-color: red;
  height: 20px;
}

.expandToFill {
  flex-grow: 1;
  background-color: blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;router&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;menu&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;expandToFill&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

The reason this works, is because it ensures that all elements take the full vertical space that's available (with the exception of .menu, which has its height set specifically), which allows the rule:

.expandToFill {
  flex-grow: 1;
}

To take effect, as the element now has a defined space into which it can expand.

huangapple
  • 本文由 发表于 2023年4月17日 08:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定