绝对位置的页面填充高度

Posted

技术标签:

【中文标题】绝对位置的页面填充高度【英文标题】:Filling height of page with position absolute 【发布时间】:2021-04-05 22:52:58 【问题描述】:

即使页面有滚动条并且我滚动到底部,我如何定位 div 以占据页面高度的 100%。我的 div 是模态的,位于导航栏下方,我将最小高度设置为 100vh,但是当我向下滚动时,它似乎没有扩展到页面底部。我使用的绝对位置不是固定的,因为我希望它相对于导航栏。

    <header className =styles.header>
    <span className=styles.header__item onClick=() => handleTab(1)>Library</span>
    <div className=styles.img onClick=()=>handleOptions()>                   
    </div>  
    <input className=styles.search type ="text" placeholder="Search"></input> 
    <PlayerModal open=playerOpen/>
    </header> 


.modalStyles
    min-height: 100vh;
    width: 26vw;
    position: absolute;
    left: 74%;
    top: 100%;
    background-color: whitesmoke;

【问题讨论】:

100vh 表示 100% 的视图高度,即所有页面的整体可见部分(可以是移动端全屏的桌面窗口) 【参考方案1】:

您可以使用位置粘性而不是绝对位置并将其设置在导航的底部。

#sticky 
  position: sticky;
  position: -webkit-sticky;
  background: #f83d23;
  width: 100px;
  height: 100px;
  top: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 0 6px #000;
  color: #fff;

.extra,
#wrapper 
  width: 75%;
  margin: auto;
  background-color: #ccc;

#wrapper 
  height: 800px;

.extra 
  height: 100px;

body 
  font-family: georgia;
  height: 1000px;

h4 
  text-align: center;

@media (min-height: 768px) 
  #wrapper
    height: 2000px;
  
<h4>Scroll to see the sticky element <em>sticking</em></h4>

<div id="wrapper">
  <div id="sticky">
    sticky
  </div>
</div>
<br />
<div class="extra"></div>

【讨论】:

以上是关于绝对位置的页面填充高度的主要内容,如果未能解决你的问题,请参考以下文章

div用绝对值填充父级的剩余高度

JS中获取 DOM 元素的绝对位置实例详解

Bootstrap 4:可滚动行,填充剩余高度

位置:绝对和父高度?

关于html和body高度 登录页面登录框永远保持在页面垂直水平居中位置

让一个 div 填充剩余屏幕空间的高度