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

HtmlCss

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter NixeyView Question on Stackoverflow
Solution 1 - HtmlGusView Answer on Stackoverflow
Solution 2 - HtmlsubstateView Answer on Stackoverflow