相对定位:relative; 绝对定位:absollute; 固定定位:fixed;
先定位两个块
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.par{
margin: auto;
width: 300px;
height: 300px;
background: salmon;
}
.child{
margin: auto;
width: 150px;
height: 150px;
background: slateblue;
}
</style>
</head>
<body>
<div class="par">
<div class="child"></div>
</div>
</body>
</html>
如果想调整小框或大框的位置,我们可以利用相对定位position:relative和绝对定位pozation: absolute,
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.par{
position: relative;
margin: auto;
width: 300px;
height: 300px;
background: salmon;
}
.child{
position: absolute;
bottom: 0;
left: 0;
margin: auto;
width: 150px;
height: 150px;
background: slateblue;
}
</style>
</head>
<body>
<div class="par">
<div class="child"></div>
</div>
</body>
</html>
我这里给的是小框相对于大框bottom:0; left:0;这里根据你对框位置的需要来写属性有:top bottom left right z-index(z-index是是对框上下位置的调整,也就是z轴的坐标位置)
top是相对于相对位置的距离上边的距离,其他的几个属性类同。
另外,还有一个固定定位的使用pozation:fixd;
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.par{
position: relative;
margin: auto;
width: 300px;
height: 300px;
background: salmon;
}
.child{
position: absolute;
bottom: 0;
left: 0;
margin: auto;
width: 150px;
height: 150px;
background: slateblue;
}
.fix{
position: fixed;
right: 0;
top: 30px;
width: 30px;
height: 500px;
background: seagreen;
}
</style>
</head>
<body>
<div class="par">
<div class="child"></div>
<div class="fix"></div>
</div>
</body>
</html>
固定定位在浏览器中是不随滚动条动的,也就是说它的位置是固定不变的。