7.2. Special In-Line Text Elements

Usually, you can leave the formatting of text lines up to the user agent, allowing it to flow text freely according to the width of its window. However, at times you will want to prevent the user agent from breaking lines across certain phrases. There are also times when you may want to allow a user agent to hyphenate long words to help preserve formatting.

7.2.1. Nonbreaking Spaces

Just as you will want to break some text into discrete chunks, other times you will want to keep text together. For example, you wouldn't want words separated in dates (December 25, 2004), awkward phrases that include letters and numbers (24 hours), or in some company names (International Business Machine Corporation).

Suppose you were to use the phrase "12 Angry Men." You would not want a user agent to split the "12" and "Angry" across two lines as shown in the following:

A good example of this argument appears in the movie 12
Angry Men.

Whenever you don't want the user agent to break text, you should use a nonbreaking space entity ( ) instead of a normal space. For example, when coding the "12 Angry Men" paragraph, you could use something similar to the following:

<p>A good example of this argument appears in the movie
<i>12&nbsp;Angry&nbsp;Men</i>.</p>

As discussed in previous chapters, user agents tend to collapse white space. This is typically a desirable effect—allowing you to be more liberal with white space when formatting your documents. However, sometimes ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.