154

Ever since switching from TABLE-layout to DIV-layout, one common problem remains:

PROBLEM: you fill your DIV with dynamic text and inevitably there is a super-long word that extends over the edge of your div column and makes your site look unprofessional.

RETRO-WHINING: This never happened with table layouts. A table cell will always nicely expand to the width of the longest word.

SEVERITY: I see this problem on even the most major sites, especially on German sites where even common words such as "speed limit" are very long ("Geschwindigkeitsbegrenzung").

Does anyone have a workable solution to this?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 4
    You must have forgotten those super-stretched and effectively broken table layouts. I'll take overflow:hidden any day over uncontrollably stretching cells. – Kornel Nov 26 '08 at 10:15
  • 1
    A table cell will always nicely???????? expand to the width of the longest word – Ian G Nov 26 '08 at 10:44
  • Yes, usually quite nicely, as in the case of the below graphic from Stackoverflow's related column, if that were a TABLE instead of a DIV, it would just be a little wider and still look nice--very pragmatic. That would be a nice feature to be able to turn on in DIV, something like word-wrap:expand. – Edward Tanguay Nov 26 '08 at 10:59
  • 1
    I know a lot of people (and I'd probably count myself amongst them) who would say that is a much worse behaviour. Page and element width is usually something that has a lot of time spent on it. If you could have random words making widths uncontrollable, you've failed with your design. – Oli Nov 26 '08 at 11:06
  • 13
    I've always felt that the table behavior was more in line with HTML's original philosophy of flexibility. The DIV/CSS rigid column width philosophy seems to come from the magazine designers who can't deal with their columns being sometimes wider sometimes narrower. – Edward Tanguay Nov 26 '08 at 11:13
  • 1
    A good design should be consistent; if page content is able to change the dimensions of the UI it would violate the design. Seriously, where would you draw the line with a stretchy layout? Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch? – Doctor Jones Nov 26 '08 at 11:45
  • I see what you mean. ;-) – Edward Tanguay Nov 26 '08 at 11:56
  • Yeah this has little to do with being uber-strict to the design (from an arty-farty point of view), and more keeping things constant and usable. With tables (and this comment box) showing all the content has meant that other comments are now pretty unreadable. – Oli Nov 26 '08 at 13:50
  • (If you want to read this comments, just copy and paste them into a text editor.) – Edward Tanguay Nov 26 '08 at 14:01
  • How can I find the source for this box, to see if it is a DIV or if it really is a TABLE? It is not in view source. Don't I have access to the current DOM text even if injected by AJAX? – Edward Tanguay Nov 26 '08 at 14:02
  • Let this be a lesson that embedding externally-hosted images into SO questions is A Bad Idea™. – Lightness Races in Orbit Jun 28 '11 at 16:52
  • @Edward: Look at how newspapers have been doing it for hundreds of years. – Lightness Races in Orbit Jun 28 '11 at 16:52
  • An "up-to-date" answer (2014) http://stackoverflow.com/questions/802175/truncating-long-strings-with-css-feasible-yet/22811590#22811590 – Adriano Nov 20 '14 at 16:50
  • Remind me never to play Scrabble against a German. That thing's gotta be worth 300 points! The 2016 solution: In your CSS, add: `word-wrap: break-word;` There may be a need for browser prefixes depending on what you're looking to support. Keep in mind also that the element or a parent (whatever element is determining how wide you want to allow for) needs to have a defined width or the word-wrap will not take effect. Hope that helps. – Tom Walker Jan 23 '16 at 02:19

26 Answers26

142

Soft hyphen

You can tell browsers where to split long words by inserting soft hyphen (­):

averyvery­longword

may be rendered as

averyverylongword

or

averyvery-
longword

A nice regular expression can ensure you won't be inserting them unless neccessary:

/([^\s-]{5})([^\s-]{5})/ → $1­$2

Browsers and search engines are smart enough to ignore this character when searching text, and Chrome and Firefox (haven't tested others) ignore it when copying text to clipboard.

<wbr> element

Another option is to inject <wbr>, a former IE-ism, which is now in HTML5:

averyvery<wbr>longword

Breaks with no hyphen:

averyvery
longword

You can achieve the same with zero-width space character &#8203; (or &#x200B).


FYI there's also CSS hyphens: auto supported by latest IE, Firefox and Safari (but currently not Chrome):

div.breaking {
  hyphens: auto;
}

However that hyphenation is based on a hyphenation dictionary and it's not guaranteed to break long words. It can make justified text prettier though.

Retro-whining solution

<table> for layout is bad, but display:table on other elements is fine. It will be as quirky (and stretchy) as old-school tables:

div.breaking {
   display: table-cell;
}

overflow and white-space: pre-wrap answers below are good too.

Community
  • 1
  • 1
Kornel
  • 97,764
  • 37
  • 219
  • 309
  • 7
    Cool! According to wikipedia, you can get a zero-width space with ​ -- since you brought it up, do you know a less-ugly escape code for it? I'll memorize 8203 if I have to, but... – ojrac Mar 31 '09 at 21:11
  • 1
    @ojrac — That depends on whether you think ​ is "less ugly" or not. :-) AFAIK, there's no "word entity" for zero-width space. – Ben Blank Mar 31 '09 at 23:18
  • Well, #x200B is easier to remember. Good enough. – ojrac Apr 01 '09 at 22:50
  • You can use something like TextExpander (http://www.smileonmymac.com/TextExpander/) to give your names to any string or character. – Kornel Apr 02 '09 at 09:04
  • 1
    That's nice, but is not a solution to the initial problem. – Albus Dumbledore Sep 02 '10 at 09:16
  • @Smamatti: I haven't seen any browser that doesn't support it since Firefox 2, which was the very last browser to implement this *15-year old standard*. – Kornel Oct 26 '11 at 12:48
  • @porneL Sorry. I guess I haven't tested it enough. There seems to be an issue with the rest of my CSS. `­` itself is indeed working fine on all major browser. - http://jsfiddle.net/Bn3th/ – Smamatti Oct 26 '11 at 15:25
  • 4
    Just FYI, you can also use . See http://www.quirksmode.org/oddsandends/wbr.html. – HaxElit Mar 15 '12 at 14:27
  • The point here, I believe, though, is to not have to manually enter extra characters. This is not a good solution since text can be arbitrary, font size, OS and browser differences in font rendering, etc. See my post below for a CSS solution. – Neil Monroe Sep 19 '12 at 16:26
  • I modified the above regex to `/([^\s-&<>]{5})([^\s-&<>]{5})/` to make sure it doesn't break HTML tags and entities (assuming you can have a string potentially with HTML tags) – AsGoodAsItGets Mar 16 '16 at 13:13
41

Two fixes:

  1. overflow:scroll -- this makes sure your content can be seen at the cost of design (scrollbars are ugly)
  2. overflow:hidden -- just cuts off any overflow. It means people can't read the content though.

If (in the SO example) you want to stop it overlapping the padding, you'd probably have to create another div, inside the padding, to hold your content.

Edit: As the other answers state, there are a variety of methods for truncating the words, be that working out the render width on the client side (never attempt to do this server-side as it will never work reliably, cross platform) through JS and inserting break-characters, or using non-standard and/or wildly incompatible CSS tags (word-wrap: break-word doesn't appear to work in Firefox).

You will always need an overflow descriptor though. If your div contains another too-wide block-type piece of content (image, table, etc), you'll need overflow to make it not destroy the layout/design.

So by all means use another method (or a combination of them) but remember to add overflow too so you cover all browsers.

Edit 2 (to address your comment below):

Start off using the CSS overflow property isn't perfect but it stops designs breaking. Apply overflow:hidden first. Remember that overflow might not break on padding so either nest divs or use a border (whatever works best for you).

This will hide overflowing content and therefore you might lose meaning. You could use a scrollbar (using overflow:auto or overflow:scroll instead of overflow:hidden) but depending on the dimensions of the div, and your design, this might not look or work great.

To fix it, we can use JS to pull things back and do some form of automated truncation. I was half-way through writing out some pseudo code for you but it gets seriously complicated about half-way through. If you need to show as much as possible, give hyphenator a look in (as mentioned below).

Just be aware that this comes at the cost of user's CPUs. It could result in pages taking a long time to load and/or resize.

Community
  • 1
  • 1
Oli
  • 235,628
  • 64
  • 220
  • 299
  • 1
    with text-overflow:ellipsis; text can be cut of nicely. – Kornel Nov 26 '08 at 10:21
  • 1
    text-overflow:ellipsis is IE-only (and by extension, non-standard). – Oli Nov 26 '08 at 10:28
  • I'd always go for `overflow: scroll;` in case the content contains useful information. And then the next goal is to try to create such CSS that the scroll bars will not appear. And in case they do, you always have the scroll bars as backup. – Yeti Dec 08 '12 at 11:22
  • regarding using `text-overflow: ellipsis` http://stackoverflow.com/a/22811590/759452 – Adriano Jun 26 '14 at 09:17
33

This is a complex issue, as we know, and not supported in any common way between browsers. Most browsers don't support this feature natively at all.

In some work done with HTML emails, where user content was being used, but script is not available (and even CSS is not supported very well) the following bit of CSS in a wrapper around your unspaced content block should at least help somewhat:

.word-break {
  /* The following styles prevent unbroken strings from breaking the layout */
  width: 300px; /* set to whatever width you need */
  overflow: auto;
  white-space: -moz-pre-wrap; /* Mozilla */
  white-space: -hp-pre-wrap; /* HP printers */
  white-space: -o-pre-wrap; /* Opera 7 */
  white-space: -pre-wrap; /* Opera 4-6 */
  white-space: pre-wrap; /* CSS 2.1 */
  white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
  word-wrap: break-word; /* IE */
  -moz-binding: url('xbl.xml#wordwrap'); /* Firefox (using XBL) */
}

In the case of Mozilla-based browsers, the XBL file mentioned above contains:

<?xml version="1.0" encoding="utf-8"?>
<bindings xmlns="http://www.mozilla.org/xbl" 
          xmlns:html="http://www.w3.org/1999/xhtml">
  <!--
  More information on XBL:
  http://developer.mozilla.org/en/docs/XBL:XBL_1.0_Reference

  Example of implementing the CSS 'word-wrap' feature:
  http://blog.stchur.com/2007/02/22/emulating-css-word-wrap-for-mozillafirefox/
  -->
  <binding id="wordwrap" applyauthorstyles="false">
    <implementation>
      <constructor>
        //<![CDATA[
        var elem = this;

        doWrap();
        elem.addEventListener('overflow', doWrap, false);

        function doWrap() {
          var walker = document.createTreeWalker(elem, NodeFilter.SHOW_TEXT, null, false);
          while (walker.nextNode()) {
            var node = walker.currentNode;
            node.nodeValue = node.nodeValue.split('').join(String.fromCharCode('8203'));
          }
        }
        //]]>
      </constructor>
    </implementation>
  </binding>
</bindings>

Unfortunately, Opera 8+ don't seem to like any of the above solutions, so JavaScript will have to be the solution for those browsers (like Mozilla/Firefox.) Another cross-browser solution (JavaScript) that includes the later editions of Opera would be to use Hedger Wang's script found here: http://www.hedgerwow.com/360/dhtml/css-word-break.html

Other useful links/thoughts:

Incoherent Babble » Blog Archive » Emulating CSS word-wrap for Mozilla/Firefox
http://blog.stchur.com/2007/02/22/emulating-css-word-wrap-for-mozillafirefox/

[OU] No word wrap in Opera, displays fine in IE
http://list.opera.com/pipermail/opera-users/2004-November/024467.html
http://list.opera.com/pipermail/opera-users/2004-November/024468.html

Neil Monroe
  • 1,201
  • 1
  • 15
  • 20
  • What if what I need is width:100%? This means 100% of the outside container. The purpose of this is to avoid horizontal scrolls bars from appearing on the page and messing up the rest of the layout. – pilavdzice Mar 22 '12 at 15:56
  • 1
    The newer versions of Firefox now support the `word-wrap: break-word;` CSS property, so if you don't need support in Firefox for earlier versions, then you can eliminate the XBL. – Neil Monroe Sep 19 '12 at 16:30
27

CSS Cross Browser Word Wrap

.word_wrap
{
    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+ */
}
Remo
  • 291
  • 4
  • 6
18

Use the style word-break:break-all;. I know it works on tables.

Zanon
  • 29,231
  • 20
  • 113
  • 126
sanimalp
  • 799
  • 5
  • 12
13

Do you mean that, in browsers that support it, word-wrap: break-word does not work ?

If included in the body definition of the stylesheet, it should works throughout the entire document.

If overflow is not a good solution, only a custom javascript could artificially break up long word.

Note: there is also this <wbr> Word Break tag. This gives the browser a spot where it can split the line up. Unfortunately, the <wbr> tag doesn't work in all browsers, only Firefox and Internet Explorer (and Opera with a CSS trick).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
9

Just checked IE 7, Firefox 3.6.8 Mac, Firefox 3.6.8 Windows, and Safari:

word-wrap: break-word;

works for long links inside of a div with a set width and no overflow declared in the css:

#consumeralerts_leftcol{
    float:left;
    width: 250px;
    margin-bottom:10px;
    word-wrap: break-word;
}

I don't see any incompatibility issues

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Zac Imboden
  • 751
  • 8
  • 11
6

After much fighting, this is what worked for me:

.pre {
    font-weight: 500;
    font-family: Courier New, monospace;
    white-space: pre-wrap;
    word-wrap: break-word;
    word-break: break-all;
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    hyphens: auto;
}

Works in the latest versions of Chrome, Firefox and Opera.

Note that I excluded many of the white-space properties the others suggested -- that actually broke the pre indentation for me.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
6

I just found out about hyphenator from this question. That might solve the problem.

Community
  • 1
  • 1
dylanfm
  • 6,292
  • 5
  • 28
  • 29
  • It made howWouldYourSiteDealWithCommentsLikeThisOne look nice and manageable. Very cool. – ojrac Mar 31 '09 at 21:14
5

For me on a div without fixed size and with dynamic content it worked using:

display:table;
word-break:break-all;
Jacob
  • 3,580
  • 22
  • 82
  • 146
4

The solution I usually use for this problem is to set 2 different css rules for IE and other browsers:

word-wrap: break-word;

woks perfect in IE, but word-wrap is not a standard CSS property. It's a Microsoft specific property and doesn't work in Firefox.

For Firefox, the best thing to do using only CSS is to set the rule

overflow: hidden;

for the element that contains the text you want to wrap. It doesn't wrap the text, but hide the part of text that go over the limit of the container. It can be a nice solution if is not essential for you to display all the text (i.e. if the text is inside an <a> tag)

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
alexmeia
  • 5,241
  • 4
  • 24
  • 24
4

Re the regex in this comment, it's good, but it adds the shy hyphen only between groups of 5 non-whitespace-or-hyphen chars. That allows the last group be much longer than intended, since there's no matching group after it.

For instance, this:

'abcde12345678901234'.replace(/([^\s-]{5})([^\s-]{5})/g, '$1&shy;$2')

...results in this:

abcde&shy;12345678901234

Here's a version using positive lookahead to avoid that problem:

.replace(/([^\s-]{5})(?=[^\s-])/g, '$1&shy;')

...with this result:

abcde&shy;12345&shy;67890&shy;1234
Community
  • 1
  • 1
enigment
  • 3,316
  • 7
  • 30
  • 35
4

Update: Handling this in CSS is wonderfully simple and low overhead, but you have no control over where breaks occur when they do. That's fine if you don't care, or your data has long alphanumeric runs without any natural breaks. We had lots of long file paths, URLs, and phone numbers, all of which have places it's significantly better to break at than others.

Our solution was to first use a regex replacement to put a zero-width space (&#8203;) after every 15 (say) characters that aren't whitespace or one of the special characters where we'd prefer breaks. We then do another replacement to put a zero-width space after those special characters.

Zero-width spaces are nice, because they aren't ever visible on screen; shy hyphens were confusing when they showed, because the data has significant hyphens. Zero-width spaces also aren't included when you copy text out of the browser.

The special break characters we're currently using are period, forward slash, backslash, comma, underscore, @, |, and hyphen. You wouldn't think you'd need do anything to encourage breaking after hyphens, but Firefox (3.6 and 4 at least) doesn't break by itself at hyphens surrounded by numbers (like phone numbers).

We also wanted to control the number of characters between artificial breaks, based on the layout space available. That meant that the regex to match long non-breaking runs needed to be dynamic. This gets called a lot, and we didn't want to be creating the same identical regexes over and over for performance reasons, so we used a simple regex cache, keyed by the regex expression and its flags.

Here's the code; you'd probably namespace the functions in a utility package:

makeWrappable = function(str, position)
{
    if (!str)
        return '';
    position = position || 15; // default to breaking after 15 chars
    // matches every requested number of chars that's not whitespace or one of the special chars defined below
    var longRunsRegex = cachedRegex('([^\\s\\.\/\\,_@\\|-]{' + position + '})(?=[^\\s\\.\/\\,_@\\|-])', 'g');
    return str
                .replace(longRunsRegex, '$1&#8203;') // put a zero-width space every requested number of chars that's not whitespace or a special char
                .replace(makeWrappable.SPECIAL_CHARS_REGEX, '$1&#8203;'); // and one after special chars we want to allow breaking after
};
makeWrappable.SPECIAL_CHARS_REGEX = /([\.\/\\,_@\|-])/g; // period, forward slash, backslash, comma, underscore, @, |, hyphen


cachedRegex = function(reString, reFlags)
{
    var key = reString + (reFlags ? ':::' + reFlags : '');
    if (!cachedRegex.cache[key])
        cachedRegex.cache[key] = new RegExp(reString, reFlags);
    return cachedRegex.cache[key];
};
cachedRegex.cache = {};

Test like this:

makeWrappable('12345678901234567890 12345678901234567890 1234567890/1234567890')

Update 2: It appears that zero-width spaces in fact are included in copied text in at least some circumstances, you just can't see them. Obviously, encouraging people to copy text with hidden characters in it is an invitation to have data like that entered into other programs or systems, even your own, where it may cause problems. For instance, if it ends up in a database, searches against it may fail, and search strings like this are likely to fail too. Using arrow keys to move through data like this requires (rightly) an extra keypress to move across the character you can't see, somewhat bizarre for users if they notice.

In a closed system, you can filter that character out on input to protect yourself, but that doesn't help other programs and systems.

All told, this technique works well, but I'm not certain what the best choice of break-causing character would be.

Update 3: Having this character end up in data is no longer a theoretical possibility, it's an observed problem. Users submit data copied off the screen, it gets saved in the db, searches break, things sort weirdly etc..

We did two things:

  1. Wrote a utility to remove them from all columns of all tables in all datasources for this app.
  2. Added filtering to remove it to our standard string input processor, so it's gone by the time any code sees it.

This works well, as does the technique itself, but it's a cautionary tale.

Update 4: We're using this in a context where the data fed to this may be HTML escaped. Under the right circumstances, it can insert zero-width spaces in the middle of HTML entities, with funky results.

Fix was to add ampersand to the list of characters we don't break on, like this:

var longRunsRegex = cachedRegex('([^&\\s\\.\/\\,_@\\|-]{' + position + '})(?=[^&\\s\\.\/\\,_@\\|-])', 'g');
enigment
  • 3,316
  • 7
  • 30
  • 35
  • I used this, but I only needed to run this once for a long domain name so I just shortened it to: `if(domainName.length > 15) domainName.replace(/([^\\s]{5})(?=[^\\s])/g, '$1​');` – Chris Barr Dec 17 '12 at 15:55
3

Need to set "table-layout: fixed” for word-wrap to work

Aleks
  • 31
  • 1
2

For compatibility with IE 8+ use:

-ms-word-break: break-all;
     word-break: break-all;

     /* Non standard for webkit */
     word-break: break-word;

-webkit-hyphens: auto;
   -moz-hyphens: auto;
        hyphens: auto;

See it here http://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/

All I had to do was apply this to the style of the div container with a set width.

DoctorFox
  • 173
  • 1
  • 6
2
p {
    overflow-wrap: break-word;
}


@-moz-document url-prefix() { 
    p {
        white-space: -moz-pre-wrap;
        word-wrap: break-word;
    }
}
microbians
  • 455
  • 5
  • 11
2

HYPHENATOR is the right answer (given above). The real problem behind your question is that web browsers are still (in 2008) extremely primitive that they do not have a hyphenation-feature. Look, we are still on the early beginnings of computer usage - we have to be patient. As long as designers rule the web world we will have a hard time waiting for some real useful new features.

UPDATE: As of December, 2011, we now have another option, with the emerging support of these tags in FF and Safari:

p {
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    hyphens: auto;
}

I've done some basic testing and it seems to work on a recent version of Mobile Safari and Safari 5.1.1.

Compatibility table: https://developer.mozilla.org/en/CSS/hyphens#AutoCompatibilityTable

Community
  • 1
  • 1
Snaky Love
  • 131
  • 1
  • 4
2

Use this

word-wrap: break-word;
overflow-wrap: break-word;
word-break: break-all;
Jay Patel
  • 5,783
  • 1
  • 17
  • 20
1

I had to do the following because, if the properties were not declared in the correct order, it would randomly break words at the wrong place and without adding a hyphen.

    -moz-white-space: pre-wrap;
white-space: pre-wrap;        
    hyphens: auto;
    -ms-word-break: break-all;
    -ms-word-wrap: break-all;
    -webkit-word-break: break-word;
    -webkit-word-wrap: break-word;
word-break: break-word;
word-wrap: break-word;
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    -ms-hyphens: auto;
hyphens: auto;

Originally posted by Enigmo: https://stackoverflow.com/a/14191114

Community
  • 1
  • 1
1

Yeah, if it is possible, setting an absolute width and setting overflow : auto works well.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
1

If you have this -

  <style type="text/css">
      .cell {
            float: left;
            width: 100px;
            border: 1px solid;
            line-height: 1em;
      }
  </style>

  <div class="cell">TopLeft</div>
  <div class="cell">TopMiddlePlusSomeOtherTextWhichMakesItToLong</div>
  <div class="cell">TopRight</div>
  <br/>
  <div class="cell">BottomLeft</div>
  <div class="cell">BottomMiddle</div>
  <div class="cell">bottomRight</div>

just switch to a vertical format with containing divs and use min-width in your CSS instead of width -

  <style type="text/css">
      .column {
            float: left;
            min-width: 100px;
      }
      .cell2 {
            border: 1px solid;
            line-height: 1em;
      }
  </style>

  <div class="column">
      <div class="cell2">TopLeft</div>
      <div class="cell2">BottomLeft</div>
  </div>
  <div class="column">
      <div class="cell2">TopMiddlePlusSomeOtherTextWhichMakesItToLong</div>
      <div class="cell2">BottomMiddle</div>
  </div>
  <div class="column">
      <div class="cell2">TopRight</div>
      <div class="cell2">bottomRight</div>
  </div>
  <br/>

Of course, if you are displaying genuine tabular data it is ok to use a real table as it is semantically correct and will inform people using screen readers that is supposed to be in a table. It is using them for general layout or image-slicing that people will lynch you for.

Dan Brown
  • 111
  • 1
  • 1
  • 6
0

after all the word wraps and breaks, preserve your overflow and see if this solves your issue. simply change your div's display to: display: inline;

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Olofu Mark
  • 1,040
  • 9
  • 6
0

Add this to css of your div: word-wrap: break-word;

Pragmateek
  • 13,174
  • 9
  • 74
  • 108
Kishan Subhash
  • 153
  • 2
  • 9
0

"word-wrap: break-word" works in Firefox 3.5 http://hacks.mozilla.org/2009/06/word-wrap/

mb21
  • 34,845
  • 8
  • 116
  • 142
-1

A simple function (requires underscore.js) -- based off of @porneL answer

    String.prototype.shyBreakString = function(maxLength) {
        var shystring = [];
        _.each(this.split(' '), function(word){
            shystring.push(_.chop(word, maxLength).join('&shy;'));
        });
        return shystring.join(' ');
    };
hharnisc
  • 937
  • 7
  • 11
-1

I've written a function that works great where it inserts &shy; x letters into the word for good line breaking. All answers here did not support all browsers and devices but this works well using PHP:

/**
 * Add line-break to text x characters in
 * @param  string  $text          
 * @param  integer $characters_in 
 * @return string                 
 */
function line_break_text($text, $characters_in = 10) {

    $split = explode(' ', $text);

    if ( ! empty($split)) {

        foreach ($split as $key => $var) {

            if ( strlen($var) > $characters_in ) {

                $split[$key] = substr_replace($var, '&shy;', $characters_in, 0);

            }

        }

    }

    return implode(' ', $split);

}
Jack
  • 3,271
  • 11
  • 48
  • 57