【发布时间】:2022-01-07 20:51:21
【问题描述】:
[![红色:在画布上居中:黑色:在画布中心和顶部之间居中][1]][1]
使用flex,很容易将元素定位在 div 的中心。但是如何相对于已经居中的元素使用 flex 来完成呢?
下面显示的代码是我迄今为止提出的最好的。我可以将红色方块居中居中,但无法让其上方的蓝色方块在红色方块和顶部边框之间垂直居中对齐。
.flex {
border-style: solid;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: white;
height: 100vh
}
.square {
height: 10px;
width: 10px;
}
#square1 {
background-color: blue;
}
#square2 {
background-color: red;
}
.flexdivider {
display: flex;
flex-direction: column
justify-content: center;
align-items: center;
flex: 1;
}
p {
color: white;
}
<body>
<div class="flex">
<div class="flexdivider">
<div class="square" id="square1"></div>
</div>
<div class="flexdivider">
<div class="square" id="square2"></div>
</div>
<div class="flexdivider">
</div>
</div>
</div>
</body>
【问题讨论】:
-
请贴出您到目前为止尝试的代码
-
刚刚发布。我想弄清楚如何将蓝色方块居中在红色方块和环绕 div 边框之间。