I have a div element on my page with its height set to 100%. The height of the body is also set to 100%. The inner div has a background and all that and is different from the body background. This works for making the div height 100% of the browser screen height, but the problem is I have content inside that div that extends vertically beyond the browser screen height. When I scroll down, the div ends at the point at which you had to begin scrolling the page, but the content overflows beyond that. How do I make the div always go all the way to the bottom to fit the inner content?

这是我的CSS的简化:

body {
    height:100%;
    background:red;
}

#some_div {
    height:100%;
    background:black;
}

一旦我滚动页面,黑色就结束了,内容就会流向红色背景。不管我在#some_div上设置的位置是相对的还是绝对的,问题都发生了。#some_div中的内容大多是绝对定位的,并且它是从数据库动态生成的,因此无法预先知道它的高度。

编辑:下面是问题的截图:


当前回答

你也可以使用

 display: inline-block;

我的工作就是这个

其他回答

只需添加这两行在你的css id #some_div

display: block;
overflow: auto;

在那之后,你会得到你想要的!

#some_div {    
  height: fit-content;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content

现代浏览器支持“视口高度”单位。这将把div展开到可用的视口高度。我发现它比其他任何方法都可靠。

#some_div {
    height: 100vh;
    background: black;
}

设置高度为auto,最小高度为100%。这应该可以解决大多数浏览器的问题。

body {
  position: relative;
  height: auto;
  min-height: 100% !important;
}

你也可以这样做

display:block;
overflow:auto;
height: 100%;

这将包括您的每个动态div根据内容。 假设你有一个通用的div类,它会根据内容增加每个动态div的高度