gpt4 book ai didi

html - 为什么 flex item 在没有 flex-grow 的情况下扩展全高?

转载 作者:行者123 更新时间:2023-11-28 15:54:32 25 4
gpt4 key购买 nike

我有以下代码:

.ai-container,
.ai-overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
}
.ai-overlay {
background-color: black;
opacity: .2;
z-index: 1000;
}
.ai-dialog {
z-index: 1001;
display: flex;
flex-direction: column;
border: 1px solid darkgray;
border-radius: 5px;
background-color: white;
min-width: 300px;
box-shadow: 0px 0px 15px #444;
overflow: hidden;
}
.ai-header,
.ai-footer,
.ai-body {
padding: 10px;
}
.ai-header {
background-color: hsl(0, 0%, 20%);
color: white;
display: flex;
font-size: 1.1em;
justify-content: space-between;
}
.ai-footer {
display: flex;
background-color: lightgray;
justify-content: space-between;
}
<div class="ai-container">
<div class="ai-overlay"></div>
<div class="ai-dialog">
<div class="ai-header">
<span>This is the header</span>
<span>X</span>
</div>
<div class="ai-body">This is the body</div>
<div class="ai-footer">
<span>
<button>Submit</button>
<button>Lookup</button>
</span>
<button>Cancel</button>
</div>
</div>
</div>

您可以在此处查看结果:http://plnkr.co/edit/5SOxkMV9qQ86m6d2jyT0?p=preview

然而,生成的“对话框”被拉伸(stretch)以填充整个垂直空间。

如果我有 flex-grow: 1,我会期待这个,但我没有。

如果您将以下内容添加到 css:

.ai-container, .ai-overlay {
... /*all the previous settings */
align-items: center;
}

然后对话框看起来像我期望的那样......刚好足以适合内容。我假设删除 align-items 只会将对话框移动到浏览器窗口的顶部而不会影响高度。

我在这里错过了什么?

最佳答案

I assumed that removing align-items would simply move the dialog to the top of the browser window without affecting the height.

你很接近。您假定 align-items 的默认值为 flex-start。实际上,默认值是 stretch

这意味着 flex 元素将始终扩展以覆盖容器横轴的整个长度,除非初始设置被覆盖。

您可以使用 align-items: flex-startcenter 等覆盖默认值

以下是更多详细信息: How to disable equal height columns in Flexbox?

关于html - 为什么 flex item 在没有 flex-grow 的情况下扩展全高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368783/

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