Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser compatible word wrap and whitespace: pre?

Tags:

css

I need text within a div to be preserved and to wrap. So far I am having a tough time coming up with a solution. The best solution I've been able to find doesn't work for all browsers.

The following works in Chrome and IE6+, but in Firefox the text is not wrapping.

 white-space: pre; 
 word-wrap: break-word;

I've found that for whatever reason the text does not wrap in Firefox with white-space:pre. And -moz-pre-wrap does not work in Firefox 3.5 (why??), only pre-wrap. BUT when I add pre-wrap to the list, IE 6 and 7 don't work. Very frustrating.

The code:

.introsub {
  position: relative;
  top: 30px;
  left: 25px;
  width: 550px;
  font-weight: normal;
  line-height: 1.5em;
  overflow: auto;
  margin: 0;
  padding: 1.5em; 
  white-space: pre; 
  word-wrap: break-word;
}
like image 269
Logan Avatar asked Dec 10 '10 20:12

Logan


People also ask

What is whitespace pre-wrap?

pre-wrap. Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks.

What is the difference between the pre and pre line options?

"pre" preserves the text as it is, regardless of spaces and if the text fits in the page (it will create a scroll bar) "pre-line" leaves only a single white space if more than one have been written and sends the text to a new line if it doesn't fit in the page.

Can Web browsers normally wrap text automatically?

When it comes to word wrapping, the default behavior of just about every browser is to break & wrap on whitespace and punctuation. This is not considered "responsive" in the modern sense of the term, but just readable and expected behavior from computer applications.

What is white space Nowrap in CSS?

nowrap: When the white-space property of CSS is set to nowrap every sequence of two or more white-spaces will appear as a single white-space. The content in the element will not be wrapped to a new line unless explicitly specified.


2 Answers

The CSS3 properties don't always work as we would like them to work :). Still, I don't see a point in mixing white-space:pre and word-wrap:break-word.

The former will not wrap the text unless a <br /> tag is encountered. The second one will do the opposite: break the words whenever it's necessary, even in the middle of a word. They seem to be in conflict, and the most obvious answers to why different browsers react differently to these properties is that

  • they support either of the two properties
  • since they are in conflict, the precedence is undefined or different in each browser

(I can't be sure though, I'm not really an expert here).

I suggest you take a closer look at this and this and then decide on what should be used in your particular case.

[EDIT]

You might want to try this (should work in ALL browsers):

white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap; /* ancient Opera */
white-space: -o-pre-wrap; /* newer Opera */
white-space: pre-wrap; /* Chrome; W3C standard */
word-wrap: break-word; /* IE */

I haven't tested this, but I believe it should do the trick for you.

like image 112
mingos Avatar answered Oct 06 '22 04:10

mingos


I know this is a very old issue, but since I have just ran into it I feel the urge to answer.

My solution would be:

Use white-space: pre-wrap; as it is nearly the same as white-space: pre;, just adds linebreaks. (reference: http://www.quirksmode.org/css/whitespace.html)

Then I would specifically target old-ie with either conditional comments (reference: http://www.quirksmode.org/css/condcom.html) or with browser specific css hacks (http://paulirish.com/2009/browser-specific-css-hacks/).

An example for the second method:

.introsub {
  position: relative;
  top: 30px;
  left: 25px;
  width: 550px;
  font-weight: normal;
  line-height: 1.5em;
  overflow: auto;
  margin: 0;
  padding: 1.5em; 
  white-space: pre-wrap; 
  word-wrap: break-word;
}

/* IE6 and below */
* html .introsub  { white-space: pre  }

/* IE7 */
*:first-child+html .introsub { white-space: pre  } 

This will be enough to force it to wrap.

like image 32
vinczemarton Avatar answered Oct 06 '22 05:10

vinczemarton