Preformatted Text

By this point you may have noticed that HTML has a characteristic way of handling spaces and line breaks in a text. Basically, any number of contiguous spaces are reduced to a single space, while line breaks are ignored entirely.

There may be times when you wish to preserve the line breaks and the exact spacing of a chunk of text. This is when you will use the preformatted text element.

Here's a chunk of text -- a few lines by the poet e. e. cummings -- displayed in two different formats:

-in the woods which stutter and sing


        -in the woods
                     which
                          stutter
                                 and

                                    sing

Notice that only the second case reflects the author's famous typographical style. In the first example, line breaks and multiple spaces are ignored.

Now look at the HTML code to understand what's going on:

<P>
        -in the woods
                     which
                          stutter
                                 and

                                    sing
</P>

<PRE>
        -in the woods
                     which
                          stutter
                                 and

                                    sing
</PRE>

As you can see, the text inside the tags is the same in both cases. The only difference is the tags themselves. The first example wraps the text in our old friend, the paragraph element. The second example preserves the original formatting by use of the <PRE> and </PRE> tags. Most browsers display the content of preformatted text in a monospaced font like Courier.

Preformatted text is a block-level element.


| Introduction to HTML | Tutorials |