gpt4 book ai didi

html - 父 flexbox 容器忽略子 flexbox 的最小宽度

转载 作者:可可西里 更新时间:2023-11-01 13:19:49 30 4
gpt4 key购买 nike

很短的史前史

我的故事始于努力让 overflow-wrap: break-word; 在 flexbox 中工作。 Flexbox 容器不想理解它的元素可以收缩,尽管元素可以打断长词:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex-column {
display: flex;
}

.item {
overflow-wrap: break-word;
background-color: #fff;
padding: 8px;
}
<div class="body">
<div class="flex-column">
<div class="item">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>

幸运的是,我们可以帮助 flexbox 理解它可以在元素上使用 min-width: 0; 来缩小它的元素:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex-column {
display: flex;
}

.item {
overflow-wrap: break-word;
background-color: #fff;
padding: 8px;
/* Okay, it fixes this */
min-width: 0;
}
<div class="body">
<div class="flex-column">
<div class="item">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>

然而,现实世界有点复杂。

问题

在我们的应用程序中,我们有许多嵌套的 flexbox 。所以这个例子应该是这样的:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
min-width: 0;
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex">
<div class="flex">
<div class="flex">
<div class="flex-column">
<div class="item">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>
</div>

如您所见,我们的 flex-columnflex 容器忽略了一个事实,即它的子项可以很好地收缩。我不明白为什么它会这样。你能给我解释一下吗?为什么 flexbox-container 不尊重它的子 flexbox min-width: 0

我找到的解决方案是将 min-width: 0 设置为层次结构中的 all flexboxes,这看起来非常 hacky 和危险,因为我可以破坏我们的应用程序在意想不到的地方布局。

最佳答案

要理解这一点,只需为您的元素添加不同颜色的边框,您就会看到不同层次的溢出。更准确地说,我们只有一个溢出,在添加每个 min-width 后移动到较低的杠杆。

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex" style="border:5px solid red;">
<div class="flex" style="border:5px solid green;">
<div class="flex" style="border:5px solid blue;">
<div class="flex-column" style="border:5px solid yellow;">
<div class="item" style="border:5px solid pink;">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get
your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>

每个 min-width 都会修复一个溢出,允许元素收缩并将溢出移动到下一层。这就是为什么您需要级联 min-width

添加一个:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex" style="border:5px solid red;">
<!-- adding min-width at this level -->
<div class="flex" style="border:5px solid green;min-width:0;">
<div class="flex" style="border:5px solid blue;">
<div class="flex-column" style="border:5px solid yellow;">
<div class="item" style="border:5px solid pink;">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>
</div>

添加另一个:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex" style="border:5px solid red;">
<div class="flex" style="border:5px solid green;min-width:0;">
<!-- adding min-width at this level -->
<div class="flex" style="border:5px solid blue;min-width:0">
<div class="flex-column" style="border:5px solid yellow;">
<div class="item" style="border:5px solid pink;">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get
your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>

再次:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex" style="border:5px solid red;">
<div class="flex" style="border:5px solid green;min-width:0;">
<div class="flex" style="border:5px solid blue;min-width:0">
<!-- adding min-width at this level -->
<div class="flex-column" style="border:5px solid yellow;min-width:0;">
<div class="item" style="border:5px solid pink;">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get
your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>

最后一个:

.body {
width: 300px;
border: 1px solid black;
padding: 8px;
background-color: #ccc;
}

.flex {
display: flex;
}

.flex-column {
display: flex;
}

.item {
padding: 8px;
background-color: #fff;
overflow-wrap: break-word;
}
<div class="body">
<div class="flex" style="border:5px solid red;">
<div class="flex" style="border:5px solid green;min-width:0;">
<div class="flex" style="border:5px solid blue;min-width:0">
<div class="flex-column" style="border:5px solid yellow;min-width:0;">
<!-- adding min-width at this level -->
<div class="item" style="border:5px solid pink;min-width:0">
This is Spaaaaaaaaaaaaaaaarrrrrrrrrrrrrttttttttttttttaaaaaaaaaaaaaaaaaaaaaa!11 It's not a bug. Firefox is correctly implementing min-width: auto for flex items. When you change it to min-width: 0, you're just using a different value for min-width to get
your example looking how you want it to look. But both values are being rendered correctly.
</div>
</div>
</div>
</div>
</div>
</div>

关于html - 父 flexbox 容器忽略子 flexbox 的最小宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53209171/

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