保持一行文字作为一个单一的行-包装整个行或根本没有

我想把一行文本放在一起,这样整行文本要么下降一行,要么一行都不下降

可以接受

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

可以接受

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

不能接受

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

Is this achievable in CSS?

156843 次浏览

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

// 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 ;
}

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