Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

Wrapping Long URLs and Text Content with CSS

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+ */
	}

See demonstration

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. And, for a complete guide to pimping your site’s <pre> content, check out my article, Perfect Pre Tags.

Here again is the demonstration page for this CSS technique.

Related articles

About this article

This is article #753, posted by Jeff Starr on Tuesday, June 01, 2010 @ 08:21am. Categorized as Presentation, and tagged with code, css, tips, tricks. Updated on acid. Visited 126628 times. 17 Responses »

BookmarkTrackbackCommentSubscribeExplore

« htaccess Redirect to Maintenance Page • Up • HTML5 Table Template »


17 Responses

1 • June 1, 2010 at 9:41 am — Johnny says:

So simple yet so effective.

2 • June 1, 2010 at 9:58 am — Jessi Hance says:

This is amazing. I remember searching for a solution to long URLs breaking layout, and only finding crazy complicated scripted solutions. I had no clue about white-space: pre. I ended up doing some overflow: hidden thing as the least of evils. This is perfect! Thank you so much!

3 • June 1, 2010 at 11:05 am — Jeff Starr says:

Awesome to hear! Hopefully it will serve as a simple and effective reference. Thank you both for the comments. :)

4 • June 1, 2010 at 12:23 pm — reADactor says:

nice tip, but i don’t get it working in Opera 10.

regards

5 • June 1, 2010 at 8:52 pm — Jeff Starr says:

reADactor, it looks like Opera will soon support white-space:pre-line.

6 • June 2, 2010 at 12:34 am — Jasper van der Kamp says:

word-wrap is now part of CSS3 and also supported by Chrome, Firefox, Opera and Safari.

7 • June 2, 2010 at 12:37 am — Jeff Starr says:

Jasper, yes that is mentioned in the article. Opera doesn’t seem to pick up on it though, as seen with the demonstration. Maybe there is an alternate value that Opera will recognize?

8 • June 2, 2010 at 3:11 pm — paul says:

I’ve used break-word before for long URLs, but I dind’t know the others.
thanks!

9 • June 5, 2010 at 6:37 pm — Michael Hu says:

So crazy, so beautiful.
I like it very much. Thanks!!

10 • June 6, 2010 at 2:52 pm — Josh says:

Very cool and useful. I also want to reiterate my vote for a tutorial on how you got such well done drop-caps on your site.

11 • June 7, 2010 at 5:29 pm — Jeff Starr says:

Vote counted! Working on generalizing the drop-cap technique to accommodate more implementations. As-is, the code is pretty specific to this particular design and uses quite a few browser-specific rules. Once I simplify it, I’ll post it. Thanks for the idea!

12 • June 11, 2010 at 11:19 pm — 海宁美食 says:

Thanks!
It solved my URL-wrapping problem.

13 • July 27, 2010 at 2:02 am — vicky says:

Code doesn’t work in FF 3.0 . Although it works perfect in 3.5 but text doesn;t wrap to next line in 3. Any workaround for FF 3.0 ? ~ Vicky

14 • August 10, 2010 at 1:33 am — Kanuel says:

Thank you very much!!!

Drop a comment


Trackbacks / Pingbacks

  1. Wrapping Long URLs and Text Content with CSS | Lively Web Tuts
  2. Wrapping Long URLs and Text Co… | BrettSinclair.net
  3. 50 New Useful CSS Techniques, Tools and Tutorials - Smashing Magazine

9rules Network

Set CSS to lite theme
Set CSS to dark theme
Attention: Do NOT follow this link!