Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Justify content with pre-wrap

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.

like image 313
cphyc Avatar asked Jun 17 '15 09:06

cphyc


1 Answers

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;
like image 159
E. Kozlovtsev Avatar answered Oct 13 '22 01:10

E. Kozlovtsev