【发布时间】:2018-11-08 13:54:54
【问题描述】:
很短的史前史
我的故事始于努力让overflow-wrap: break-word; 在弹性盒中工作。 Flexbox 容器不想理解它的 item 可以缩小,尽管 item 可能会打断长词:
.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 了解它可以在 item 上使用 min-width: 0; 来缩小其 item:
.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>
然而,现实世界要复杂一些。
问题
在我们的应用程序中,我们有许多嵌套的弹性盒。所以这个例子应该是这样的:
.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-column 的 flex 容器忽略了它的子元素可以很好地收缩的事实。我不明白为什么它会这样。你能给我解释一下吗?为什么 flexbox-container 不尊重它的子 flexbox min-width: 0?
我找到的解决方案是将min-width: 0 设置为层次结构中的所有 flexbox,这看起来非常笨拙和危险,因为我可以在意想不到的地方破坏我们的应用程序布局。
【问题讨论】:
标签: html css flexbox word-wrap