【问题标题】:Avoid breaking line between two words when resizing [duplicate]调整大小时避免在两个单词之间换行[重复]
【发布时间】:2017-01-29 10:25:01
【问题描述】:

我有一个字符串,我需要它不要在不同的行中破坏两个特定的单词。示例:

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

当我调整窗口大小并使其更小时,它会输出一个时刻:

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

我希望T-2 + Rate 永远在一起。怎么做?

【问题讨论】:

  • 您能发布您的代码并告诉我们您已经尝试过什么吗?
  • 请发布您的代码?
  • 你为什么不在句子段落中使用特定的宽度,在较小的分辨率中。这样它就会保留特定的单词,然后它就会中断。

标签: html css string text line-breaks


【解决方案1】:

只需将<span style="white-space: nowrap"> 用于非破坏性部件,如MDN 所述。

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

【讨论】:

    【解决方案2】:

    您使用不间断空格。它的 HTML 实体是  。您可能还需要在 T-2 中使用不间断的连字符 (‑):

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

    例子:

    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;
    }
    &lt;div id="target"&gt;Ask for it it when contracting until 2016/09/30 with T&amp;#8209;2&amp;nbsp;Rate&lt;/div&gt;

    【讨论】:

    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多