2010-10-13

CSS Text Wrapping Tutorial

Perm url with updates: http://xahlee.org/js/css_text-wrap.html

CSS Text Wrapping Tutorial

Xah Recommends:
Kindle
Amazon Kindle. Read books under the sun. Review

Xah Lee, 2005-11, 2010-10-12

This article explains how to control the text wrapping in HTML/CSS.

CSS2 provides the several ways to control text wrapping. The wrapping attribute is “white-space”. How the line wraps depends on its value. The default value for “p” and “div” tags is “normal”.

white-space:normal

<p style="white-space:normal">...</p>
  • Repeated spaces shrink into a single one.
  • Newline chars are equivalent to space. (no wrap)
  • Text longer than the element's width value are wrapped.

This is the normal behavior of the paragraph tag “<p>”.

Note that white spaces are any of space, tab, and newline characters. You can always use “<br>” tag to force a line break in display.

white-space:pre

<p style="white-space:pre">...</p>

With “white-space: pre”, the behavior is the same as the “<pre>” tag.

  • White spaces are shown as they are. (they do not shrink into one)
  • Any newline chars will force a wrap.
  • The element's “width” attribute has no effect on the line wrapping.

“pre” is best used for showing computer program code, or poetry of short lines.

white-space:pre-wrap

<p style="white-space:pre-wrap; width:60ex">...</p>

With “white-space:pre-wrap”, it's like mix of “pre”, and “normal”.

  • Repeated spaces are shrinked into just one space.
  • Newline char will force a wrap.
  • Very long lines will be automatically wrapped too, by the element's width.

This is very useful for computer code, even better than “pre”, because if you have a lone line comment, it'll be wrapped in the display, but when user copies the code, there will not be extra newline. For example:

# Here is a very long comment. long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long 
print 5

white-space:pre-line

This is just like “pre-wrap”, except that repeated spaces are shrinked into just one space.

white-space:nowrap

<p style="white-space:nowrap; width:60ex">...</p>
  • Repeated spaces shrink into a single one.
  • Newline chars are equivalent to space. (no wrap)
  • The element's “width” attribute has no effect.
  • All text becomes one single very long line, even longer than window width.
  • No wrap whatsoever.
  • The only wrap is by “br”.

Using “width”, “<br>”, “NO-BREAK SPACE”

A common way to control wrap for paragraphs is simply to use the “width”. Like this:

<p style="width:60ex">...</p>

That will limit the width to 60 “ex” unit. A “ex” unit is basically the height of the lowercase letter “x”. (note that this does not guarantee that your paragraph will have 60 characters per line, but usually slightly more. Exactly how many characters per line with “width:60ex” depends on the font used.)

You can always force a line break by using “<br>”.

To prevent a line wrap at positions you do not want to happen, use the unicode character named NO-BREAK SPACE “ ”. Its decimal value is 160, hex is A0. The html entity is “&nbsp;”.