Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent linebreaks after an inline image?

I noticed that my browser may set a line break after an <img> tag, even if this image tag is followed by &nbsp;:

<p style="width: 12ex; font-family:monospace;">
  12345678 <img style="width: 2ex" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/25px-Smiley.svg.png">&nbsp;123
</p>

The smiley should be on the second line, because the image tag is followed by nbsp;. I can force this behavior by adding a <span> with white-space: nowrap:

<p style="width: 12ex; font-family:monospace;">
 12345678 <span style="white-space: nowrap"><img style="width: 2ex" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/25px-Smiley.svg.png">&nbsp;123</span>
 </span>
</p>

Is there a solution without adding an additional <span> tag? For example: Is there a CSS statement for <img> to prevent line breaks after it?

Note: The CSS of the <p> should not be altered. I only use it to simulate the problem.

like image 253
Stephan Kulla Avatar asked Oct 20 '22 23:10

Stephan Kulla


1 Answers

Yea, just use css's display: inline-block

img {
    display: inline-block;
}
<p style="font-family:monospace;">
  sometext<img style="width: 2ex" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/25px-Smiley.svg.png">&nbsp;sometext
</p>

Just make sure the p is wide enough, so it doesn't need to wrap. (I removed the width style)

This doesn't appear to work in Firefox 33.

like image 156
Cerbrus Avatar answered Nov 01 '22 13:11

Cerbrus