When to use &nbsp

I have seen &nbsp in html and can't quite tell what it does other than create some whitespace. I am wondering what exactly it does and when it should be used?


Solution 1:

  (it should have a semi-colon on the end) is an entity for a non-breaking space.

Use it between two words that should not have a line break inserted between them by word wrapping.

There is a good explanation about when this is appropriate grammar on the English StackExchange.


It is sometimes abused to create horizontal space between content in web pages (since it will not collapse like multiple regular spaces). Padding and margins should usually be used instead of this hack.

Solution 2:

One reason for   is to insert multiple spaces in a document.

In HTML, multiple whitespace characters are collapsed into one space. This includes tabs and newlines.

IF you wanted to display the following:

three   spaces.

You could insert 3   entities instead of using spaces like so:

three   spaces.

Edit: It's worth mentioning that   is more of a historical artifact than anything else. Just about every use for it that is mentioned in the answers to this question has a better alternative means to accomplish that goal. However,   is still with us, and these are some of the things people have used it for.

See also: http://www.sightspecific.com/~mosh/www_faq/nbsp.html