The Web Design Group

pre - Preformatted Text

Syntax <pre>...</pre>
Attribute Specifications
Contents Inline elements except img, object, applet, big, small, sub, sup, font, basefont
Contained in applet, blockquote, body, button, center, dd, del, div, fieldset, form, iframe, ins, li, map, noframes, noscript, object, td, th

The pre element contains preformatted text. Visual browsers should render preformatted text in a fixed-pitch font, should not collapse whitespace, and should not wrap long lines.

pre is useful for formatting computer code or poetry where whitespace is important, but since preformatted text is inherently visual, authors should avoid dependence on it wherever possible. When using pre, authors should avoid altering the element's fixed-pitch font or non-collapsing whitespace properties by means of style sheets.

The following example features Java code in a pre element:

<pre><code class="java">
class helloworld {
  public static void main(string[] args) {
    system.out.println("hello world!");

The code in the preceding example would be more difficult to read if it were not preformatted, and so pre is used to provide the proper spacing. Note that the code element is also included to add structural information.

The deprecated width attribute of pre tells the browser the expected line length of the preformatted block so that a suitable font size or margin can be used. Browsers ignore this attribute in practice.

The bidirectional algorithm that determines the directionality of text still applies within the pre element.

More Information