【问题标题】:Word-wrap doesn't respect parent's width for long non-break text对于长的不间断文本,自动换行不尊重父母的宽度
【发布时间】:2018-06-15 08:39:17
【问题描述】:

.container {
  /* 
     Container's width can be dynamically resized. 
     I put width = 300px in the example to demonstrate a case 
     where container's width couldn't hold second msg 
  */
  width: 300px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 1em;
  background-color: blue;
}

.parent{
  display:flex;
  flex-direction:row;
  flex-wrap:nowrap;
  padding:1em;
  background-color:red;
  box-sizing:border-box;
  margin-bottom: 1em;
}
.name{
  background-color:mistyrose;
  width: 70px;
  padding: 1em;
}
.msg{
  background-color:powderblue;
  max-width: 30vw;
  padding:.5em;
  word-wrap:break-word;
}
<div class='container'>
 <div class="parent">
    <div class="name">
      David
    </div>
    <div class="msg">
    How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? 

    </div>
  </div>
  <div class="parent">
    <div class="name">
      Hannah
    </div>
    <div class="msg">
    somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething

    </div>
  </div>
</div>

理想情况下,我希望每个msg 的最大宽度为30vw,但同时尊重父母的宽度。 第一行行为正确,但第二行不正确。如果父母的宽度调整为小于 30vw 的某个值,second msg 将溢出。

我想要max-width = min(30vw, parent's width)之类的东西

注意:容器的宽度可以动态调整。我在示例中放置了 width = 300px 来演示容器无法容纳第二个 msg 的情况,该 msg 以某种方式具有 width = 它的 max-width = 30vw。

您也可以在 http://jsfiddle.net/vbj10x4k/231/

上查看示例

【问题讨论】:

  • 只是因为没有规则这么说..最大宽度与视口单位有关
  • 我会改用word-break:break-word; - 这应该会更好
  • @TemaniAfif 但为什么第一条消息会调整到其父级的宽度?他们有相同的类名
  • @Pete 谢谢。但是 Firefox 不支持 word-break:break-word; :'(
  • 当然是。

标签: html css overflow word-wrap word-break


【解决方案1】:

只需将max-width:100% 设置为.parent,这样就可以尊重.container 的宽度,然后依赖flex,默认情况下你的元素会缩小。也不要忘记元素本身的min-width:0 to enable the element to shrink

.container {
  width: 300px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 1em;
  background-color: blue;
}

.parent{
  display:flex;
  flex-direction:row;
  flex-wrap:nowrap;
  padding:1em;
  background-color:red;
  box-sizing:border-box;
  margin-bottom: 1em;
  max-width:100%; /*Added this */
}
.name{
  background-color:mistyrose;
  width: 70px;
  padding: 1em;
}
.msg{
  background-color:powderblue;
  max-width:30vw;
  min-width:0; /*addedd this*/
  padding:.5em;
  word-wrap:break-word;
}
<div class='container'>
 <div class="parent">
    <div class="name">
      David
    </div>
    <div class="msg">
    How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? How are you? 

    </div>
  </div>
  <div class="parent">
    <div class="name">
      Hannah
    </div>
    <div class="msg">
    somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething

    </div>
  </div>
</div>

【讨论】:

  • 我更新了帖子以添加注释。我的意思是容器的宽度可以动态调整大小。所以理想情况下我不想为孩子的最大宽度设置固定值:D
  • @PhucNguyen 但至少你明白为什么这个问题吗? ...删除最大宽度,您将看到第一个和第二个的行为方式
  • 不,我不明白。两个msgs 都有相同的类。第一个味精也有max-width = 30vw,但它仍然适合容器,而第二个则不适合
  • @PhucNguyen 但删除最大宽度,看看发生了什么......你会看到第一个已经很合适,第二个溢出太多了......所以 max-width 简单减少了第二次溢出
  • 完美解决方案!在所有浏览器上工作。如果我早点问的话,我应该可以节省 1 天的时间。
【解决方案2】:

如果您不需要支持 IE11 或 safari,一个简单的解决方案是将您的 word-wrap:break-word; 切换为 word-wrap: anywhere (working example)

如果您需要支持 safari(但仍不支持 IE11),请将 word-wrap:break-word; 替换为 word-break: break-word。请注意,word-break: break-worddeprecated,而不是 word-wrap: anywhere

我还建议将word-wrap 切换为更常用的别名:overflow-wrap。在搜索词中使用会更有效。

另见:Difference between overflow-wrap and word-break?

【讨论】:

    【解决方案3】:

    只需添加:

    word-break: break-all;
    

    在 .msg

    【讨论】:

    • 这会在任何地方中断。我想保留整个单词。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多