html - 防止中间容器沉入底部容器
问题描述
我以为我找到了一种方法,可以在调整窗口大小时防止中间容器沉入顶部容器和底部容器
但我意识到我弄乱了边距的方式以及我拥有 CSS 的方式它只适用于顶部容器,所以我怎样才能让它适用于底部容器的意义
防止中间容器沉入底部容器,这就是我的滚动条被切断的原因。我做了
底部容器是透明的,所以你们可以明白我的意思,是的,我知道如果我继续缩小
顶部容器和底部容器的窗口大小最终会相互碰撞。我不介意,尤其是因为
当您将窗口缩小到我也不在乎的程度时,中间容器看起来就像消失了
我只是关注您是否仍然可以看到中间容器。
这是我的代码
#container{
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: red;
height: 100%;
width: 80%;
margin-left: auto;
margin-right: auto;
}
#top-container{
background-color: gold;
position: absolute;
top: 0;
height: 50px;
width: 100%;
}
#middle-container{
background-color: pink;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin-top: 50px;
margin-bottom: 50px;
height: 87%;
width: 100%;
overflow-y: auto;
overflow-x: hidden;
}
#bottom-container{
background-color: rgba(0, 255, 0, 0.5);
height: 50px;
width: 100%;
position: absolute;
bottom: 0;
}
<div id='container'>
<div id='top-container'></div><!--</top-container>-->
<div id='middle-container'>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
</div><!--</middel-container>-->
<div id='bottom-container'></div><!--</bottom-container>-->
</div><!--</container>-->
解决方案
您不需要设置#middle-container 的高度或边距。它的位置是绝对的,所以top: 50px
它在#top-container 之后开始(高度为50px)并bottom: 50px
在#bottom-container 之前结束。
#container{
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: red;
height: 100%;
width: 80%;
margin-left: auto;
margin-right: auto;
}
#top-container{
background-color: gold;
position: absolute;
top: 0;
height: 50px;
width: 100%;
}
#middle-container{
background-color: pink;
position: absolute;
top: 50px;
left: 0;
right: 0;
bottom: 50px;
width: 100%;
overflow-y: auto;
overflow-x: hidden;
}
#bottom-container{
background-color: rgba(0, 255, 0, 0.5);
height: 50px;
width: 100%;
position: absolute;
bottom: 0;
}
<div id='container'>
<div id='top-container'></div><!--</top-container>-->
<div id='middle-container'>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
<h1>x</h1>
</div><!--</middel-container>-->
<div id='bottom-container'></div><!--</bottom-container>-->
</div><!--</container>-->
推荐阅读
- python - 如何将 TD Ameritrade 的 API 时间戳转换为 pandas 日期时间?
- typescript - NestJS - 测试套件无法运行从“comment/comment.entity.ts”中找不到模块“src/article/article.entity”
- scala - 改进关于特征的丑陋参数列表
- javascript - 在 React-Native 中从 Firebase 登录/注销的最佳方式?
- mysql - Nodejs:结合两种不同类型的 SQL 结果
- r - 在一系列更新后,rcpp 无法编译 c++ 代码
- mips - Mips SLL 算子
- r - 用函数编写绘图
- mql4 - 如何每天只计算一次指标缓冲区 mql4
- javascript - Swiper幻灯片自定义计数器(分数)