gpt4 book ai didi

html - 显示彼此居中并顶部对齐的前 2 个 flex 元素

转载 作者:行者123 更新时间:2023-11-28 14:20:18 26 4
gpt4 key购买 nike

我正在尝试将前 2 个 block 彼此居中对齐(带有紫色边框的 block )并使所有 3 个框顶部对齐。我已经使用 display: inline-flex 实现了这一点,但是是否可以仅使用 display: flex 获得相同的结果?

Fiddle

.container {
font-size: 0;
box-sizing: border-box;
}

.blocks {
display: inline-flex;
}

.blocks-left {
align-items: center;
width: 66%;
vertical-align: top;
border: 2px solid purple;
}

.blocks-right {
width: 33%;
}

.block {
width: 100%;
}

#block-1 {
background: red;
height: 100px;
}

#block-2 {
background: blue;
height: 200px;
}

#block-3 {
background: green;
height: 400px;
}
<div class="container">
<div class="blocks blocks-left">
<div id="block-1" class="block"></div>
<div id="block-2" class="block"></div>
</div>
<div class="blocks blocks-right">
<div id="block-3" class="block"></div>
</div>
</div>

最佳答案

如果你想改变blocksblock-level display: flex,你可以得到相同的结果,如果你做你的container flexbox 并将 align-self: flex-start 添加到 blocks-left 元素 - 请参见下面的演示:

.container {
font-size: 0;
box-sizing: border-box;
display: flex; /* added */
}

.blocks {
display: flex; /* now flex instead of inline-flex */
}

.blocks-left {
align-items: center;
align-self: flex-start; /* added */
width: 66%;
vertical-align: top;
border: 2px solid purple;
}

.blocks-right {
width: 33%;
}

.block {
width: 100%;
}

#block-1 {
background: red;
height: 100px;
}

#block-2 {
background: blue;
height: 200px;
}

#block-3 {
background: green;
height: 400px;
}
<div class="container">
<div class="blocks blocks-left">
<div id="block-1" class="block"></div>
<div id="block-2" class="block"></div>
</div>
<div class="blocks blocks-right">
<div id="block-3" class="block"></div>
</div>
</div>

关于html - 显示彼此居中并顶部对齐的前 2 个 flex 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55286095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com