Avoid breaking line between two words when resizing [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-05 15:44:33

问题


I have a string and I need it not to break two specific words in different lines. Example:

"Ask for it it when contracting until 2016/09/30 with T-2 Rate"

When I resize window and make it smaller there is a moment that it outputs:

"Ask for it it when contracting until 2016/09/30 with T-2 \n
Rate"

I would like T-2 + Rate to be always together. How to do it?


回答1:


You use a nonbreaking space. The HTML entity for it is  . You'll probably want a non-breaking hyphen (‑) in T-2 as well:

Ask for it it when contracting until 2016/09/30 with T‑2 Rate

Example:

var target = document.getElementById("target");
var originalWidth = target.innerWidth || target.clientWidth;
var width = originalWidth;
tick();
function tick() {
  width = width < 10 ? originalWidth : (width - 10);
  target.style.width = width + "px";
  setTimeout(tick, 400);
}
#target {
  display: inline-block;
  border: 1px solid #ddd;
}
<div id="target">Ask for it it when contracting until 2016/09/30 with T&#8209;2&nbsp;Rate</div>



回答2:


Just use <span style="white-space: nowrap"> for nonbreaking parts, as MDN states.

Ask for it it when contracting until 2016/09/30 with <span style="white-space: nowrap">T-2 Rate</span>


来源:https://stackoverflow.com/questions/39611979/avoid-breaking-line-between-two-words-when-resizing

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