绝对定位
<!DOCTYPE html>
<html>
<head>
<title>三色</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
div {
width: 100px;
height: 100px;
}
#d1 {
position: absolute;
background-color: red;
}
#d2 {
position: absolute;
left: 40px;
top: 40px;
background-color: yellow;
}
#d3 {
position: absolute;
left: 70px;
top: 70px;
background-color: green;
}
</style>
</head>
<body>
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>
</body>
</html>
代码显示:

相对定位
<!DOCTYPE html>
<html>
<head>
<title>三色</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
div {
width: 100px;
height: 100px;
}
#d1 {
background-color: red;
}
#d2 {
position: relative;
left: 40px;
top: 40px;
background-color: yellow;
}
#d3 {
position: relative;
left: 40px;
top: 40px;
background-color: green;
}
</style>
</head>
<body>
<div id="d1">
<div id="d2">
<div id="d3"></div>
</div>
</div>
</body>
</html>
代码显示:

绝对定位与盒子模型
<!DOCTYPE html>
<html>
<head>
<title>三色</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
div {
width: 100px;
height: 100px;
}
#d1 {
background-color: red;
}
#d2 {
position: relative;
left: 40px;
top: 40px;
background-color: yellow;
}
#d3 {
position: relative;
left: 40px;
top: 40px;
background-color: green;
}
</style>
</head>
<body>
<div id="d1">
<div id="d2">
<div id="d3"></div>
</div>
</div>
</body>
</html>
代码显示:

进阶
<!DOCTYPE html>
<html>
<head>
<title>三色</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
.div1 {
border: black 5px solid;
width: 300px;
height: 300px;
}
.div2 {
border: black 5px solid;
width: 200px;
height: 200px;
}
#d1 {
background-color: red;
float: left;
position: absolute;
left: 0px;
top: 0px;
}
#d2 {
background-color: yellow;
position: absolute;
left: 100px;
top: 100px;
}
#d3 {
background-color: green;
position: absolute;
left: 200px;
top: 200px;
}
#d4 {
background-color: yellow;
position: absolute;
left: 100px;
top: 100px;
}
#d5 {
background-color: green;
position: absolute;
left: 200px;
top: 200px;
}
#d6 {
border: black 5px solid;
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<div class="div1" id="d1"></div>
<div class="div1" id="d2"></div>
<div class="div1" id="d3"></div>
<div class="div2" id="d4"></div>
<div class="div2" id="d5"></div>
<div id="d6"></div>
</body>
</html>
代码显示:
