gpt4 book ai didi

html - 子 DIV 用可滚动内容填充剩余高度

转载 作者:太空狗 更新时间:2023-10-29 13:46:15 24 4
gpt4 key购买 nike

我正在尝试设置一个菜单 div A,其下方有一个内容容器 div B。它们都嵌套在父 div C 中。当 div B 中的内容高于 div B 本身时,则需要在 div B 上显示一个滚动条。

illustration of problem.

我遇到的主要问题是 div A 的高度未知,并且可以在运行时通过 JavaScript 添加和删除内容时随时更改,我想要 div B 以填充剩余的父级高度,同时保持其内容的可滚动性。

我正在寻找一个纯 CSS 的解决方案,因为我正在处理的应用程序已经很重 JS,我希望避免添加更多的膨胀。

我的问题的简化版本: https://jsfiddle.net/uf3frdjv/

最佳答案

使用 flexbox 可以解决您的问题。

一个简单的例子:

.C {
display: flex; /* this enables flex layout */
flex-direction: column; /* child divs are placed in column */
height: 300px; /* Parent container must have a fixed height */
}
.A {
flex: 0 0 auto; /* div A should remain its original height (neither expand nor shrink) */
}
.B {
flex: 1 1 auto; /* div B should fit the remaining space */
overflow-y: auto; /* enable scroll bar when div D exceeds div B's height */
}

有关实际示例,请参阅此 fiddle

关于html - 子 DIV 用可滚动内容填充剩余高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34668540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com