前端学习笔记54-固定定位

前端学习笔记54-固定定位


前面学了相对定位和绝对定位,接下来学习固定定位。
position:fixed

固定定位也是一种绝对定位,大部分特点与绝对定位一样。不同的是,固定定位的参照点是浏览器的视口。
视口是啥?
看代码

<!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>
        .box1{
            width: 100px;
            height: 100px;
            background-color: #bfa;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: fixed;
            top: 0;
            left:0;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: orchid;
        }
        .box4{
            height: 200px;
            width: 200px;
            background-color: peru;
            position: relative;
        }
        .box5{
            height: 150px;
            width: 150px;
            background-color: red;
            position: relative;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">
        4
        <div class="box5">
            <div class="box2">2</div>
        </div>
    </div>
    <div class="box3">3</div>
</body>
</html>

在这里插入图片描述
从上图可以看到,虽然给box4和box5都开启了相对定位,但是box2还是在左上角,所以它的参考点永远是浏览器视口的左上角。
这里可能解释不清楚视口,所以再给body设置高度。

        body{
            height: 2000px;
        }

在这里插入图片描述
我滑动了滚动条,然后其他几个box都看不见了,只有box2看得见。这就很好理解视口了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小欣CZX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值