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/reset.css">
    8. <link rel="stylesheet" href="../css/72粘滞定位.css">
    9. </head>
    10. <body>
    11. <div class="nav">
    12. <ul>
    13. <li>
    14. <a href="#">HTML/CSS</a>
    15. </li>
    16. <li>
    17. <a href="#">Browser Side</a>
    18. </li>
    19. <li>
    20. <a href="#">Server Side</a>
    21. </li>
    22. <li>
    23. <a href="#">Programming</a>
    24. </li>
    25. <li>
    26. <a href="#">XML</a>
    27. </li>
    28. <li>
    29. <a href="#">Web Building</a>
    30. </li>
    31. <li>
    32. <a href="#">Reference</a>
    33. </li>
    34. </ul>
    35. </div>
    36. </body>
    37. </html>

    css:
    (1)粘滞定位,就像一个胶水一样,把某个元素粘在某个位置,即使拖动滚动条也不会使得该元素移出浏览器

    (2)用法:
    position: sticky;
    top: x; / 或者其他的三个方位/

    代码:

    body {
        height: 2000px;
    }
    
    .nav {
        width: 1210px;
        height: 48px;
        background-color: hsl(48, 10%, 90%);
        margin: 100px auto;
        position: sticky;
        top:10px;
    }
    
    .nav li a{
        display: block;
        float: left;
        padding: 0 39px;
    }
    
    /* .nav li:lastchild a {
        padding: 0 42px 0 42px;
    } */
    .nav li:last-child a{
        padding: 0 41px 0 41px;
    } 
    
    .nav li a:hover {
        background-color: #DEDDD9;
    }
    .nav li a{
        text-decoration: none;
        color: #868685;
        font-size: 18px;
        height: 48px;
        line-height: 48px;
    }
    

    image.png
    image.png