6.2、绝对定位
定位:基于xxx定位,上下左右
1.没有父级元素定位的前提下,相对于浏览器定位
2.假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3.在父级元素范围内移动
相对于父级或者浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 相对定位:相对于自己原来的位置进行偏移-->
<style>
body {
padding: 20px;
}
div {
margin:10px;
padding:5px;
font-size: 12px;
line-height: 25px;
}
#father {
border: 1px solid #666;
padding: 0;
position:relative;
}
#first {
border: 1px dashed #661b50;
background-color: red;
}
#second {
border: 1px dashed #426615;
background-color: orange;
position: absolute;
right: 30px;
left:-10px;
}
#third {
border: 1px dashed #171a66;
background-color: yellow;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
效果图:
6.3、固定定位:fixed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
height:1000px;
}
div:nth-of-type(1) {/*绝对定位:相对于浏览器*/
width:100px;
height:100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2) {/*fixed,固定定位*/
width:50px;
height:50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
效果图:
应用场景: