Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap long lines without spaces in HTML?

If a user types in a long line without any spaces or white space, it will break formating by going wider than the current element. Something like:

HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA.............................................................................................................................................

I've tried just using wordwrap() in PHP, but the problem with that is if there is a link or some other valid HTML, it breaks.

There seems to be a few options in CSS, but none of them work in all browsers. See word-wrap in IE.

How do you solve this problem?

like image 231
Chris Bartow Avatar asked Dec 12 '08 17:12

Chris Bartow


People also ask

How do you wrap text without spaces in HTML?

Replace spaces with   Then use the letter-spacing css attribute to bring the spaces down. I know, it's a hack... but if NOTHING else works, it should wrap without problem.

How do you prevent line breaks?

Use white-space: nowrap; or give that link more space by setting li 's width to greater values. I prevented line break in li items using display: inline; . Maybe this will also help others with similar problems. Its important to be careful with display: inline as it can have side effects.

How do you stop a line break in HTML?

There are several ways to prevent line breaks in content. Using   is one way, and works fine between words, but using it between an empty element and some text does not have a well-defined effect. The same would apply to the more logical and more accessible approach where you use an image for an icon.


2 Answers

in CSS3:

word-wrap:break-word 
like image 185
Marcin Avatar answered Sep 20 '22 09:09

Marcin


I was trying to solve the same problem and I found de solution here:

http://perishablepress.com/press/2010/06/01/wrapping-content/

Solution: adding to the container the following CSS properties

div {     white-space: pre;           /* CSS 2.0 */     white-space: pre-wrap;      /* CSS 2.1 */     white-space: pre-line;      /* CSS 3.0 */     white-space: -pre-wrap;     /* Opera 4-6 */     white-space: -o-pre-wrap;   /* Opera 7 */     white-space: -moz-pre-wrap; /* Mozilla */     white-space: -hp-pre-wrap;  /* HP Printers */     word-wrap: break-word;      /* IE 5+ */ } 

The idea is using them all so you get better cross-browser compatibility

Hope this helps

like image 25
Fernando Costas - Mercadosweb Avatar answered Sep 21 '22 09:09

Fernando Costas - Mercadosweb