关于css:CSSsticky布局

2次阅读

共计 928 个字符,预计需要花费 3 分钟才能阅读完成。

position: sticky; top:0;性能:
1)失常状况下, sticky 元素在页面中失常布局;
2)当 sticky 元素顶部间隔浏览器页面顶部为设定值 (此处为 0) 时, 体现得像 fixed;
3)当 sticky 元素底部与包裹它的容器底部相触时, 恢复正常;
以下示意图能够更好的感触到它的成果:

demo 代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
    <title>Document</title>
    <style>
        body {margin: 0;}
        .container {
            position: relative;
            margin: 0 auto;
            width: 500px;
            height: 2500px;
            background-color: red;
        }
        .footer {
            position: relative;
            margin: 0 auto;
            width: 500px;
            height: 2500px;
            background-color: rgb(76, 31, 160);
        }
        .header {
            width: 300px;
            height: 500px;
            margin: 0 auto 20px;
            background-color: green;
        }
        .content {
            position: sticky;
            top: 0;
            height: 220px;
            width: 300px;
            margin: 0 auto;
            background-color: rgb(90, 79, 238);
        }
    </style>
</head>

<body>
    <body>
        <div class="container">
            <div class="header">header</div>
            <div class="content">content</div>
        </div>
        <div class="footer">footer</div>
    </body>
    <script>
    </script>
</body>

</html>
正文完
 0