566

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?

lorem monkey
  • 3,852
  • 3
  • 33
  • 48
Rajasekar
  • 17,868
  • 32
  • 99
  • 135

5 Answers5

1046

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

Wolf
  • 9,246
  • 7
  • 59
  • 101
n1313
  • 19,727
  • 7
  • 28
  • 45
  • 4
    I prevented line break in li items using `display: inline;`. Maybe this will also help others with similar problems. –  Oct 03 '13 at 13:24
  • Its important to be careful with display: inline as it can have side effects. white-space: nowrap; has only the one effect. – Crispen Smith Aug 10 '15 at 03:51
  • In this case text overflow happens. how to prevent that? –  Mar 28 '16 at 14:08
171

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;
}
Vadim Ovchinnikov
  • 12,039
  • 5
  • 54
  • 85
JimmyRare
  • 3,828
  • 1
  • 19
  • 23
37

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…

André Werlang
  • 5,723
  • 32
  • 48
ptim
  • 13,723
  • 9
  • 79
  • 94
17

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

li {
  display: inline-block;
}
Alexander van Oostenrijk
  • 4,385
  • 3
  • 22
  • 37
Nadeesh Peiris
  • 319
  • 2
  • 9
  • 1
    If I click through to the jsfiddle and adjust the width of the output, the individual list items break between the word "list" and the number, which is exactly what the OP *doesn't* want... – GentlePurpleRain Jan 24 '18 at 20:39
4

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

Yevgeniy Afanasyev
  • 32,743
  • 20
  • 155
  • 172