<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.fixed, .demo{
width: 100px;
height: 100px;
}
.fixed {
margin: 0 auto;
margin-top: 100px;
position: relative;
background: red;
border: 5px solid #000;
}
.demo {
position: absolute;
background: blue;
left: 0;
top: 0;
border: 5px solid gray;
}
</style>
</head>
<body>
<div class="fixed">
<div class="demo"></div>
</div>
</body>
</html>
如上图所示,红底黑边的divA作为相对定位,蓝底灰边的divB作为绝对定位。
且设置divB的 top、left为 0,可以看到,divB以border的左上角作为对齐点,对divA的content区域左上角点对齐。