子元素相对于父元素固定定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>子元素相对于父元素定位</title>
    <style>
        .assistor {
        position: relative; /*关键点*/
        display: block;
        width: 500px;
        height: 300px;
        margin: 100px auto 0 auto;
        background-color: #ddd;
        }
        .parent {
        width: 500px;
        height: 300px;
        background-color: #888;
        overflow: auto; /*关键点*/
        }
        .child {
        position: absolute; /*关键点*/
        width: 120px;
        height: 120px;
        margin: 100px 50px;
        background-color: #ffff00;
        }
        .placeholder {
        width: 1000px;
        height: 1000px;
        }
    </style>
</head>
<body>
    <div class="assistor">
        <div class="parent">
          <p>我是子元素1</p>
          <div class="child">我是子元素2</div>
          <div class="placeholder"></div>
        </div>
      </div>
</body>
</html>
发布了180 篇原创文章 · 获赞 36 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weixin_38404899/article/details/103693593