这是我的代码示例:
.flex-container {
height: 300px;
width: 300px;
display: flex;
align-items: center;
justify-content: center;
background-color: #ffeeee
}
#item1 {
width: 100px;
height: 100px;
background-color: blue;
}
#item2 {
width: 120px;
height: 120px;
background-color: red;
}
<div class="flex-container">
<div id="item1"></div>
<div id="item2"></div>
</div>
https://jsfiddle.net/vL508wax/
我希望蓝色正方形居中,并且红色正方形顶部与蓝色正方形顶部齐平。
我知道可以使用 margin-top
来实现,但我认为这不是一个好的方式。是否可以直接使用 flexbox 来完成此操作?