经常会有一个需求,在一个固定窗口内容滚动,底下有操作按钮需要固定,不随着滚动,一般第一个念头就是用固定定位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>
网友评论