CSS: Wrap text AND break word if necessary

本秂侑毒 提交于 2019-12-07 10:54:13

问题


How can I set up a div such that it will wrap text AND will also break word if necessary? Examples below, with the dots showing the width of the div ("zzzzzzz..." is all one word).

....................................
Hello these are
words

....................................
zzzzzzzzzzzzzzzzzz
zzzzzz


回答1:


You can use css word-wrap: break-word;

.wrap {
  width: 50px;
  word-wrap: break-word;
  border: 1px solid red
}
<div class="wrap">This is a mormal div wrapping and breaking words</div>



回答2:


and if you want a word to be separated at a certain position when it's too long for the container, you can use the &shy; entity (soft hyphen). So the HTML

singledoubletriple&shy;quadruplequintupleburger 

will remain

singledoubletriplequadruplequintupleburger

on the screen when it fits in one line, but become

singledoubletriple-
quadruplequintupleburger

if the line is shorter



来源:https://stackoverflow.com/questions/36160508/css-wrap-text-and-break-word-if-necessary

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