html:
    代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. <link rel="stylesheet" href="../css/71固定定位.css">
    8. </head>
    9. <body>
    10. <div class="box1">
    11. <div class="box2">
    12. <div class="box3"></div>
    13. </div>
    14. </div>
    15. </body>
    16. </html>

    css:
    (1)基本和绝对相似,但不同点在于,position: fixed; 是永远相对于浏览器的视口进行定位的。即如果出现上下浮动条,该模块也不会移动。
    代码:

    .box1 {
        width: 300px;
        height: 1300px;
        background-color: blueviolet;
    }
    
    .box2 {
        width: 200px;
        height: 200px;
        background-color: brown;
        position: relative;
    }
    
    .box3 {
        width: 100px;
        height: 100px;
        background-color: cornflowerblue;
        position: fixed;
        left: 30px;
        top: 50px;
    }
    

    效果:
    image.png
    image.png