【发布时间】:2020-06-20 16:50:22
【问题描述】:
给出了一个简单的 flexbox 布局,在任意文本旁边显示一个图像,这是期望的结果:
#container {
align-items: center;
background: black;
display: flex;
justify-content: center;
padding: 10px;
width: 500px; /* Dynamic */
}
#image {
background: white;
height: 50px; /* Dynamic */
margin-right: 10px;
width: 50px; /* Dynamic */
}
span {
color: white;
font-size: 40px;
/* Make sure text also breaks when no hints are available */
overflow-wrap: anywhere;
}
<div id="container">
<div id="image"></div>
<span>long_text_should_be_<br />allowed_to_break</span>
</div>
但是,对于我的用例,我必须使用 <wbr> 而不是 <br>,这会破坏居中行为。
#container {
align-items: center;
background: black;
display: flex;
justify-content: center;
padding: 10px;
width: 500px;
}
#image {
background: white;
height: 50px;
margin-right: 10px;
width: 50px;
}
span {
color: white;
font-size: 40px;
overflow-wrap: anywhere;
}
<div id="container">
<div id="image"></div>
<span>long_text_should_be_<wbr />allowed_to_break</span>
</div>
我知道后者是预期的行为,因为 flexbox 子项被阻止并且块中的换行符不会缩小到实际的文本宽度。但由于它与<br> 无缝协作,我想知道是否可以通过<wbr> 或其他换行提示对其进行调整以达到相同的结果。
目标:
- 必须在动态大小的环境中工作,容器和图像大小可能会有所不同
- 在没有给出换行提示时必须回退到
break-all行为 - 必须使用 1 到 n 个字符的任意文本长度
- 只需要在带有 LTR 拉丁文本的最新版 Chrome 中工作
【问题讨论】: