<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.parent{
width: 500px;
height: 500px;
background: lightblue;
margin: auto;
position: relative;
}
.child{
width: 100px;
height: 100px;
background: hotpink;
position: absolute;
top: 50%;
left: 0;
margin-top:-50px;/*子盒子的高度的一半*/
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>77.</title>
<style>
*{
margin: 0;
padding: 0;
}
.parent{
width: 800px;
height: 800px;
background: red;
margin: auto;
position: relative;
}
.son{
width: 200px;
height: 200px;
background: yellow;
position: absolute;
left: 50%;
top: 0;
margin-left: -100px;
}
</style>
</head>
<body>
<div class="parent">
<div class="son"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>77.</title>
<style>
*{
margin: 0;
padding: 0;
}
.parent{
width: 800px;
height: 800px;
background: red;
margin: auto;
position: relative;
}
.son{
width: 200px;
height: 200px;
background: yellow;
position: absolute;
left: 50%;
top: 0;
margin-left: -100px;
}
</style>
</head>
<body>
<div class="parent">
<div class="son"></div>
</div>
</body>
</html>
复制代码
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101253.html原文链接:https://javaforall.cn