css - 子 div 中的滚动条不适用
问题描述
我正在向你伸出援手...
我有一个叠加层(包含两个 div),它应该需要尽可能多的高度,但不能大于屏幕。如果内容太大,我需要一个滚动条,但我只想要第二个 div的滚动条。
HTML
<div class="parent">
<div class="child1"></div>
<div class="child2">This div contains a long text, longer than there is space.</div>
</div>
CSS
.parent {
position: absolute;
width: 250px;
max-height: calc(100% - 50px);
/* overflow-y: auto;*/ -> This would work, but this is not what I want
}
.child1 {
width: 100%; -> this is an image that can have different heights
}
.child2 {
width: 100%;
overflow-y: auto;
}
经过我所有的尝试,我只设法为整个父 div 获得了一个滚动条,但我只希望它用于第二个 div。我猜第二个 div 不了解其父级的高度,因此忽略它。有谁知道如何解决这个问题?
解决方案
像这样?是的,您的父母需要有一个孩子可以理解的高度,才能使用百分比高度。下面这两个示例中的关键是height
在父元素上定义 a,在 CSS 中,因为默认情况下它设置为auto
,这不会让子元素弄清楚它们应该做什么。
此外,由于您的第一个 div 的高度固定为50px
,因此很容易将较低的、可滚动的 div 设置为一个calc
值,使其占据父级的 100%,减去其上方 div 的高度。
最后,我还包含了一个在示例的 CSS 中注释掉的 flexbox 实现,所以也请看一下,如果您对此有任何疑问,请告诉我!
* {
box-sizing: border-box;
}
.parent {
background-color: blue;
position: absolute;
border: 2px solid black;
padding: 5px;
width: 250px;
height: 100%;
max-height: 100vh;
}
.child1 {
background-color: red;
height: 50px;
}
.child2 {
padding: 6px;
background-color: yellow;
height: calc(100% - 50px);
overflow-y: scroll;
}
/*
Here is an implementation using flexbox, as well,
if your browser support allows it
.parent {
background-color: blue;
position: absolute;
border: 2px solid black;
padding: 5px;
width: 250px;
height: 100%;
max-height: 100vh;
display: flex;
flex-direction: column
}
.child1 {
background-color: red;
height: 50px;
flex-shrink: 0;
}
.child2 {
padding: 6px;
background-color: yellow;
overflow-y: scroll;
flex-grow: 1;
flex-shrink: 1;
}
*/
<div class="parent">
<div class="child1"></div>
<div class="child2">
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
This div contains a long text, longer than there is space.
</div>
</div>
推荐阅读
- javascript - 我在 javascript 中创建了没有 eval() 的计算器。现在我想在那里存储历史(以前的计算)
- php - Typo3 - 访问不同页面上内容元素的图像?
- flutter - 无法从缓存 Flutter 中删除 PDF 文件
- mpi - openmpi 中的 -cpu-set 无法正常工作
- performance - 操作寄存器的正确方法(PUT32 vs GPIO->ODR)
- java - VLCJ 从单个 Java 程序控制多个音频文件
- firebase - Firebase Nuxt SSR App 刷新后返回根页面
- python - 用序列信息表示 pandas 数据帧中的流程图
- r - R插入符号训练-但每个样本都有三个单独的测量值,我想使用多数票来预测
- javascript - 如何从 [rgb(248, 215, 218)][rgb(13, 202, 240)][rgb{1,2,3)] 的字符串中提取三个数字?