1、固定定位
<!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>
/* 固定定位
固定定位是元素固定于浏览器可视区的位置。主要使用场景: 可以在浏览器页面滚动时元素的位置不会改变。
特点:
1. 以浏览器的可视窗口为参照点移动元素。
跟父元素没有任何关系
不随滚动条滚动。
2. 固定定位不在占有原先的位置。 */
img {
position: fixed;
top: 50px;
left: 50px;
}
</style>
</head>
<body>
<img src="images/pvp.png" alt="">
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
</body>
</html>
2、绝对定位
<!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>绝对定位</title>
<style>
/* 绝对定位
绝对定位是元素在移动位置的时候,是相对于它祖先元素来说的
特点:
1. 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准定位(Document 文档)。
2. 如果祖先元素有定位(相对、绝对、固定定位),则以最近一级的有定位祖先元素为参考点移动位置。
3. 绝对定位不再占有原先的位置。(脱标) */
.father {
width: 400px;
height: 400px;
margin: 100px auto;
background-color: gray;
}
.father .son-one {
position: absolute;
top: 50px;
left: 100px;
width: 600px;
height: 200px;
background-color: skyblue;
}
.father .son-two {
width: 300px;
height: 300px;
background-color: tan;
}
.father .son-one .grandson {
position: absolute;
top: 20px;
right: 50px;
width: 50px;
height: 50px;
background-color: red;
}
</style>
</head>
<body>
<div class="father">
<div class="son-one">
<div class="grandson">
</div>
</div>
<div class="son-two"></div>
</div>
</body>
</html>
效果:
3、相对定位
<!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>相对定位</title>
<style>
/* 相对定位
特点:
1. 它是相对于自己原来的位置来移动的(移动位置的时候参照点是自己原来的位置)。
2. 原来在标准流的位置继续占有,后面的盒子仍然以标准流的方式对待它。(不脱标) */
.one {
position: relative;
top: 100px;
left: 50px;
width: 200px;
height: 200px;
background-color: skyblue;
}
.two {
width: 300px;
height: 300px;
background-color: tan;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
</body>
</html>
4、粘性定位
<!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>
/* 粘性定位
特点:
1. 以浏览器的可视窗口为参照点移动元素(固定定位特点)
2. 粘性定位占有原先的位置(相对定位特点)
3. 必须添加 top 、left、right、bottom 其中一个才有效
跟页面滚动搭配使用。 兼容性较差,IE 不支持。 */
.nav {
position: sticky;
top: 0;
margin: 100px auto;
width: 900px;
height: 50px;
background-color: skyblue;
}
.box {
height: 2000px;
}
</style>
</head>
<body>
<div class="nav">导航栏</div>
<div class="box"></div>
</body>
</html>