【发布时间】: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。
上查看示例【问题讨论】:
-
只是因为没有规则这么说..最大宽度与视口单位有关
-
我会改用
word-break:break-word;- 这应该会更好 -
@TemaniAfif 但为什么第一条消息会调整到其父级的宽度?他们有相同的类名
-
@Pete 谢谢。但是 Firefox 不支持
word-break:break-word;:'( -
当然是。
标签: html css overflow word-wrap word-break