不要乱用position:fixed

经常会有一个需求,在一个固定窗口内容滚动,底下有操作按钮需要固定,不随着滚动,一般第一个念头就是用固定定位position: fixed,但是fixed是以窗口为父元素去定位的,这么做肯定是错的,需要正确使用相对定位和绝对定位


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.wrap {
    position: relative;
    width: 200px;
    border: 1px solid #ccc;
}
.content{
    width: 200px;
    height: 300px;
    overflow: auto;
    margin-bottom: 50px;
    border: 1px solid #ccc;
}
.item {
    width: 100%;
    height: 100px;
    background: red;
    margin-bottom: 10px;
}
.btn {
    position: absolute;
    bottom: 0;
    background: #fff;
    width: 100%;
    height: 50px;
}
</style>
</head>
<body>
<div class="wrap">
    <div class="content">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="btn">按钮</div>
    </div>
</div>
</body>
</html>

复制代码

转载于:https://juejin.im/post/5d27f4ecf265da1ba84abcd4

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值