Break long words inside flex item

笑着哭i 提交于 2020-01-04 13:44:15

问题


I would like to break long words inside "div2", both div2 and div3 width cannot be greater than parent width (i.e 150px). The only thing that works is word-break: break-all but this will break short words as well.

#div1{
    display: flex;
    max-width: 150px;
    height: 100px;;
}
#div2{
    background-color: gray;
}
#div3{
    background-color: rgb(197, 181, 181);
}
#div4{
    width: 150px; 
    background-color: rgb(144, 199, 172);
}
<div id="div1">
        <div id="div2">aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb</div>
        <div id="div3">oooo</div>
</div>

<div id="div4">150 px - max width</div>

回答1:


You can use break-word to break only in words

#div1 {
  display: flex;
  max-width: 150px;
  height: 100px;
  ;
}

#div2 {
  background-color: gray;
  word-break: break-word;
}

#div3 {
  background-color: rgb(197, 181, 181);
}

#div4 {
  width: 150px;
  background-color: rgb(144, 199, 172);
}
<div id="div1">
  <div id="div2">aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb</div>
  <div id="div3">oooo</div>
</div>

<div id="div4">150 px - max width</div>

And break-all to break at any given point

#div1 {
  display: flex;
  max-width: 150px;
  height: 100px;
  ;
}

#div2 {
  background-color: gray;
  word-break: break-all;
}

#div3 {
  background-color: rgb(197, 181, 181);
}

#div4 {
  width: 150px;
  background-color: rgb(144, 199, 172);
}
<div id="div1">
  <div id="div2">aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb</div>
  <div id="div3">oooo</div>
</div>

<div id="div4">150 px - max width</div>

More info available on MDN.

Hope this helps :)



来源:https://stackoverflow.com/questions/49943630/break-long-words-inside-flex-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!