如何在body标签内垂直居中父div ?



我在body中有一个父div,我希望它与所有其他子div垂直对齐。如果我在任何一点上错了,请告诉我。

<style>
.container{display:flex; justify-content:center; align-items:center;}
.parent div{height:100px; width:100px; background-color:yellow; margin:10px auto;}
</style>
<body class="container">
<div class"parent">
<div></div>
<div></div>
<div></div>
</div>
</body>

你把=忘在class="parent"里了

<style>
.container{display:flex; justify-content:center; align-items:center;}
.parent div{height:100px; width:100px; background-color:yellow; margin:10px auto;}
</style>
<body class="container">
<div class="parent">
<div></div>
<div></div>
<div></div>
</div>
</body>

为body(容器)添加宽度和高度,并为class="parent";

<style>
.container{display:flex; justify-content:center; align-items:center; width:100%; height: 100vh;}
.parent div{height:100px; width:100px; background-color:yellow; margin:10px auto;}
</style>
<body class="container">
<div class="parent">
<div></div>
<div></div>
<div></div>
</div>
</body>

最新更新