15

Here's some example HTML and CSS to show the problem:

<p>thisssssssssssss&nbsp;issssssssss a test</p>
<p>thisssssssssssss&nbsp;<span>isssssssssss another</span> test</p>
<p>thisssssssssssss&nbsp;<button>isssssssssss another</button> test</p>

button { display: inline; }

Try it out on this JSFiddle, by resizing the output area.

Result (in Chromium on Ubuntu):

JSFiddle screenshot

As you can see, there is a line break before the <button> in the third example, which I am trying to avoid. The &nbsp; character seems as if it is being ignored (treated as a regular space). The desired result is that there is no break between "this" and "is," just like the first two examples.

I've already found Why do inline-blocks break after non-breaking space?. An answer there suggests using <nobr> or white-space: nowrap. However:

  • I'm setting the <button> to display: inline, so I don't even understand why the problem exists anymore since it's an inline element.

  • I need a pure CSS solution, without any extra HTML in the text before the button. My HTML has to look something like this:

    <p>{{SOME TEXT}}&nbsp;<button>foo</button></p>
    

    and I don't know whether the {{SOME TEXT}} will contain spaces or not. I can add extra HTML around the text, but the solution linked in the answer above requires adding an element within the text itself.

Why is the problem happening even when setting display: inline;, and how can I solve it without modifying the text itself?

Community
  • 1
  • 1
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • I've experienced that before... I concluded that the button element just doesn't care that there's a nbsp. :( – Andrew Barber Nov 22 '14 at 00:31
  • 1
    @AndrewBarber Hmmm... will strict discipline help? I've been considering doing interviews to make sure that all the lazy – tckmn Nov 22 '14 at 00:35
  • 1
    I don't understand the problem. The HTML behaves as expected. What is wrong, and what is expected? – Praveen Kumar Purushothaman Nov 22 '14 at 02:46
  • 1
    @PraveenKumar I expect that the ` ` behaves as normal. It does not behave as normal; there is a line break between the text and the button. – tckmn Nov 22 '14 at 02:47
  • 2
    @Doorknob The ` – Praveen Kumar Purushothaman Nov 22 '14 at 02:50
  • the button is not a text node ... so that is why – Elise Chant Nov 22 '14 at 02:53
  • @PraveenKumar This is not what I am expecting. By setting the `display` to `inline`, I expect that the button behaves similarly to ordinary text or a `` element. However, it does not. – tckmn Nov 22 '14 at 03:03
  • 2
    I think you can make it inline if you make the browser stop thinking it's a button. e.g. `-webkit-appearance: none` – bjb568 Nov 22 '14 at 03:08
  • @bjb568 Nope, doesn't seem to work. – tckmn Nov 22 '14 at 03:09
  • 1
    The question *seems* to be clear, but you have accepted an answer that violates your explicit requirement. If that answer is acceptable, then the question is a duplicate of the question you refer to—and the answer is essentially the same as one of the techniques suggested in the answers to the old question. – Jukka K. Korpela Nov 22 '14 at 08:16
  • possible duplicate of [PHP Parsing Problem -   and Â](http://stackoverflow.com/questions/4515117/php-parsing-problem-nbsp-and-%c3%82) – davidcondrey Nov 22 '14 at 08:19
  • @dcc That has absolutely nothing to do with my question. – tckmn Nov 22 '14 at 13:06
  • @JukkaK.Korpela Sorry, that was just bad wording on my part (see my comment on the accepted answer). – tckmn Nov 22 '14 at 13:09
  • 1
    @Doorknob, your comment says that you have edited the question, but you haven’t (and an edit like “I can't add HTML within the text itself, but around it is okay” would not clarify if you don’t say *which* text). – Jukka K. Korpela Nov 22 '14 at 13:26
  • @JukkaK.Korpela Yes, I [have edited](http://stackoverflow.com/posts/27072628/revisions) the post. I do, in fact, say "*which* text" in the previous sentence. – tckmn Nov 22 '14 at 13:40

1 Answers1

10

Can you put a span before the nbsp?

<p>thisssssssssssss<span id="b">&nbsp;<button>isssssssssss anotherrrrrrrrr</button></span> test</p>

#b {
    white-space: nowrap;
}

http://jsfiddle.net/bggk33du/10/

AaronS
  • 465
  • 1
  • 7
  • 10
  • One little thing I just noticed: the `a` class is unnecessary. It's not even being applied to the `p` element (since it has the `.a` class, not the `a` class), and removing it has no effect and it still works. So if you remove that, it's a little cleaner. http://jsfiddle.net/bggk33du/8/ – tckmn Nov 22 '14 at 03:52
  • Thanks. That was a leftover experiment. – AaronS Nov 22 '14 at 03:59
  • 1
    The question emphatically says “without any extra HTML in the part before the ` `”. – Jukka K. Korpela Nov 22 '14 at 08:14
  • 1
    @JukkaK.Korpela Sorry, I might not have worded that part the best way. I've edited the question, which will hopefully clarify that I can't add HTML within the text itself, but around it is okay. – tckmn Nov 22 '14 at 13:08