登录
首页 >  文章 >  前端

如何让子元素absolute根据父元素滚动内容的高度设置高度?

时间:2024-10-27 11:54:45 236浏览 收藏

推广推荐
免费电影APP ➜
支持 PC / 移动端,安全直达

今天golang学习网给大家带来了《如何让子元素absolute根据父元素滚动内容的高度设置高度?》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

如何让子元素absolute根据父元素滚动内容的高度设置高度?

子元素absolute根据父元素滚动内容的高度设置高度

为了让子元素absolute根据父元素滚动内容的高度设置高度,需要对父元素进行一些调整:

  1. 包裹滚动内容:

为滚动内容创建一个新的 div 元素,并将其定位为相对于父元素。这个新 div 设置 position: relative,使内容能够在其中进行定位。

  1. 移动overflow属性:

将 overflow: auto 属性从滚动内容的子元素移动到父元素上。这样,父元素就可以对内容进行滚动,而滚动条会出现在父元素上。

  1. 修改子元素定位:

将子元素absolute定位为相对于其新父元素。这样,子元素高度将自动调整为内容高度。

示例代码:

<div class="box">
  <div class="content">
    <div style="width: 100%; height: 300px; background-color: black; margin-bottom: 20px;"></div>
    <div style="width: 100%; height: 300px; background-color: black; margin-bottom: 20px;"></div>
    <div style="width: 100%; height: 300px; background-color: black; margin-bottom: 20px;"></div>
    <div style="width: 100%; height: 300px; background-color: black; margin-bottom: 20px;"></div>

    <div style="position: absolute; width: 100%; height: 100%; top: 0; background-color: aqua; opacity: 0.5;"></div>
  </div>
</div>

<style>
  .box {
    width: 100%;
    height: 100vh;

    display: flex;
    flex-direction: column;
    overflow: auto;
  }

  .content {
    position: relative;
    width: 100%;
  }
</style>

以上就是《如何让子元素absolute根据父元素滚动内容的高度设置高度?》的详细内容,更多关于的资料请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>