如何使带有top属性的div相对屏幕100%高度且滚动内容完整可见
问题解决:Fixed定位Div底部内容被遮挡
问题出在你的Div样式设置冲突:设置了top:70px的同时,又给了height:100%和max-height:100vh,这会让Div从顶部70px的位置开始,向下延伸整个视口高度,导致Div底部超出视口,内容被截断。
修复方案
方案1:用calc()计算精确高度
直接通过calc()把顶部偏移的70px从视口高度中扣除,确保Div刚好填满剩余的视口空间:
div { position: fixed; top: 70px; left: 0; width: 100%; height: calc(100vh - 70px); /* 关键修改 */ background-color: rgba(0, 0, 0, 0.9); padding: 30px; overflow: auto; }
方案2:使用inset简写属性(更简洁)
用CSS的inset属性直接定义四个边的位置,自动让Div填充从top70px到视口底部的区域:
div { position: fixed; inset: 70px 0 0 0; /* top:70px, right:0, bottom:0, left:0 */ width: 100%; background-color: rgba(0, 0, 0, 0.9); padding: 30px; overflow: auto; }
修复后完整代码
CSS
*, *::before, *::after { padding: 0; margin: 0; border: 0; box-sizing: border-box; } div { position: fixed; top: 70px; left: 0; width: 100%; height: calc(100vh - 70px); background-color: rgba(0, 0, 0, 0.9); padding: 30px; overflow: auto; } div::before { content: ""; position: fixed; width: 100%; top: 0; left: 0; height: 70px; background-color: #222; z-index: 2; }
HTML
<div> <ul> <li><a href="">Section #1</a></li> <li><a href="">Section #2</a></li> <li><a href="">Section #3</a></li> <li><a href="">Page #1</a></li> <li><a href="">Page #2</a></li> <li><a href="">Page #3</a></li> <li><a href="">Page #4</a></li> <li><a href="">Page #5</a></li> <li><a href="">Page #6</a></li> </ul> </div>
内容的提问来源于stack exchange,提问作者user228
相关产品推荐
相关产品推荐

