views:

79

answers:

4

Okay, this is really confusing me. I have some content inside of a div like so:

<div style="background-color: green; width: 200px; height: 300px;">

Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.

</div>

However, the content overflows the DIV (as expected) because the 'word' is too long.

How can I force the browser to 'break' the word where necessary to fit all of the content inside?

+3  A: 

I am not sure about the browser compatability

word-break: break-all;

Also you can use <wbr> tag

<wbr> (word break) means: "The browser may insert a line break here, if it wishes." It the browser does not think a line break necessary nothing happens.

rahul
+1 - It works in Chrome. I'll test Firefox and Opera...
George Edison
Unfortunately, it only works on Chrome :(
George Edison
Nice, can't say I have ever seen these tags before
danixd
+1  A: 

CSS word-wrap:break-word;, tested in FireFox 3.6.3

Babiker
Will this work if the word is too long?
rahul
Yes, I just tested it.
Babiker
A: 

You could pull this off with JavasScript, here is a sample that inserts a "wbr" tag into strings at a position of your choice. I don't think wbr is 100% cross browser, so you could also insert a "-space" to allow the words to wrap.

function wbr(str, num) { 
  return str.replace(RegExp("(\\w{" + num + "})(\\w)", "g"), function(all,text,char){
    return text + "<wbr>" + char;
  });
}
Zachary
+4  A: 

Use word-wrap:break-word;

It even works in IE6, which is a pleasant surprise.

Chi