4

I've been trying to remove the line break that is caused by a break tag

I have come up with a solution that works in chrome, but not in firefox / IE11. Kind of curious if there is a CSS only solution that I could use in this situation that would work across most modern browsers:

HTML

<p>This line breaks in firefox,<br> but not chrome</p>

CSS

br {
    display: inline-block;
    content: " ";
    width: 7px;
}

JSFiddle

Edit:

  1. The break tag also needs to act like a space between the two words.
Mathias Rechtzigel
  • 3,596
  • 1
  • 18
  • 37

2 Answers2

0

I tried a few CSS methods and none of them are working in Firefox. I would suggest using a little bit of JavaScript to help you out.

Using jQuery, or just plain JS, insert a spacer element after each <br>

$('br').after('<span class="spacer"></span>');

The CSS:

br { display: none; }
.spacer { content: "\00a0"; }
Alex K
  • 14,893
  • 4
  • 31
  • 32
  • If you're already using javascript, why go through the trouble of adding a spacer span instead of just replacing it with a space? – Zach Saucier Aug 01 '14 at 19:38
  • Because to switch back you'd just need to do `br { display: block; }` `.spacer { display: none; }`. If you simply replace the `
    ` elements, you have no obvious way to return to the original state. I suppose you could store the original content as a string in a variable though.
    – Alex K Aug 01 '14 at 19:54
  • You could also just have a   instead of a span. – Mathias Rechtzigel Aug 01 '14 at 19:56
  • Sure, but then what if you have other ` `s that were there before? Now you've got `
    `s where you don't want them.
    – Alex K Aug 02 '14 at 15:36
  • 1
    `content: " ";` will not render a space, it will print " " – misterManSam Aug 05 '14 at 14:57
  • Whoops, you're right. Need to convert html entities to hex to use them in CSS. `content: "\00a0";` Updated answer. – Alex K Aug 05 '14 at 16:40
0

Using the following markup works cross browsers:

HTML

<div class="test">WORD<br>&nbsp;WITH<br>&nbsp;SPACE</div>
<div class="">WORD&nbsp;<br>WITH&nbsp;<br>SPACE</div>

CSS:

.test br{
    display: none;
}

Use case is if you want to have a
tag at a certain media query while still retaining spaces between the letters. JSFIDDLE

Mathias Rechtzigel
  • 3,596
  • 1
  • 18
  • 37