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?
I have posted a solution which uses JavaScript and a simple Regular Expression to break long word so that it can be wrapped without breaking your website layout.
Wrap long lines using CSS and JavaScript
I would put the post in a div that would have a fixed width setting overflow to scroll (or to hide completely depending on the content).
so like:
But that's just me.
EDIT: As cLFlaVA points out... it is better to use
auto
thenscroll
. I do agree with him.Here's what I do in ASP.NET:
I looked at other CSS based ways of doing this, but didn't find anything that worked cross-browser.
Add the Zero width space (
​
) to the string and it will wrap.Here is a Javacript example:
I didn't want to add libraries to my pages just for word breaking. Then I wrote a simple function which I provide below, hope it helps people.
(It is breaking by 15 characters, and applying "& shy;" between, but you can change it easily in the code)
based on Jon's suggestion the code that I created: