position: static;
position: fixed; /* 脱离文档流,是根据窗口定位 */
position: relative;/* 不脱离文档流,是根据自身定位 */
position: absolute; /* 根据非static的父元素来定位,否则根据浏览器进行定位,在滚动时不会发生偏移*/
position: sticky; /*当视口滚动到sticky元素设置的top位置时将脱离文档流变成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>
* {
margin: 0px;
font-size: 24px;
}
main {
width: 1200px;
height: 3000px;
/* height:500px; */
/* overflow:auto; */
background-color: pink;
margin: 0 auto;
position: relative;
}
main .tips {
position: sticky;
top: 30px;
height: 100px;
width: 100px;
background-color: violet;
}
.content {
height: 300px;
}
</style>
</head>
<body>
<main>
<section class="content">
</section>
<div class="tips">
</div>
<div style="background-color:red" class="tips">
</div>
<section class="content">
</section>
<section class="content">
</section>
<section class="content">
</section>
</main>
</body>
</html>