<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标题</title>
<style type="text/css">
*{
margin: 0;
padding-top:0;
}
.box1{
width:400px;
height:400px;
background-color: pink;
}
.box2{
background-color: red;
width: 200px;
background-color: red;
}
.father{
position: relative;
top:0;
left: 0;
}
.son{
position: absolute;
top:0;
left:0;
right:0;
bottom: 0;
margin:auto;
/*儿子元素必须有确切的高度写出来,如果不写,儿子高度会和父亲一样的,好惨*/
height:200px;
}
</style>
</head>
<body>
<div class="box1 father">
<div class="box2 son">I'am son,want in distance of center</div>
</div>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标题</title>
<style type="text/css">
*{
margin: 0;
padding-top:0;
}
.box1{
width:400px;
height:400px;
background-color: pink;
}
.box2{
background-color: red;
width: 200px;
background-color: red;
}
.father{
position: relative;
top:0;
left: 0;
}
.son{
position: absolute;
top:0;
left:0;
right:0;
bottom: 0;
margin:auto;
/*儿子元素必须有确切的高度写出来,如果不写,儿子高度会和父亲一样的,好惨*/
height:200px;
}
</style>
</head>
<body>
<div class="box1 father">
<div class="box2 son">I'am son,want in distance of center</div>
</div>
</body>
</html>