<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 500px;
height: 500px;
background-color: yellow;
margin: 0 auto;
position: relative;
}
.child {
width: 200px;
height: 200px;
background: red;
position: absolute;
top: 50%;
left: 50%;
/* 设置 自身盒子的一半
不可以设置百分比 百分比是针对父盒子来算
*/
margin-top: -100px;
margin-left: -100px;
}
</style>
</head>
<body>
<div class="box">
<div class="child">
</div>
</div>
</body>
</html>
定位的水平垂直居中
最新推荐文章于 2023-01-30 19:51:45 发布