【问题标题】:How to make a scrollable element fill 100% of the remaining height of a parent element?如何使可滚动元素填充父元素剩余高度的 100%?
【发布时间】:2018-04-05 16:16:15
【问题描述】:

我有一个固定高度的容器 div。它有一些内容和另一个子元素。我希望子元素在填充剩余高度时滚动。

我想出了一个似乎可行的解决方案,但我不确定它是否正确。

<style type="text/css">
  #container {height: 100px; width: 100px; border: solid; }
  #titlebar { background: gray;}
  #app-body {height: 100px; overflow: auto; background: lightblue;}    
</style>

工作fiddle编辑:更新小提琴以删除样式标签。

有没有更好的方法?我不喜欢在子 div 中重述容器高度。

【问题讨论】:

  • 你的小提琴坏了。 &lt;style&gt; 标签不应进入 CSS 面板。
  • 事实上,你的 CSS 渲染得更糟:jsfiddle.net/millimoose/6VKUt
  • 无论如何,我认为你别无选择。 overflow 根本不会在没有限制大小的元素上做任何事情,而且我不确定在 CSS 中是否有办法说“剩余空间的 100%”。 (没有使用 flexbox 或表格。)经验法则是 CSS 在使用垂直空间方面很糟糕。
  • 一个不幸的是不可行的 flexbox 解决方案:jsfiddle.net/millimoose/6VKUt/1。 (我的建议是放弃并使用表格,直到 CSS 不再适用于非基于列的网格的布局。)
  • 谢谢。如果可以的话,我会将该 flexbox 解决方案标记为可接受的答案。

标签: css


【解决方案1】:

显示:表格可能很有用:
演示 http://jsfiddle.net/GCyrillus/Vedkw/1
Firefox 不喜欢它,来自 display:table-row 的子项,行为类似于 table-cell,逻辑

html, body {height:100%;width:100%;margin:0;}
#container {display:table;height:100%;width:100%;}
#titlebar, #body {display:table-row;height:100%;}
/* overwrite height of titlebar */
#titlebar{height:auto;}

编辑:请稍微挖掘一下,这是一个基本布局,您可以使用它。b
#body 的内部内容可以滚动。

【讨论】:

  • 你可以添加一个带有 table-row 和 height:auto 的页脚;它会粘在底部
  • 孩子的卷轴到底在哪里?从 OP I would like the child element to scroll when it has filled the remaining height. 阅读此内容
  • -1:这并没有真正限制容器的宽度。 (当我尝试这样做时,表格只会扩大:jsfiddle.net/millimoose/Vedkw/2
  • 嗯,要使用一个基地。从那里,添加内部 div 并看到它滚动
  • @GCyrillus 如何使用基础,并提供对所述问题的答案,而不是仅仅朝着它的方向迈出一步。 (此外,您需要将答案中的链接更新为当前版本的小提琴。)
【解决方案2】:

这是一个实验性的 CSS 可能性说明 - 我将容器的高度和宽度加宽以更清楚地看到它。

 #container {
      height: 200px;
      width: 200px;
      border: solid;
    overflow: auto;
  }
  #titlebar {
      background: gray;
  }
  #body {
      height:calc(100% - 4em); /* definitely non standard - the 4 em is an approximate size of the titlebar*/
      overflow: auto;
      background: lightblue;
  }

/*Old answer - disregard */

/*#container {
    height: 100px;
    width: 100px;
    border: solid;
    overflow:auto;   /* overflow belongs here *//*
}
#titlebar {
    background: gray;
}
#app-body {

    background: lightblue;
}*/

【讨论】:

  • -1:这实际上并没有做 OP 想要做的事情,即只让内容滚动,并让标题栏保持原位。
  • @millimoose:OP 没有说明这一点。以上确实会滚动子内容。我不是读心术,我不会假装知道他想要完成什么
  • @frostro 成为文本阅读器就足够了! “['container div'] 有一些内容和另一个子元素。我希望子元素在填充剩余高度时滚动。”与小提琴中样本的结构相关,“容器 div”指的是#containersome content 指的是#titlebar,而another child element 似乎是#body。最后两个可能是相反的,但很明显应该滚动的元素是 something else 而不是#container
  • millimoose 是正确的。我本可以更好地表达这个问题。 +1 尝试。
  • 关于您的编辑。不幸的是,我不会提前知道标题栏的高度,所以这对我不起作用。
【解决方案3】:

你在找这个吗:Fiddle

我认为这只有在您设置#body 的高度时才有可能。否则它只是流动,overflow: scrolloverflow: hidden#container 无效。要设置它的高度,您必须知道/设置#titlebar 的高度。

我觉得jQuery可以轻松解决这个身高问题。

CSS

#container {
    height: 250px;
    border: solid;
    /*overflow: hidden*/ /* <-- not really needed if height of sub-elements*/
                         /* is <= child of container */
}
#titlebar {
    height: 60px;
    background: gray;
}
#body {
    height: 190px;
    overflow-y: scroll;
}

编辑

我认为仅通过 CSS 无法使 block 元素保持剩余高度,因为除非指定,否则元素会占用所需的高度而不是可用的高度。

如果您可以使用脚本,请参阅this fiddle

jQuery

var $body = $('div#body');
var h = $body.parent('div').height()
        - $body.siblings('div#titlebar').height();
$body.height(h);

CSS

#container {
    height: 250px;
    border: solid;
    overflow: hidden
}
#titlebar {
    background: gray;
}
#body {
    overflow-y: scroll;
}

【讨论】:

  • OP 说他不想重述孩子们的容器高度。我理解这暗示他不想在容器以外的任何地方使用明确的高度。 IE。让标题栏“根据需要”具有高度,并让内容具有“其余部分”。使用精确的像素尺寸和绝对定位可以“解决”任何 CSS 问题,但很少是可行的方法/
  • 我不提前知道标题栏内容的高度,所以很遗憾这不起作用。我将使用 javascript 或 flexbox。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2013-01-23
  • 2019-08-26
  • 2019-07-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多