7

.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,但同时尊重父母的宽度。第一行行为正确,但第二没有。如果 parent 的宽度调整为小于 30vw 的某个值,则第二个msg 将溢出。

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

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

您还可以在 http://jsfiddle.net/vbj10x4k/231/上查看示例

4

3 回答 3

4

简单地设置max-width:100%.parent这样一个尊重.container然后依赖 flex 的宽度,默认情况下你的元素会缩小。也不要忘记min-width:0元素本身以使元素缩小

.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>

于 2018-06-15T09:20:17.933 回答
1

如果您不需要支持 IE11 或 safari,一个简单的解决方案是切换您word-wrap:break-word;word-wrap: anywhere工作示例

如果您需要支持 safari(但仍不支持 IE11),请改为word-wrap:break-word;使用word-break: break-word. 请注意word-break: break-word不赞成使用word-wrap: anywhere.

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

另请参阅:溢出换行和分词之间的区别?

于 2019-02-13T13:39:07.617 回答
0

只需添加:

word-break: break-all;

在.msg

于 2018-06-15T09:14:13.197 回答