Today my colleague showed me an interesting problem. He had a table cell which was supposed to be constrained to a certain width but the content kept running out of bounds and wouldn't wrap.
The table look something like this
| monday tuesday wednesday thursday |
The table is supposed to be constrained to 150 pixel, so the correct one should look like this
| monday tuesday wednesday thursday |
So we were wondering what's wrong and I looked at the source of the page the browser was displaying, take note, this is the source on the CLIENT side, not your server side source. And I saw something interesting, this was what his td tag looked like.
<td>monday tuesday wednesday thursday </td>
I asked him the obvious question, why are you using as a space? He tells me that the data was entered as a comma and the client wants it to be displayed as a space so he did a simple string.replace on it. At first I didn't think too much about it since.. well it IS a space. That was until I remembered what nbsp stood for, non BREAKING space. Keyword here being non breaking, I guess that would mean that the browser will not word wrap on that space hence the name non breaking. So.. sure enough by replacing the with a simple space everything worked fine.
So.. the question now was why do we use nbsp in the first place? Well, for me it all began when I started learning HTML a long long time ago. I was wondering why I couldn't insert multiple spaces to space out a word like this. This was of course when I was using Notepad to make my pages and I realised that browser would basically shrink multiple spaces into one. Then I used Dreamweaver and noticed that it could insert multiple spaces between words, so obviously I take a look at what got used to force the space and found my little friend and I've been using him as a forced space ever since.
So.. obviously now that I know that nbsp might not be the best way to force a space with in every situation I start looking for alternatives. I tried the most obvious one   the ascii code for a space.. and it didn't work the browser collapsed repeats of those into a single space as well. I wonder if there really is an alternative to the dear old nbsp, not that it causes much problems lar!