Html 将一行文本保留为一行 - 将整行换行或根本不换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6667081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:32:00  来源:igfitidea点击:

Keep a line of text as a single line - wrap the whole line or none at all

htmlcss

提问by Peter Nixey

I'd like to keep a line of text together such that either the whole line drops down a line or none at all

我想将一行文本保留在一起,以便整行都下降到一行或根本没有

Acceptable

可接受

How do I wrap this line of text - asked by Peter 2 days ago

Acceptable

可接受

How do I wrap this line of text 
- asked by Peter 2 days ago

Not acceptable

不能接受的

How do I wrap this line of text - asked by Peter 
2 days ago

Is this achievable in CSS?

这可以在 CSS 中实现吗?

回答by Gus

You can use white-space: nowrap;to define this behaviour:

您可以使用white-space: nowrap;来定义此行为:

// HTML:

.nowrap {
  white-space: nowrap ;
}
<p>
      <span class="nowrap">How do I wrap this line of text</span>
      <span class="nowrap">- asked by Peter 2 days ago</span>
    </p>

// CSS:
.nowrap {
  white-space: nowrap ;
}

回答by substate

You could also put non-breaking spaces (&nbsp;) in lieu of the spaces so that they're forced to stay together.

您还可以放置不间断空格 ( ) 代替空格,以便它们被迫保持在一起。

How do I wrap this line of text
-&nbsp;asked&nbsp;by&nbsp;Peter&nbsp;2&nbsp;days&nbsp;ago