html - 溢出导致整个页面滚动
问题描述
我正在尝试创建一个不滚动的页面。页面上的某些子元素可以滚动,但我试图阻止整个页面滚动。我有一个非常嵌套的子元素,当它溢出时会收到一个滚动条,但也会导致主文档增长并收到一个滚动条。
这是问题的核心,但还有一些嵌套级别可能是一个因素。
<div class="h-100 d-flex flex-column">
<div class="d-flex align-items-center justify-content-center bg-red" style="height: 7%">
</div>
<div class="d-flex align-items-center justify-content-center bg-red" style="height: 3%">
</div>
<div class="bg-green" style="max-height: 75%; height: 75%; overflow-y: auto;">
<div class="bg-gray m-4" style="height: 2000px;">
The height of this content causes BOTH scroll bars to appear. I only want a bar on the
'green section'
</div>
</div>
<div class="bg-red flex-grow-1">
</div>
</div>
此代码笔演示了我的应用程序是如何设置的。溢出元素嵌套在许多 flex 显示中(来自 bootstrap 4 实用程序类)。
https://codepen.io/averyferrante/pen/YMdNpO
我只希望代码笔中的绿色部分滚动,而不是整个文档也增长/滚动。
解决方案
问题是您的几个容器缺少高度定义。因此,这些容器的子容器会忽略应用于它们的百分比高度。
将此添加到您的代码中:
<div class="flex-grow-1" style="height: calc(100% - 48px);">
此高度规则为容器提供定义的高度,同时补偿其兄弟的高度。
另一个高度规则缺少三个层次:
<div class="d-flex flex-column w-100" style="height: 100%;">
更详细的解释:
推荐阅读
- java - 在 Android Studio 中实现导航抽屉的问题
- azure-resource-manager - ARM ADF 部署功能
- python - Python - Librosa:不能反转 MFCC
- html - 当链接中包含元素时,单击 css 更改链接的背景颜色
- python-3.x - 动态创建 GPIO PIN 变量
- reactjs - 使用 React JS 删除超过 2 天的 Firestore 文档
- css - Bootstrap 中 flex-fill 和 flex-grow-1 的区别
- jenkins - 通过管道(Jenkins)恢复vsphere的快照
- c - 在 C 中找到下一个素数?
- oauth-2.0 - Google API google-auth 身份验证永远等待,因为重定向到本地主机