tags:

views:

1091

answers:

12

Both of them mean space, but is there any difference?

+45  A: 

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.

Brian Rasmussen
  are also non-collapsing, that's probably the most significant aspect of their use (at least, that's how I tend to use them, pad stuff out, quick and easy)
Svend
Good point. I'll add that.
Brian Rasmussen
Protip: Please do not use 15 %nbsp;'s in a row to space something. Drives me insane when I see people doing that. Use CSS padding or margin.
mrinject
It drives me insane when I see Microsoft Word doing that, too…
jleedev
And you are a non-breaking space Guru :)
Amarghosh
@Amarghosh: Just the kind of guru I always wanted to be ... :)
Brian Rasmussen
A: 

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
A: 

@Zoidberg is right, example:

<h1>title</h1> <h2>date</h2>

will not display space between header markup, with

& nbsp ;

will do space :)

Rin
A: 

&nbsp; should be handled as a whitespace.

&nbsp;&nbsp; should be handled as two whitespaces

' ' can be handled as a non interesting whitespace

' ' + ' ' can be handled as a single ' '

Barfieldmv
+9  A: 

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.

Graeme Perrow
Stewart
Graeme Perrow
I see now. BTW it's a known bug:http://meta.stackoverflow.com/questions/3314
Stewart
+2  A: 

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&‍nbsp;&‍nbsp;&‍nbsp;bar

is displayed as

foo   bar
Gumbo
+3  A: 

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 &nbsp; 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

Pete
Stewart
@Stewart - I tried with amp, and it worked in the text, but not in code exmple - because it also wrote the "amp" part out
Pete
Very strange. Obviously a bug.
Stewart
I'm not the only one with that problem, I can see ;)
Pete
+26  A: 

The entity &nbsp; 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.

Guffa
Good examples of nbsp use. I was looking for some myself, but couldn't think of them.
Pete
A: 

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

ppuschmann
+3  A: 
Christopher Kelly
Safe to assume your first line of code has the in it.
idrumgood
good catch, fixed.
Christopher Kelly
A: 

&nbsp; is stackable, meaning you can create multiple spaces all together.

HTML will only parse one space '' and it drops the rest...

If you want five spaces, you would place 5 x &nbsp;

A: 

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.

Jacob R