1

When I try to use

`hi       another text`

it returns me hi another text - spaces are breaked.

5
  • Use the { } button in the markdown editor. It preserves multiple spaces.
    – user102937
    Commented Sep 22, 2011 at 16:37
  • 5
    @RobertHarvey He knows that, he used that feature to ask the question.
    – agf
    Commented Sep 22, 2011 at 16:39
  • @agf: Meh. Multiple spaces in a single line of code is exceedingly rare (except maybe in the Whitespace language).
    – user102937
    Commented Sep 22, 2011 at 16:40
  • Related: meta.stackexchange.com/questions/83522/…
    – user154510
    Commented Sep 22, 2011 at 17:04
  • It's a CSS issue. When you use <code> tags (which are generated by the backticks), multiple spaces are collapsed into one by your browser. Commented Sep 22, 2011 at 17:06

4 Answers 4

6

The fact that non-breaking spaces render in html as separate characters rather than a single character is a side-effect of what &nbsp; is really about: line breaks. The non-breaking space is considered significant white space (as opposed to normal insignificant whitespace) for the purpose of preventing text from wrapping to a new line at that location. It has nothing to do with showing wider gaps/multiple spaces between words, and the fact that it often does so is a mere accident or side effect of the significant whitespace designation.

Once you realize this, rendering spaces from inline code blocks using &nbsp; makes absolutely no sense. It might be a good idea to do something else to make this white-space significant rather than insignificant, but a non-breaking space is not the way to go.

A better option would be to use U+2002 (en space) or U+2003 (em space): &ensp; or &emsp; (or &#x2002; / &#x2003;).

4

Like most things that aren't needed often enough to be in the Markdown syntax, you can achieve this using HTML.

<code>hi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another text</code>.

The result is hi     another text.

You can also use the unicode non-breaking space character U+00A0 inside backticks. You can copy them from the character map (or from this post).

`hi     another text`
   ^^^^^
   non-breaking spaces

The result is hi     another text.

4

You don't actually want non-breaking spaces; the results aren't pretty (this styling is meant to allow inline code, which depending on its position in the text may well need to be broken across multiple lines - spaces are a pretty good place to do this). You simply want the spaces to be preserved in the output, which the Markdown renderer does, but your browser does not. Adding the style,

white-space: pre-wrap;

...to the code tag would correct this. As hammar notes, you can work around the current lack of such styling by manually inserting &nbsp; into a <code> span... However, to avoid ugly breaking, I suggest altering spaces and non-breaking spaces:

<code>hi&nbsp; &nbsp; &nbsp;another text</code>

You can see the difference here: after some text to push the code toward the end of the line hi     another text. Vs. some other text to put the code with alternate non-breaking spaces hi     another text (note the wasted space at the end of the first line as the entire line of code was wrapped).

2
  • 1
    As an aside: the pre-wrap is used on the mobile sites, to allow for wrapping the <pre> blocks. Anyone who doesn't know it: click the mobile link in the footer to see how that wrapping works, while still preserving the whitespace.
    – Arjan
    Commented Sep 22, 2011 at 17:30
  • As an alternative to putting spaces between the non-breaking spaces, you could put zero-width spaces (&#x200B) in instead. Then, you'll always see the exact number of spaces that you want, even when the line breaks in the middle of them.
    – RobH
    Commented Aug 9, 2013 at 17:03
1

Another solution is to type Alt + (Keypad 0 Keypad 1 Keypad 6 Keypad 0) which also allows you to write hi     another text.

1
  • Windows only, of course, but a convenient solution.
    – Jonathan
    Commented May 20, 2015 at 4:00

Not the answer you're looking for? Browse other questions tagged .