gpt4 book ai didi

html - 为什么align-self是: stretch not working on a flex item?

转载 作者:太空狗 更新时间:2023-10-29 14:42:31 25 4
gpt4 key购买 nike

我正在尝试拉伸(stretch) .side-bar div,使其占据整个窗口的高度。

我将 flex 添加到 flex 容器并指定了 flex 元素的宽度和高度,但侧边栏的高度显示为与 flex 元素相同。

是因为我的 CSS 类的顺序吗?

* {
margin: 0;
}

.flex-container {
display: flex;
height: 500px;
border: 1px solid blue;
}

.flex-items {
width: 100px;
height: 250px;
border: 1px solid red;
}

.side-bar {
width: 400px;
align-self: stretch;
}
<div class="flex-container">
<div class="flex-items side-bar"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
</div>

最佳答案

您已经在 flex 元素上指定了高度。

.flex-items {
width: 100px;
height: 250px;
border: 1px solid red;
}

此高度规则覆盖 align-itemsalign-self ( here's why ) 上的 stretch

此外,将 align-self: stretch 添加到 .side-bar 无论如何也不会做任何事情,因为 stretch 已经应用了——这是默认设置值(value)。

试试这个:

* {
margin: 0;
}

.flex-container {
display: flex;
height: 500px;
border: 1px solid blue;
}

.flex-items:not(.side-bar) { /* modified selector */
width: 100px;
height: 250px;
border: 1px solid red;
}

.side-bar {
width: 400px;
background-color: yellow;
border: 1px solid red;
}
<div class="flex-container">
<div class="flex-items side-bar"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
</div>

关于html - 为什么align-self是: stretch not working on a flex item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34365528/

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