Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a line break in a long word in a DIV?

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?

like image 614
Nathan Osman Avatar asked Jun 17 '10 04:06

Nathan Osman


People also ask

How do you force a line break in text?

Pro tip: Place a <br> element at each point where you want the line of text to break. Meaning, the text after the <br> will begin at the start of the next line of the text block.

How do I break a line inside a div?

Basic HTML Line Break Syntax You can insert line breaks in HTML with the <br> tag, which is equivalent to a carriage return on a keyboard.

How do you break a word to the next line in CSS?

The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. word-break: break-all; It is used to break the words at any character to prevent overflow.


2 Answers

Use word-wrap:break-word;

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


word-wrap: break-word has been replaced with overflow-wrap: break-word; which works in every modern browser. IE, being a dead browser, will forever rely on the deprecated and non-standard word-wrap instead.

Existing uses of word-wrap today still work as it is an alias for overflow-wrap per the specification.

like image 193
Chi Avatar answered Sep 24 '22 21:09

Chi


I am not sure about the browser compatibility

word-break: break-all; 

Also you can use the <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.

like image 45
rahul Avatar answered Sep 22 '22 21:09

rahul