treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Prevent Long URL’s From Breaking Out of Container

Last updated on:

Or any long bit of text, really.

.comment-text {
   word-wrap: break-word;
}

A more robust browser support, you'll need more (via):

-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;

The above works in Internet Explorer 8+, Firefox 6+, iOS 4.2, Safari 5.1+ and Chrome 13+.

View Comments

Comments

  1. Apparently, overflow-wrap is the new word-wrap as its been removed from the CSS3 spec. I just read about this only a couple of days ago, along with some other interesting proposed typographic properties:

    http://www.impressivewebs.com/new-css3-text-wrap/

    Worth noting: although word-wrap has been removed from spec there is no current browser support for the replacement property!

  2. But before you go rushing into changing all that word-wrap goodness you just added – from the spec: “For legacy reasons, UAs may also accept ‘word-wrap’ as an alternate name for the ‘overflow-wrap’ property.”

  3. agentsuperdave
    Permalink to comment#

    the surrounding element must have a specified width or your long url will still break out of the box.

  4. Zach Case
    Permalink to comment#

    You solved a problem I’ve been having. Awesome!

  5. Quentin
    Permalink to comment#

    Thanks agentsuperdave, this tip was extremely useful. For a table, that means you should use “table-layout: fixed” and specify widths for your tds.

    • Permalink to comment#

      Quentin, I love you man, you saved my ass! ;)

      Btw. I didn’t declare width, just “table {table-layout: fixed; width: 100%}” and “table a {word-wrap: break-word}”.

    • Permalink to comment#

      I didn’t declare “td” width, just…

    • Quentin
      Permalink to comment#

      Yeah, it works too. In my case I needed a different width for each column.

  6. Great thanks for that, I had been looking for a solution for word-break in FF for like an hour. Cheers

  7. Your robust solution fixed my issue, my URL did not want to break while inside a table styled to be responsive (display:block). Cheers!

  8. kongu
    Permalink to comment#

    fix example from

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

    to

     /* Non standard for webkit */
     word-break: break-word;
    
  9. Dave Merrill
    Permalink to comment#

    If these options dont’ give you enough control, see this question on SO, especially two of my answers (;-), http://stackoverflow.com/a/6298738/736006 and http://stackoverflow.com/a/6508168/736006.

  10. Dzumla
    Permalink to comment#

    Thanks man!

  11. Priyank Shrivastava

    I tried using this :

    // Given on the top post

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

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

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

    but was not working.

    Until i added

    float:left and a width.

    Now it is working perfectly fine.

  12. I tried all these solutions and it works perfectly in Safari, Firefox and IE but it does not work in the newest Chrome.

    The one I ended up using now is this from the article:

    -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;
    

    The article says it is supposed to work in Chrome. Does anyone know what the issue might be?

Leave a Comment

Use markdown or basic HTML and be nice.