css 3

Icon

A comprehensive CSS 3 reference guide, tutorial, and blog
Are you a CSS expert? Want to contribute? Contact us

CSS WORD-WRAP

This property specifies whether the current rendered line should break if the content exceeds the boundary of the specified rendering box for an element (this is similar in some ways to the ‘clip’ and ‘overflow’ properties in intent.) This property should only apply if the element has a visual rendering, is an inline element with explicit height/width, is absolutely positioned and/or is a block element.

 

Examples

div { word-wrap: break-word } 

<div style=”word-wrap: break-word”>Here is some content for the div element</div> 

 

Possible Values

Value Description
normal Content will exceed the boundaries of the specified rendering box.
break-word Content will wrap to the next line when necessary, and a word-break will also occur if needed.

 

 

27 Responses

  1. Polina says:

    Hi there!

    Thank you for the info, but somehow I still cannot get the text to wrap even using word-wrap: break-word. My problem is that I have an unbreakable string eg. “../eng/resources/unitednations/unpoarevconf/regcoposstats/RwandanCivilSocietyRecomm.pdf”
    and pushes the div ignoring the width restriction… is there hope for this case?

  2. Mike says:

    Your browser may not support the word-wrap property – it was an IE invention… I had the same problem!

  3. Capt. Anon says:

    Seriously, there has to be a better string for stylesheets to wrap text regardless of browser. This doesn’t even work in IE7!

  4. ahmed says:

    it’s not working with firefox ?

  5. zingo says:

    Rather than force the string to break, shouldn’t an unbreakable string be handled this way?

    div.codeWrapper pre {
    padding: 0;
    margin: 4px 15px;
    }
    div.codeWrapper {
    padding: 15px 0;
    margin: 1px 0;
    }
    html>body div.codeWrapper {
    overflow: auto;
    }

    unbreakable string “../eng/resources/unitednations/unpoarevconf/regcoposstats/RwandanCivilSocietyRecomm.pdf”

  6. zingo says:

    Rather than force the string to break, shouldn’t an unbreakable string be handled this way?

    <style>
    div.codeWrapper pre {
    padding: 0;
    margin: 4px 15px;
    }
    div.codeWrapper {
    padding: 15px 0;
    margin: 1px 0;
    }
    html>body div.codeWrapper {
    overflow: auto;
    }
    </style>

    <div class="codeWrapper">
    <pre>
    unbreakable string "../eng/resources/unitednations/unpoarevconf/regcoposstats/RwandanCivilSocietyRecomm.pdf"

  7. Srinivas says:

    Are there some other parameters which can be given? I got a situation where I need to show the line like this
    “example example exa…” within the given width.

  8. Kelicula says:

    I have a textarea that I want to force a wrap on.
    It will wrap fine if you type in it. BUT I have little javascript buttons that allow you to insert “bold” or “italic” style. A prompt comes up and asks for the text to be Bolded, or italic’d. Well if you enter a really long string, it will break the wrap and go out indefinitely to the right, this messes the page up where you view the message you have written.

    Any Ideas?

  9. Bob Campbell says:

    the WIDTH attribute will work.. I was have wrapping problems.. I set the text in the container to a smaller width and boom the text wrapped.

    div container is 400px wide
    set width:300px;

    works!

    Cheers,
    Bob

  10. BlueBoden says:

    Its important that all browsers add this functionality, we should not need to rely on JavaScript or server-side scripting to do something as simple as this.

  11. Sanjeev says:

    I really want word-wrap to work in mozilla browser.
    But not getting any ideas. So far i have truncated the string or used the program to do this.

  12. bahodir says:

    If you are scripting in PHP, use the WORDWRAP function: http://us2.php.net/wordwrap

  13. BlueBoden says:

    Well that could work i suppose.

    I would simply use a regular expression to block, or break words which are to long.

  14. Mozilla have implemented this property so should be appearing in a firefox near you soon.

    https://developer.mozilla.org/web-tech/2008/08/20/word-wrap-break-word/

  15. Anonymous says:

    Still on this Site i can see the strings not broken.
    Much are just too long for the div’s.
    A workaround for that is needed.

  16. Deepthi says:

    Hi srikanth,

    To wrap a word in mozilla use folloing style class.
    .mainLink {
    white-space:-moz-pre-wrap;
    white-space: normal;
    }

  17. Joe says:

    I tried this, but it doesn’t seem to validate under CSS 3 validation on w3c.org. Is it really a CSS 3 property or just another useless Microsoft invention that doesn’t work?

    I simply fix the problem when forms are submitted. Since we are talking about user input, the form needs to be validated anyway – say with PHP. Do a reg expression to find strings longer than x length, and either break it up into chunks, trim it and set a title attribute so it comes up on hover. Only real difficult scripting would be the links, ensuring you keep the href intact. Using BB script for posting (like many forums do), this should be easy.

    I am sure there are scripts available.

    Joe

  18. Matt says:

    Dudes …

    To wrap a word in IE7 use this:

    .wrapIt {
    width: 125px;
    word-wrap: break-word;
    break-word: break-all;
    }

    seems to me you need layout (a width) and just use both of the above … may be you could even use width: auto …

    Anyway … I guarantee you this works in IE7 ..

    Over and Out

  19. Ihtesham says:

    Matt …the following didnt work on ie 7

    .wrapIt {
    width: 125px;
    word-wrap: break-word;
    break-word: break-all;
    }

Leave a Reply

Pages

What is CSS?

Cascading Style Sheets (CSS) are the modern standard for website presentation. It allows both website developers and users more control over how pages are being displayed on a browser. Style sheets define how elements such as headers, paragraphs, links appear. CSS specifies a priority scheme which determines what style rules apply if/when more than one rule matches against a defined element (the so called cascade).

Follow us on Twitter
css3 on twitter

Please help CSS3.com stay alive and running


Please take a moment to visit our friends
css wrap

Categories

Bad Behavior has blocked 764 access attempts in the last 7 days.

CSS3.com is Digg proof thanks to caching by WP Super Cache!