Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

Wrapping Long URLs and Text with CSS

Quick tutorial post today. To wrap long URLs, strings of text, and other content, just apply this carefully crafted chunk of CSS code to any block-level element (e.g., perfect for <pre> tags):

pre {
	white-space: pre;           /* CSS 2.0 */
	white-space: pre-wrap;      /* CSS 2.1 */
	white-space: pre-line;      /* CSS 3.0 */
	white-space: -pre-wrap;     /* Opera 4-6 */
	white-space: -o-pre-wrap;   /* Opera 7 */
	white-space: -moz-pre-wrap; /* Mozilla */
	white-space: -hp-pre-wrap;  /* HP Printers */
	word-wrap: break-word;      /* IE 5+ */
	}

Explanation

By default, the white-space property is set to normal. So you might see something like this when trying to force long URLs and other continuous strings of text to wrap:

[ pre box with long URL extending beyond width ]

To force long, continuous strings of text to wrap within the width of our <pre> content (or other block-level element, such as <div> and <p>), we need a different value for the white-space property. Here are our options:

  • normal – Default value for the white-space property. Sequences of whitespace are collapsed to a single whitespace. <pre> content will wrap at whitespaces according to the width of the element.
  • nowrap – Sequences of whitespace are collapsed to a single whitespace. <pre> content will wrap to the next line ONLY at explicit <br /> elements.
  • pre – All whitespace is preserved. <pre> content will wrap at implicit line breaks. This is the default behavior of the <pre> element.
  • pre-line – Sequences of whitespace are collapsed to a single whitespace. <pre> content will wrap at whitespaces and line breaks according to the width of the element.
  • pre-wrap – All whitespace is preserved. <pre> content will wrap at whitespaces and line breaks according to the width of the element.
  • inherit – Value of white-space inherited from parent element.

In a perfect world, we could simply use white-space:pre-line, like so:

pre {
	white-space: pre-line;
	width: 300px;
	}

Although the white-space property is supported by all major browsers, unfortunately many of them fail to apply the property to long strings of continuous text. Different browsers will wrap long strings, but they require different white-space values in order to work. Fortunately, we can apply the required values for each browser by including multiple white-space declarations in our pre selector. This is exactly what we are doing with the CSS code solution presented at the beginning of this article.

[ pre box with long URL wrapping within width ]

The comments included in the CSS solution explain which declarations are targeting which browsers. Notice that some of the rules are browser-specific (using vendor-specific prefixes), while others declare standard values from different CSS specifications. The funky word-wrap property is a proprietary Microsoft invention that has been included with CSS3. And thanks to the CSS forward compatibility guidelines, it’s perfectly fine to include multiple instances of the same property. In a nutshell:

  • Unrecognized properties are ignored
  • For multiple instances of the same property, only the last rule will be applied

The code solution presented in this article seems to work fine in every browser I have tested, but it doesn’t validate because of the vendor-specific stuff and the crazy Microsoft thing.

For more complete discussion on text wrapping and all the gruesome details, check out Lim Chee Aun’s excellent post on whitespace and generated content (404 link removed 2014/03/24). And, for a complete guide to pimping your site’s <pre> content, check out my article, Perfect Pre Tags.

Here again is the demo page for this CSS technique.

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
The Tao of WordPress: Master the art of WordPress.

33 responses to “Wrapping Long URLs and Text with CSS”

  1. First of all The code is great and thanks a lot for sharing…
    I need to wrap the urls to some width, but the problem is I need to keep them in an anchor tag(that means links should work). do we have any way to do that/

  2. Kuhandoss 2012/04/19 9:58 pm

    Use This…

    #container {
    width:200px;
    height:200px;
    border:solid 1px #333;
    word-wrap:break-word;
    }
    p {
    white-space: pre-line;
    margin:0 5px 0 5px;
    }

    LoremIpsumIsSimplyDummyTextOfThePrintingAndTypesettingIndustry.

  3. To make it work in IE 8-9, I had to put
    word-wrap: break-word; /* IE 5+ */
    on top.

  4. just for an update

    white-space: -moz-pre-wrap; /* Mozilla */

    I am able to use

    white-space: normal; /* Mozilla */

    And by the way this example helped a lot

  5. Cynthia Artish 2012/10/08 12:31 am

    RE: Excessive Hyphens in Published Text

    The post looks correct when I write it. But when it’s published there are excessive hyphens to account for text that exceeds the margin.

    Example:
    Post: …study involves where language is …

    Published: …study involves where lan-
    guage is spoken.

    How can I avoid this?

    Thank you!

    Cynthia

  6. It’s things like this that make me long for the simplicity of tables. Seriously. Remember when a TD tag did this, in all browsers? Christ what have we done.

  7. JohnnyCaraveo 2013/01/25 5:59 pm

    I was having a problem with the element in wordpress not wrapping correctly on the iPhone. Your code block fixed this issue and now my website looks great on the iPhone. Thank you!

  8. Amazing dude :)

    Good one keep it up!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
.htaccess made easy: Improve site performance and security.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.