CS Trivia#8: What is & nbsp; ?!
Tags: html, webdev, todayilearned, codenewbie
Published: 2020-04-15 03:15:03

**edit: I know that the title contains a typo. If you're curious why, jump to the ["Typing it out" section](#typing-it-out).**


Welcome to my little CS trivia series. Every week I post a new trivia piece.

Today's question is...

---

## What is `&nbsp;` ?!

In all honesty, the first time I saw this during bootcamp, I really thought it was some encoding bug. Later I noticed it being a solution proposed on StackOverflow to some problem and that really baffled me. First of all —- what is it? Secondly, why is it so odd? And finally, who remembers such a line of nonsensical characters?

## The non-breaking space

These letters are really just a minimalistic version of "non-breaking space", which is a space character that prevents an automatic line break at its position. So, really, it's just another empty space but with different properties. 

## Typing it out

In fact, you can type the character with your own keyboard by pressing `option + space` on Mac or `Ctrl+Space` on Windows. It will look like space but its encoding will be different.
When it comes to HTML you can use the names of this whitespace symbol by either writing `&nbsp;` or `&#160;`. By the way, see what happens when I try to google it:

![Screen recording showing google interprets &#160; as an empty space and not letters](https://dev-to-uploads.s3.amazonaws.com/i/68w2df9k19xgz0f84p34.gif)

As you see, Google interprets `&#160;` as non-breaking space and — it cannot search for it. However, if you google `&nbsp;`, you will see plenty of results. The reason why Google "breaks" on `&#160;` is because it is a so-called "decimal HTML entity" whereas  `&nbsp;` is the same character in "human" version of the html, which Google doesn't speak. 

Another example from just a minute ago: I just needed to change the title of this post after publication because...

![Screen recording showing the blog post title on the dev page flicking between `&nbsp;` and a visually empty space](https://dev-to-uploads.s3.amazonaws.com/i/cf1gp29i2b9284nkqivl.gif)

... otherwise the blog post title on the dev page flicking between `&nbsp;` and a visually empty space, which is because markdown and html view `&nbsp;` as empty space. I know it contains a typo. I know.

## The difference between `&nbsp;` and `""` 

The `&nbsp;` and an empty string (`""`) are different. For instance, in HTML, trailing empty strings will collapse into just one, whereas non-breaking space will always print in the amount you desired. See here:

`<span>Word1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Word2</span>`
gets translated to:
<span>Word1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Word2</span>

whereas `<span>Word1     Word2</span>` (6 spaces in markdown version)
gets translated to:
<span>Word1    Word2</span>

Now, that sounds like an easy fix for all the styling and centering problems but don't give in to the temptation. 

## When not to use `&nbsp;`?
Although you might be tempted to add extra space to your HTML elements by adding the non-breaking space, you should not do that. Instead, resort to `padding` or `margin` properties of CSS. Otherwise, you will run into problems when trying to make your page responsive. 

## When to use `&nbsp;`?
Use it as its design intended: between two words that should not have a line break inserted between them by word wrapping. As explained by Alenanno on [StackExchange](https://english.stackexchange.com/questions/28467/when-is-it-appropriate-to-use-non-breaking-spaces/28476#28476):

> 1. It is advisable to use a non-breaking space (also known as a hard space) to prevent the end-of-line displacement of elements that would be awkward at the beginning of a new line:
 - in expressions in which figures and abbreviations (or symbols) are separated by a space (e.g. 17 kg, AD 565, 2:50 pm);
 - between the date number and month name (e.g. 3 June or June 3);
 - in other places where breaking across lines might be disruptive to the reader, especially in infoboxes, such as £11 billion, June 2011, 5° 24′ 21.12″ N, Boeing 747, after the number in a numbered address (e.g. 123 Fake Street) and before Roman numerals at the end of phrases (e.g. World War II and Pope Benedict XVI).
2. A hard space can be produced with the HTML code &nbsp; instead of the space bar; 19&nbsp;kg yields a non-breaking 19 kg.
3. A literal hard space, such as one of the Unicode non-breaking space characters, should not be used since some web browsers will not load them properly when editing.
4. Unlike normal spaces, multiple hard spaces are not compressed by browsers into a single space.
5. A non-breaking space should be used before a spaced [en dash](http://en.wikipedia.org/wiki/Dash#En_dash).

***
Cover photo by Felix Mittermeier from [Pexels](pexels.com)