Documentation

No-break spaces

How to write a non-breaking space in HTML ( ,  , U+00A0), when to use the narrow U+202F, and how polytypo inserts them per locale automatically.

Updated · spec 1.33.0

The nbsp rule makes a line break fall where the language allows it. It turns an ordinary space into a no-break space (U+00A0) or a narrow no-break space (U+202F) where a locale needs one: before French ? ! ; :, between a number and its unit, after a short Russian or Czech preposition, between initials and a surname. In a few places it inserts a space the author did not type.

It runs last (order 70), after every other rule has settled the characters around each space, and it is on by default in every mode. Almost everything it does is conversion of a space that is already there. Text that already carries the right no-break spaces passes through unchanged.

In HTML #

A no-break space looks like an ordinary space, but the browser never breaks a line at it and never collapses a run of it. HTML gives U+00A0 several spellings; the narrow no-break space U+202F has no named one.

CharacterWays to write it in HTML
No-break space, U+00A0  (also  ),  ,  , or the character itself
Narrow no-break space, U+202F ,  , or the character itself. There is no named reference

Use U+202F where a narrow gap is the convention, such as before French ? ! ;, and check that your font has a glyph for it (see "Narrow versus ordinary no-break space" below).

You do not have to type either one by hand. In html mode polytypo inserts and converts these spaces in the text of the document, per locale, and leaves tags, attributes and existing character references exactly as written. It writes the characters themselves, not entities.

transform(input, { locale: "fr", mode: "html" })

Input

<p title="Vraiment ?">Vraiment ? Oui, 20 km !</p>

Output

<p title="Vraiment ?">Vraiment ? Oui, 20 km !</p>

How it works #

The rule is one scan with ten sub-rules, N1 to N10, each driven by a list in the locale file. When two sub-rules want the same position, the earlier one wins and the position is edited once.

Sub-ruleLocale fieldWhat it does
N1beforePunctuationU+00A0 before the listed marks
N2narrowBeforePunctuationnarrow no-break space before the listed marks
N3afterShortWordsU+00A0 after a listed short word
N4abbreviationsU+00A0 for every space inside a listed abbreviation
N5beforeUnits, spacedUnits, closedUnitsU+00A0 between a number and a listed unit, or no space at all for a closed unit
N6afterSymbolsU+00A0 between a listed symbol and the number after it
N7initialBindingU+00A0 inside initials and between initials and a surname
N8quotes.*.innerSpacethe space just inside a quotation pair
N9beforeNumberU+00A0 between a listed abbreviation and a following number
N10beforeWordU+00A0 between a listed abbreviation and a following word

N1, N2, N5 and N8 insert a space. N5 also removes one, for units the locale writes closed. The others convert an existing space and do nothing if there is none.

Before punctuation (N1, N2) #

French sets a space before : ; ! ?. The fr locale puts : in beforePunctuation (U+00A0) and ; ! ? in narrowBeforePunctuation (U+202F). If the author typed no space, one is inserted. If there is a space, it is converted. If the correct character is already there, nothing changes.

transform(input, { locale: "fr" })

Input

Vraiment ? Oui ! Et alors ; voici : un test.

Output

Vraiment ? Oui ! Et alors ; voici : un test.

Several guards keep this from damaging text:

  • Only the first mark of a run takes a space: Vraiment?! gets one narrow space before the ? and none before the !.
  • The mark must be followed by a space, a closing bracket, a quote mark, an ellipsis, another listed mark or the end of the text. http://example.org and 12:30 therefore keep their colons tight.
  • The space after an opening quotation glyph belongs to N8, so «? gets a no-break space after the guillemet and none before the question mark.
  • A semicolon that ends a character reference (&#160;, &amp;) is skipped, so references survive in text mode.
  • A line never begins with a no-break space, and a tab or a thin space someone placed deliberately is left alone.
transform(input, { locale: "fr" })

Input

Vraiment?! Voir http://example.org et 12:30.

Output

Vraiment ?! Voir http://example.org et 12:30.

Short words (N3) #

Russian, Polish and Czech do not leave a one- or two-letter preposition at the end of a line. The word must follow a space, an opening bracket, an em or en dash or the start of the text, and be followed by exactly one ordinary space and then a letter, digit or opening bracket. The first letter is matched in either case; the rest must match exactly.

transform(input, { locale: "ru" })

Input

Он живёт в Москве и работает на заводе.

Output

Он живёт в Москве и работает на заводе.
transform(input, { locale: "cs" })

Input

Byl jsem v Praze a u moře.

Output

Byl jsem v Praze a u moře.

The Polish list differs from the Czech one (no k or s, but w), so the same sentence structure binds different words:

transform(input, { locale: "pl" })

Input

Ala ma kota i idzie w las.

Output

Ala ma kota i idzie w las.

A word after a hyphen is not a free-standing word. In из-за дождя the за is the tail of one compound preposition and is not bound.

Abbreviations (N4, N9, N10) #

abbreviations lists multi-token forms written with ordinary spaces, such as German z. B. or Russian т. е.. Every space inside a listed form becomes U+00A0. The text on both sides must not be a letter or digit, and case must match exactly.

beforeNumber holds abbreviations that bind to a number after them (Nr. 5, S. 12). beforeWord holds those that bind to a word (Mme Hugo, ул. Ленина). Both require exactly one space and never insert one.

transform(input, { locale: "de-DE" })

Input

Es sind 20 km, z. B. bis Berlin, siehe Nr. 5 und S. 12, § 3.

Output

Es sind 20 km, z. B. bis Berlin, siehe Nr. 5 und S. 12, § 3.

beforeWord has no way to see a sentence boundary, so locale files list only forms where the wrong reading is rare. In Russian, ул. binds forward to the street name. г. is a beforeUnits entry that binds backwards to a year, not a beforeWord entry:

transform(input, { locale: "ru" })

Input

Мы жили на ул. Ленина, г. Москва, в 1990 г. см. № 5

Output

Мы жили на ул. Ленина, г. Москва, в 1990 г. см. № 5

Units and symbols (N5, N6) #

N5 reads three lists, and a unit belongs to exactly one of them:

  • beforeUnits converts an existing space to U+00A0 and does nothing else.
  • spacedUnits converts an existing space, and inserts U+00A0 when the number and the unit touch.
  • closedUnits removes the single space between the number and the unit.

A unit binds to the digits before it when it is followed by something other than a letter or digit. Digits glued to a letter (H2 O) do not count as a number.

transform(input, { locale: "de-DE" })

Input

Es sind 20 km und 5 %.

Output

Es sind 20 km und 5 %.

Which list a unit goes in is the locale's call, taken from its national sources: where they allow both forms the locale writes the closed one, and where only the spaced form is attested it writes the space. In de-DE 20km and 50% both come out with a no-break space, and in ru 50 % comes out closed.

transform(input, { locale: "de-DE" })

Input

Es sind 20km und 50%.

Output

Es sind 20 km und 50 %.
transform(input, { locale: "ru" })

Input

Скидка 50 %, температура 20°C.

Output

Скидка 50%, температура 20 °C.

Insertion and removal are skipped where the text is not a plain number followed by a unit: next to a range dash, after a hyphen or underscore, in a product of numbers (2 x 3kg), and in a modulo (7 % 3). afterSymbols works the other way: § 3, № 5.

Initials (N7) #

initialBinding is none, chain or single. An initial is one capital letter and a full stop at the start of a token.

  • chain (en-US, de-DE, ru and others): the spaces between initials bind, and so does the space before the surname once the chain has two initials. A lone A. Smith does not bind, because it cannot be told from the end of a sentence.
  • single (fr, cs): a single initial also binds to the following capitalised word.
  • none: the sub-rule is off.
transform(input, { locale: "en-US" })

Input

J. R. R. Tolkien wrote it. Meet A. Smith there.

Output

J. R. R. Tolkien wrote it. Meet A. Smith there.

A capital letter and a full stop that follow a lowercase letter and a full stop, as in z. B., are the tail of an abbreviation and not an initial.

Space inside quotation pairs (N8) #

When a pair's innerSpace is nbsp, as it is for the French primary pair, N8 inserts or converts the space inside the guillemets. A spaced input and an unspaced input end up the same:

transform(input, { locale: "fr" })

Input

Il a dit « mot » et «autre».

Output

Il a dit « mot » et « autre ».

Narrow versus ordinary no-break space #

U+00A0 is a no-break space of normal width. U+202F is narrower, which is how French sets the space before ? ! ;. N2 always targets the narrow space. No shipped locale gives a quotation pair a narrow inner space, so inside guillemets the rule writes U+00A0.

Many fonts have no glyph for U+202F. The browser then falls back to another font for that one character and the line width shifts, or a PDF renderer drops the glyph. The narrowNbsp option replaces the narrow target with U+00A0 everywhere the rule would have written U+202F:

transform(input, { locale: "fr", narrowNbsp: "nbsp" })

Input

Vraiment ? Oui ! Et alors ; voici : un test.

Output

Vraiment ? Oui ! Et alors ; voici : un test.

The option changes what is written, not which positions are chosen or what is read. An existing U+202F at a position the rule owns is normalised to the target. Elsewhere it is left alone. Valid values are narrow (the default) and nbsp; anything else raises POLYTYPO_INVALID_OPTION, even when nbsp is switched off. See Options. Converting the output yourself with replaceAll is not equivalent: feeding that result back through the rule turns the spaces into U+202F again.

Locale data #

Locales differ in which lists are filled. Each entry is attested by a source for its locale.

  • fr, fr-CA: punctuation lists, French abbreviations, M. Mme Mlle, units, art. n°, guillemets with a no-break inner space (fr-CA has the colon only).
  • ru, uk: short prepositions, abbreviations such as т. д., units and №, ул., initials in chains.
  • de-DE, de-CH: z. B. d. h., Nr. S. Art., units, §.
  • cs, pl: one- or two-letter prepositions, §, units, Czech tj. tzv., Polish lek. med..
  • en-GB, en-US, sv: units (sv also §); en-US adds initial chains.
  • el: every list is empty. For Greek the rule changes nothing, and the source for Greek explicitly rejects a space before punctuation.
transform(input, { locale: "el" })

Input

Τι κάνεις; Έχει 10,5 % φέτος και «καλημέρα».

Output

Τι κάνεις; Έχει 10,5% φέτος και «καλημέρα».

Edge cases #

  • Edges. A no-break space is never inserted at the start or end of a text unit, and never next to a line break. In html this matters because a text node often begins at a tag.
  • Other spaces. A tab, a thin space (U+2009), a figure space (U+2007) or any other fixed-width space is left as the author typed it.
  • Markup boundaries. In html and markdown, a French mark that follows an inline element still gets its space: <em>non</em> ! becomes <em>non</em> plus a narrow space plus !. The cost is that 12:<b>30</b> is read as sentence punctuation and gets a space before the colon. A Russian preposition before an inline element, as in в <em>Москве</em>, stays unbound.
  • Case. Short words match with a capital first letter. Abbreviations, units and symbols match exactly, so Nr. is not nr..
  • Longest match. If two list entries match at a position, only the longer is considered. If it fails a guard, the shorter is not tried.
  • Known miss. M. dupont with a lowercase surname is not bound, because the initial rule needs a capital after the space.

Normative text: spec/rules/nbsp.md.

Already using polytypo? Add our badge

Two lines: a script tag and a span. No per-embed network call. Pick a language and a theme, then copy the code into your footer or credits. Embedding notes cover Content Security Policy. Sites with the badge are listed on Sites using polytypo.

<script async src="https://polytypo.dev/badge.js"></script>
<span data-polytypo-lang="en" data-polytypo-theme="light"></span>

Search runs in your browser. Nothing is sent anywhere.