Invisible Characters in Text: Zero-Width Spaces and Non-Breaking Spaces
A zero-width space (U+200B) has no width and marks a spot where a line may break. A non-breaking space (U+00A0) is as wide as an ordinary space but forbids a break there. They are opposites, and neither is the plain space you get from the space bar.
That matters whenever text moves between apps. Characters you can't see still count as characters. Below: what each one is, where it's used on purpose, why software treats them differently, and what to do with the ones you never meant to have.
Two characters, two jobs
The Unicode Standard describes each one by what it does to line breaking.
The zero-width space (U+200B) "indicates a word break or line break opportunity, even though there is no intrinsic width associated with this character." It is meant for languages with no visible word spacing, such as Thai, Myanmar, Khmer, and Japanese.
The no-break space (U+00A0) "has the same width as U+0020 SPACE," but under normal circumstances no line breaks are permitted between it and the characters around it.
Put side by side:
| | U+200B zero-width space | U+00A0 no-break space | |---|---|---| | Visible width | None | Same as a normal space | | Line break at this spot | Allowed | Not allowed | | Typical use | Break points in scripts without spaces | Keeping two pieces of text together |
There is a related character worth knowing. U+2060 word joiner also forbids a break, like U+00A0, but it has no width. Unicode says it should be used instead of U+FEFF when you need to join words in new text. Character counts can also surprise you, as in why one visible character can be several.
The zero-width space (U+200B) in practice
Wikipedia's entry on the zero-width space calls it a non-printing character used in typesetting to mark word boundaries without showing a space. In UTF-8 it is the three bytes E2 80 8B, and in HTML you can write it as ​ or ​.
Its closest relative is the soft hyphen, except that a soft hyphen prints a hyphen when the line breaks and U+200B prints nothing. In HTML, the <wbr> element functions as a zero-width space.
Two details are easy to miss:
- Justified text. The rendering engine may add letter spacing between letters separated by a zero-width space, unlike around fixed-width spaces. Both Wikipedia and the Unicode Standard say so.
- Domain names. ICANN rules prohibit non-displayed characters, including the zero-width space, in domain names. Most browsers block them because they enable homograph attacks, where a lookalike address fools the reader.
A U+200B that arrives by accident is hard to spot. A word that looks fine can hide a break point inside it.
The non-breaking space (U+00A0) in practice
Wikipedia's article on the non-breaking space notes that text software generally assumes a line break may be inserted anywhere a space occurs. U+00A0 prevents that. Typical uses it lists:
- Keeping a number and its unit together, so "100 km" never splits across lines.
- French typography, before marks such as
:,;,?, and!, and inside guillemets. - German multi-part abbreviations such as "z. B."
It does a second job in HTML. HTML treats a run of whitespace as a single space, but non-breaking spaces prevent that collapsing. That is why is a common way to force extra gaps on a web page. If you write Markdown that ends up as HTML, it also helps to know how to escape special characters in Markdown.
Why tools treat them differently
Both characters are space-like, yet different standards draw the whitespace line in different places. The MDN whitespace glossary lists two of them:
- HTML defines five ASCII whitespace characters: tab (U+0009), line feed (U+000A), form feed (U+000C), carriage return (U+000D), and space (U+0020). U+00A0 is not among them.
- JavaScript defines white space as U+0009, U+000B, U+000C, U+0020, U+00A0, U+FEFF, and any other Unicode Space_Separator code point. U+200B does not appear in MDN's list.
So the same character can count as whitespace in one language and not in another. Code that trims or splits on "spaces" can treat U+00A0 differently from a plain space depending on the language and which definition it uses. Check the documentation for the tool you use rather than assuming.
Finding invisible characters in your own text
You can't fix what you can't see, so make the characters visible first:
- Turn on any "show invisibles" or "show whitespace" option your editor offers.
- Search for the character itself rather than for a space. Pasting a single U+200B into the search box is enough to find every copy.
- Replace what you did not mean to have with a normal space, or delete it. Keep a U+00A0 where it is doing its job, such as between a number and a unit.
If search works on one line and fails on another that looks identical, a hidden character is a likely cause. Strange symbols in a file are a different problem, covered in diagnosing encoding problems in a text file. To scan a whole folder of notes, see searching your notes with grep and ripgrep.
How Carets fits in
Invisible characters cause trouble in text you can't inspect, and plain text is the easiest format to inspect. Carets is a native notes and code editor for iPhone, iPad, and Mac that keeps Markdown, plain text, and code in one place. Your notes stay plain text files, so you can search them and clean them up with ordinary tools. Syntax highlighting for code and config files helps when a stray character lands inside a snippet, and files, projects, and tags keep related notes together. Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
What is the difference between a zero-width space and a non-breaking space?
A zero-width space (U+200B) has no width and allows a line break at its position. A non-breaking space (U+00A0) is as wide as a normal space and forbids a line break there.
Is a non-breaking space the same as a normal space?
No. It looks the same and has the same width, but it's a different character. It blocks line breaks, and in HTML it does not collapse with neighboring whitespace. HTML's list of ASCII whitespace does not include it, while JavaScript's white space definition does.
Is U+200B counted as whitespace in JavaScript?
MDN's list of ECMAScript white space includes U+00A0 and U+FEFF but not U+200B, so U+200B does not appear in that list.
What is the word joiner for?
U+2060 behaves like U+00A0 in forbidding a line break, but it has no width. Unicode prefers it over U+FEFF for joining words in new text.