Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

word wrap in css / js

Tags:

javascript

css

I'm looking for a cross-browser way of wrapping long portions of text that have no breaking spaces (e.g. long URLs) inside of divs with pre-determined widths.

Here are some solutions I've found around the web and why they don't work for me:

  • overflow : hidden / auto / scroll - I need the entire text to be visible without scrolling. The div can grow vertically, but not horizontally.
  • Injecting ­ into the string via js / server-side - ­ is supported by FF3 now, but copying and pasting a URL with a ­ in the middle will not work in Safari. Also, to my knowledge, there isn't a clean method of measuring text width to find out the best string offsets to add these characters to (there's one hacky way, see next item). Another problem is that zooming in Firefox and Opera can easily break this.
  • dumping text into a hidden element and measuring offsetWidth - related to the item above, it requires adding extra characters into the string. Also, measuring the amount of breaks required in a long body of text could easily require thousands of expensive DOM insertions (one for every substring length), which could effectively freeze the site.
  • using a monospace font - again, zooming can mess up width calculations, and the text can't be styled freely.

Things that look promising but are not quite there:

  • word-wrap : break-word - it's now part of CSS3 working draft, but it's not supported by either Firefox, Opera or Safari yet. This would be the ideal solution if it worked across all browsers today :(
  • injecting <wbr> tags into the string via js/ server-side - copying and pasting URLs works in all browsers, but I still don't have a good way of measuring where to put the breaks. Also, this tag invalidates HTML.
  • adding breaks after every character - it's better than thousands of DOM insertions, but still not ideal (adding DOM elements to a document eats memory and slows downs selector queries, among other things).

Does anyone have more ideas on how to tackle this problem?

like image 834
Leo Avatar asked Nov 27 '08 04:11

Leo


People also ask

How do I wrap text in a box CSS?

You have to set 'display:inline-block' and 'height:auto' to wrap the content within the border. Show activity on this post. Two ways are there. No need to mention height in this it will be auto by default.

How do I wrap text in a div?

You can't wrap that text as it's unbroken without any spaces. You need a JavaScript or server side solution which splits the string after a few characters. You need to add this property in CSS.


1 Answers

Css yo!

.wordwrap {     white-space: pre-wrap; /* css-3 */     white-space: -moz-pre-wrap; /* Mozilla, since 1999 */     white-space: -pre-wrap; /* Opera 4-6 */     white-space: -o-pre-wrap; /* Opera 7 */     word-wrap: break-word; /* Internet Explorer 5.5+ */  } 

Also I just found this article http://www.impressivewebs.com/word-wrap-css3/

So you should use

.wordwrap {       word-wrap: break-word; }    .no_wordwrap {       word-wrap: normal; }   
like image 152
superlogical Avatar answered Sep 19 '22 15:09

superlogical