Code is supposed to be exact and "as-is". Sometimes, showing two spaces is important. In a comment, it is not possible to show code with  multiple   spaces. This became apparent in this answer (see comments).

EDIT: apparently this bug is larger then I thought: multiple spaces here are neither preserved in normal question text. Pity (though I couldn't find this reported before, I'd assume it is).

Workaround: use Alt+0160 (windows), or U+00A0 (Unicode NBSP), as I did in the example above.

  • @voyλger nice alternative workaround (but try to select and copy it).
    – Abel
    Nov 17 '09 at 22:05
  • @Abel: What is the problem? On FF, If I select the text and paste it I get the spaces on the pasted text.
    – perbert
    Nov 17 '09 at 23:37
  • I used Opera (doesn't work). Now tested with IE (doesn't work) and FF (works). Looking at the code (<code>with </code><code> multiple </code><code> </code><code> spaces</code>) there are spaces (but most space is created by CSS), but when selecting they aren't "blue" in any browser, and only FF copies them. Interesting little browser quirks.
    – Abel
    Nov 18 '09 at 13:37
  • @Abel: you are right, Chrome doesn't like it either. It must be because generally in HTML 1+ space becomes 1 space. I think that firefox's (pronounce it out loud, I dare you) is the correct behavior in this case.
    – perbert
    Nov 21 '09 at 23:21
  • Firefox is correct in copying, but not in selecting. Unfortunately, the W3 and other standards bodies say little if anything about how selections are supposed to look (or what Ctrl-C should copy consecutively), that's totally up to the UI.
    – Abel
    Nov 30 '09 at 0:05
  • 3
    Couldn't code {white-space:pre-wrap} be added to the SE default stylesheets?
    – Lri
    Jul 7 '11 at 2:49
  • 2
    This is still an issue, and now unicode nbsp doesn't work either (at least as far as I can test).
    – Chris Down
    Dec 14 '11 at 16:09
  • I guess not, @Lri, as pre-wrap also enables newlines? Those don't get any love in comments.
    – Arjan
    Mar 21 '12 at 17:15
  • @Arjan But aren't newlines removed before the CSS anyway?
    – Lri
    Mar 22 '12 at 6:21
  • No, @Lri, they are not. (See the HTML page source for this comment.)
    – Arjan
    Mar 22 '12 at 6:52
  • 1
    @Oded Can you at least post an explanation as to why it is status by design or declined instead of simply flooding the front page with your retagging? Feb 22 '13 at 14:49
  • @LoremIpsum - Sorry, trying to close old old old bugs. Didn't mean to flood the front page.
    – Oded
    Feb 22 '13 at 14:50
  • @Oded I don't mind the flooding; in fact, I'm happy the devs are closing/fixing bugs. However, without a comment/answer, I don't know if you mean "This is so old that changes since then have rendered it invalid" or "This is so lame that I don't care to respond" or "This is how it will be and I'm not going to fix it" Feb 22 '13 at 23:12

I've added this to our stylesheets:

code {
    white-space: pre-wrap; /* don't collapse multiple spaces in code */

– no idea why we never did that; it's an easy enough fix, and these days it's supported by all browsers. This is in the next build.

  • 10
    Well, there is one reason...
    – Tim Stone
    Feb 24 '14 at 21:42
  • @TimStone Well if that was the reason we never did it, I assume someone would've answered here :) Yep, stupid me, this behavior is right there in the spec. I read it and I still missed it.
    – balpha
    Feb 25 '14 at 6:02
  • This is causing some problems in Firefox.
    – animuson StaffMod
    Mar 29 '14 at 15:56
  • Thanks for the pointer, @animuson. Will look.
    – balpha
    Mar 31 '14 at 13:51
  • 1
    was this fixed correctly (if so, multiple consecutive spaces should show)? Apparently not, have you reverted the change of last year?
    – Abel
    Sep 6 '15 at 22:21
  • 1
    @Abel You're right, this was removed (or rather, overwritten) during the great CSS overhaul of 2014/2015. I'll talk to the person who made that change to find out what the intention was.
    – balpha
    Sep 7 '15 at 5:37
  • 3
    So can the status-completed be removed please? May 15 '17 at 19:26
  • This never made it into mobile.css, which means this problem still exists on mobile. Dec 7 '19 at 17:18

You do have a workarround:

`with `` multiple `` `` spaces`

Looks like

with  multiple   spaces

in a comment

  • 1
    wowch. I'd hate to hand parse my code to add that...
    – Pollyanna
    Nov 17 '09 at 20:35
  • 1
    Unfortunately, doesn't work any longer..., see: comment with `` multiple `` `` spaces
    – Abel
    Dec 19 '11 at 15:24
  • 1
    See this post for a working workaround
    – CharlesB
    Feb 16 '12 at 9:01

In one of my comments I've suggested to apply sed s/ / /g… which looks completely stupid because spaces are not rendered properly (as you can (not) see).

Now, I've tested the CSS snipset provided in a comment by Lri:

code { white-space:pre-wrap; }

It does the job of fixing this. Why isn't this applied yet?

  • "Why isn't this applied yet?" >> my guess, it doesn't have priority. Which seems kinda odd for a site about coding though...
    – Abel
    Mar 21 '12 at 8:26
  • Or, @Abel, as it also enables newlines? Those don't get any love in comments.
    – Arjan
    Mar 21 '12 at 17:14
  • @Arjan: everything is filtered anyway for whatever illegal characters are disallowed. Can hardly think it's an issue to either replace spaces with non-breaking spaces, or to remove any \r\n.
    – Abel
    Mar 21 '12 at 20:34
  • 1
    Then I'd not vote for replacing spaces with non-breaking spaces, to avoid any issues with copying & pasting code fragments, @Abel. That may indeed make CSS with removing line breaks a good option then. (Though that might need some conversion of existing comments, if comments are sanitized upon saving rather than upon rendering.)
    – Arjan
    Mar 21 '12 at 21:03
  • @Arjan: while I agree (and I think this is up to the implementers, if at all they want to do something about this), copy/pasting with nbsp's in it doesn't give me trouble with Visual Studio. Not sure of other editors though.
    – Abel
    Mar 21 '12 at 21:13
  • 1
    @Abel At least on OS X non-breaking spaces are not usually converted to normal spaces, and running code with them results in an error.
    – Lri
    Mar 22 '12 at 6:28

You must log in to answer this question.

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