How to prevent line breaks in list items using CSS

HtmlCssWord Wrap

Html Problem Overview


I'm trying to put a link called Submit resume in a menu using a li tag. Because of the whitespace between the two words it wraps to two lines. How to prevent this wrapping with CSS?

Html Solutions


Solution 1 - Html

Use white-space: nowrap;[1] [2] or give that link more space by setting li's width to greater values.


[1] § 3. White Space and Wrapping: the white-space property - W3 CSS Text Module Level 3
[2] white-space - CSS: Cascading Style Sheets | MDN

Solution 2 - Html

You could add this little snippet of code to add a nice "…" to the ending of the line if the content is to large to fit on one line:

li {
  overflow: hidden; 
  text-overflow: ellipsis;
  white-space: nowrap;
}

Solution 3 - Html

If you want to achieve this selectively (ie: only to that particular link), you can use a non-breaking space instead of a normal space:

<li>submit&nbsp;resume</li>

https://en.wikipedia.org/wiki/Non-breaking_space#Encodings

edit: I understand that this is HTML, not CSS as requested by the OP, but some may find it helpful…

Solution 4 - Html

display: inline-block; will prevent break between the words in a list item:

li {
  display: inline-block;
}

Solution 5 - Html

Bootstrap 4 has a class named text-nowrap. It is just what you need.

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
QuestionRajasekarView Question on Stackoverflow
Solution 1 - Htmln1313View Answer on Stackoverflow
Solution 2 - HtmlJimmyRareView Answer on Stackoverflow
Solution 3 - HtmlptimView Answer on Stackoverflow
Solution 4 - HtmlNadeesh PeirisView Answer on Stackoverflow
Solution 5 - HtmlYevgeniy AfanasyevView Answer on Stackoverflow