html - What's the difference between " " and " "?

ID : 20227

viewed : 3

Tags : htmlhtml

Top 5 Answer for html - What's the difference between " " and " "?

vote vote

98

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, just like it wouldn’t be wrapped in the middle of a word.

Furthermore as Svend points out in his comment, non-breaking spaces are not collapsed.

vote vote

90

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.

vote vote

71

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 the below one will keep the spaces when rendered. --> <p>Word1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Word2</p>

vote vote

62

Not an answer as much as examples...

Example #1:

<div style="width:45px; height:45px; border: solid thin red; overflow: visible">     Hello&nbsp;There </div>   

Example #2:

<div style="width:45px; height:45px; border: solid thin red; overflow: visible">     Hello There </div> 

And link to the fiddle.

vote vote

60

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 

Top 3 video Explaining html - What's the difference between "&nbsp;" and " "?

Related QUESTION?