640

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?

6 Answers 6

1158

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

Sign up to request clarification or add additional context in comments.

3 Comments

I prevented line break in li items using display: inline;. Maybe this will also help others with similar problems.
Its important to be careful with display: inline as it can have side effects. white-space: nowrap; has only the one effect.
In this case text overflow happens. how to prevent that?
198

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

1 Comment

I was looking for a text clamping solution, but I'll stick to this solution
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…

Comments

21

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

li {
  display: inline-block;
}

1 Comment

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...
5

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

3 Comments

I'd prefer to avoid recommend using Bootstrap in 2019.
@BrodaNoel Why?
@YevgeniyAfanasyev It's bloat
3

In your CSS, white-space can do the job

possible values:

white-space: nowrap
white-space: pre
white-space: pre-wrap
white-space: pre-line
white-space: break-spaces
white-space: normal

1 Comment

I think white-space: nowrap should do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.