Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a non-breaking space before an inline-block-default element, such as a <button>?

Tags:

html

css

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?

like image 340
tckmn Avatar asked Nov 22 '14 00:11

tckmn


People also ask

How do you put a space between inline elements in HTML?

HTML Break (<br>) Tag If you want to prevent a line break between two words, use a non-breaking space. If you want to insert a line break, use the HTML break tag, written as <br>.

Which tag is used to create a non-breaking space?

The &nbsp; character entity is used to denote a non-breaking space which is a fixed space.

What elements are inline block by default?

The answer is the span element.


1 Answers

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/

like image 169
AaronS Avatar answered Oct 27 '22 11:10

AaronS