方式一 : felx布局
<!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>
/* 方式一
flex布局
*/
*{
margin: 0;
padding:0;
}
.a{
width: 600px;
height:600px;
background-color: gray;
display:flex;
justify-content:center;
align-items:center;
}
.b{
width: 200px;
height: 200px;
background-color:green;
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
</div>
</body>
</html>
方式二:定位
<!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;
}
.a{
width: 600px;
height:600px;
background-color: gray;
/* 父元素开启相对定位 */
position: relative;
}
.b{
width: 200px;
height: 200px;
background-color:green;
/* 子元素开启绝对定位 */
position:absolute;
/* 基于父元素 移动 */
top: 50%;
left:50%;
/* 基于自身移动 */
transform:translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
</div>
</body>
</html>