相对定位与绝对定位
relative(父级)
参照物:自己原来的位置
不脱离文档流:原来占的位置还在,别人不会挤过来
用 top / left / right / bottom 偏移,只相对于自己原本位置移动
absolute
参照物:最近一个有定位(非 static)的父元素
完全脱离文档流:原来的位置空出来,后面元素会补上来
必须配合 top/left/right/bottom 才生效
常用:弹窗、图标、悬浮按钮、布局层叠
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.parent{
margin: 100px auto;
width: 800px;
height: 400px;
border: 10px solid red;
position: relative;
}
.son1{
width: 50%;
height: 50%;
background-color: blue;
position: absolute;
/*向右移动*/
right: 0;
top: 0;
}
.son2{
width: 50%;
height: 50%;
background-color: orangered;
position: absolute;
/*向右移动*/
left: 0;
top: 0;
}
.son3{
width: 50%;
height: 50%;
background-color: yellow;
position: absolute;
/*向右移动*/
bottom: 0;
left:0 ;
}
.son4{
width: 50%;
height: 50%;
background-color: red;
position: absolute;
bottom: 0;
right:0 ;
/*向右移动*/
}
</style>
</head>
<body>
<div class="parent">
<div class="son1"></div>
<div class="son2"></div>
<div class="son3"></div>
<div class="son4"></div>
</div>
</body>
</html>
示例
五一排版
<html>
<head>
<meta charset="UTF-8">
<title>五一</title>
<style>
@font-face {
font-family:"微软雅黑";
}
.banner{
width: 700px;
height: 200px;
background-color:#ff6b6b;
color: white;
text-align: center;
line-height:200px;
font-family: "微软雅黑";
font-size: 50px;
position: relative;
margin: 50px auto;
}
.id{
position: absolute;
right: 20px;
bottom: 10px;
font-size: 16px;
line-height: 1;
}
</style>
</head>
<body>
<div class="banner">
五一劳动节快乐
<div class="id">学号:39 姓名:zhang</div>
</div>
</body>
</html>
效果
`
浙公网安备 33010602011771号