最好的就是用弹性盒子,margin值也可以
<!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>
.box1{
width: 200px;
height: 200px;
background: salmon;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.box1>div{
width: 80%;
height: 80px;
background: aqua;
}
.box2{
width: 200px;
height: 200px;
background: salmon;
}
.box2>div{
width: 80%;
height: 80px;
background: aqua;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">
<div>1</div>
<br>
<div>2</div>
</div>
<br>
<div class="box2">
<div>1</div>
<br>
<div>2</div>
</div>
</body>
</html>
变为行内元素