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?
in CSS3:
I know that this is a really old problem and since I had the same problem I searched for a easy solution. As mentioned in the first post I decided to use the php-function wordwrap.
See the following code example for information ;-)
Hope this helps.
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
The idea is using them all so you get better cross-browser compatibility
Hope this helps
I'm surprised that nobody has mentioned one of my favorite solutions to this problem, the
<wbr>
(optional line-break) tag. It's fairly well-supported in browsers and essentially tells the browser that it can insert a line-break if it's necessary. There's also the related zero-width space character,​
with the same meaning.For the use case mentioned, displaying user comments on a web page, I would assume that there is already some output formatting to prevent injection attacks, etc. So it's simple to add these
<wbr>
tags everyN
characters in words that are too long, or in links.This is especially useful when you need control over the format of the output, which CSS doesn't always let you do.
There is no "perfect" HTML/CSS solution.
The solutions either hide the overflow (ie scrolling or just hidden) or expand to fit. There is no magic.
Q: How can you fit a 100cm wide object into a space only 99cm wide?
A: You can't.
You can read break-word
EDIT
Please check out this solution How to apply a line wrap/continuation style and code formatting with css
or
How to prevent long words from breaking my div?
I like to use the
overflow: auto
CSS property/value pairing. This will render the parent object the way you'd expect it to appear. If the text within the parent is too wide, scrollbars appear within the object itself. This will keep the structure the way you want it to look and provide the viewer with the ability to scroll over to see more.Edit: the nice thing about
overflow: auto
compared tooverflow: scroll
is that withauto
, the scrollbars will only appear when overflowing content exists. Withscroll
, the scrollbars are always visible.