Justify content with pre-wrap

2.6k views Asked by At

On Firefox, how can you justify text that has the white-space: pre-wrap; CSS attribute?

I need the pre-wrap to prevent the browser from collapsing whitespaces, but it breaks the text-align: justify; property. See http://jsfiddle.net/xpp48knq/ for example.

I would be ok with any solution that doesn't collapse spaces and that justifies content.

1

There are 1 answers

0
E. Kozlovtsev On

Here's some workaround for ur problem.

U should remove 'white-space: pre-wrap' from the element, and replace all spaces in text to '​ '(mean zero-width space plus space). And everything will be working fine in every major browser.

Here's some example: https://jsfiddle.net/gvm3x354/

<div id='output' class='text'>
</div>

.text {
  border: 1px solid black;
  text-align: justify;
  width: 200px;
  height: auto;
}


var output = document.getElementById('output'),
  text = 'I\'m a justified  text with multiple    spaces between   words. Really cool,  so js. Found better workaround? E-mail me!';

text = text.replace(/\s/g, '&#8203; ');
output.innerHTML = text;