In HTML, is it possible to insert a word wrapping hint?

不想你离开。 提交于 2019-12-03 22:18:17

Use &shy; in words or <wbr> between words, as <wbr> won't introduce a hyphen.

See also:

Not quite exactly, but close: http://jsfiddle.net/uW4h8/1/.

In brief, you should set white-space: nowrap; for you text container and use <wbr> to insert breaks between words as desired.

The elements <wbr> and &nbsp; often work, but not always. They are especially problematic when designing a static landing page that (a) has to work on a variety of screens and browsers and (b) has to look good.

In this case I want control over line-break hints at a variety of different screen resolutions. To do so, I use <br> tags and css. It can become a mess if it gets complex, but I found it works up to a point. For example:

<p class='break-hints'>
Hello there, dear customer. <br class='break-big'>
Please have a look <br class='break-small'> at our offer.
</p>

I then use CSS with media queries to indicate when the various breaks should be triggered.

p.break-hints br {
  display: none;
}

@media only screen and (max-width: 300px) { 
  p.break-hints br.break-small {
    display: inline;
  }
}

p.break-hints br.break-big {
  display: inline;
}

Use a no-break space U+00A0 (or &nbsp; if you have no convenient way of entering the character as such) between words when a line break is not to be allowed, and normal space otherwise.

This won’t work when words contains hyphens “-”, and some other characters, such as parentheses, may cause problems as well, because some browsers treat them as allowing a line break after them. See http://www.cs.tut.fi/~jkorpela/html/nobr.html for a lengthy treatise, including various techniques of coping with the problems. But if you have normal words with normal punctuation only and no hyphens, you should be fine with the simple approach.

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