固定定位

363 阅读1分钟

固定定位

不管页面如何卷动,它永远固定在哪里

position:fixed;

top:100px;

left:100px;

固定定位注意事项

固定定位只能以页面为参考点,没有子绝父相的性质

固定定位脱离标准文档流

固定定位小案例

<!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 {
            height:800px;
            /* background:blue; */
        }
        .block {
            width:80px;
            height:80px;
            line-height:80px;
            text-align:center;
            border:2px solid black;

        }
        .block:nth-child(1) {
            position:absolute;
            top:10px;
            left:20px;
        }
        .block:nth-child(2) {
            position:fixed;
            top:10px;
            left:20px;
        }
    </style>
</head>
<body>
    <div class="block">绝对定位</div>
    <div class="block">固定定位</div>
</body>
</html>