Both of them mean space, but is there any difference?
One is non-breaking space and the other is a regular space. A non-breaking space means that the line should not be wrapped at that point.
Furthermore as Svend points out in his comment, non-breaking spaces are not collapsed.
The first is not treated as white space by the HTML parser, the second is. As a result the " " is not guaranteed to showup within certain HTML markup, while the non breakable space will always show up.
@Zoidberg is right, example:
<h1>title</h1> <h2>date</h2>
will not display space between header markup, with
& nbsp ;
will do space :)
should be handled as a whitespace.
should be handled as two whitespaces
' ' can be handled as a non interesting whitespace
' ' + ' ' can be handled as a single ' '
In addition to the other answers here, non-breaking spaces will not be "collapsed" like regular spaces will. For example, both
<p>Word1 Word2</p>
and
<p>Word1 Word2</p>
will render the same on any browser, while
<p>Word1& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;Word2</p>
will keep the spaces when rendered. (Please ignore the spaces in the previous line; Markdown was formatting the & nbsp;'s and not displaying them as written.
Multiple normal white space characters (space, tabulator and line break) are treated as one single white space character:
For all HTML elements except
PRE
, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.
So
foo bar
is displayed as
foo bar
But no-break space is always displayed. So
foo   bar
is displayed as
foo bar
As already mentioned, you will not receive a line break where there is a "no-break space".
Also be wary, that elements containing only a " " may show up incorrectly, where will work. In i.e. 6 at least (as far as I remember, IE7 has the same issue), if you have an empty table element, it will not apply styling, for example borders, to the element, if there is no content, or only white space. So the following will not be rendered with borders:
<td></td>
<td> <td>
Whereas the borders will show up in this example:
<td>& nbsp;</td>
Hmm -had to put in a dummy space to get it to render correctly here
The entity
produces a non-breaking space, which is used when you don't want an automatic line break at that position. The regular space has the character code 32, while the non-breaking space has the character code 160.
For example when you display numbers with space as thousands separator: 1 234 567, then you use non-breaking spaces so that the number can't be split on separate lines. If you display currency and there is a space between the amount and the currency: 42 SEK, then you use a non-breaking space so that you don't get the amount on one line and the currency on the next.
When having line-breaks, the line will not break when you use an $bnsp; because it's a 'non-breaking space'. This can be important if you have certain product-names or such, that always shall be written together.
Can be interesting if you (have to) use a whitespace as delimiter in numbers, like 12344567, that is displayed 12 344 567 in France. Without the the line would break in the middle of the number, very ugly. Test:12 344 567
There are a lot of different types of spaces, such as thin space, em space, non breaking space... Jon Tan has made a writeup of the most common ones at his blog.