HTML <xmp> Element

The <xmp> element, short for example, was an early HTML element used to display blocks of text and HTML markup without the browser interpreting most of the markup as elements. It also preserved whitespace and line breaks, making it useful for displaying HTML examples and other preformatted text.

Historical Syntax

Older HTML documents could contain markup similar to the following:

<xmp>
<h1>HTML Example</h1>
<p>This markup is displayed as text.</p>
</xmp>

Unlike normal HTML content, the markup between the opening and closing <xmp> tags was displayed as text rather than interpreted as headings, paragraphs, or other HTML elements. The closing </xmp> tag ended the example so normal HTML parsing could continue afterward. This code is shown for historical reference only and should not be used in modern HTML.

Attributes

The <xmp> element did not have any element-specific attributes. Its historical behavior came from the way browsers parsed and displayed the content between its opening and closing tags.

Historical Use

The <xmp> element dates back to the earliest versions of HTML and was designed primarily for displaying examples of markup. Authors could place HTML source inside the element without converting every less-than and greater-than character into character references.

The element also preserved whitespace and line breaks and was normally displayed in a monospace font. These characteristics made it convenient for source-code examples, formatted text, and other content where the original spacing needed to remain visible.

Unlike the obsolete <plaintext> element, which caused everything through the end of the document to be treated as plain text, <xmp> had an end tag. This allowed normal HTML content to continue after the example.

Why It Became Obsolete

The <xmp> element relied on special parsing behavior that did not fit well with the way modern HTML represents structured content. HTML provides standard elements for identifying preformatted text and computer code without requiring a special element that changes how its contents are parsed.

The <pre> element preserves whitespace and line breaks, while <code> identifies computer code. When HTML markup needs to be displayed as text, special characters such as < and > can be written using the &lt; and &gt; character references.

This approach keeps the document structure predictable while clearly describing the purpose of the displayed content.

Modern Alternative

For a block of HTML source code, place a <code> element inside <pre> and represent the HTML special characters with character references:

<pre><code>&lt;h1&gt;HTML Example&lt;/h1&gt;
&lt;p&gt;This markup is displayed as text.&lt;/p&gt;</code></pre>

The <pre> element preserves the formatting of the example, while <code> identifies the content as computer code. The &lt; and &gt; character references cause the angle brackets to appear on the page instead of being interpreted as HTML markup.

This provides the useful behavior authors once obtained from <xmp> while using conforming modern HTML.

Play in Editor

Legacy Browser Support

The <xmp> element is obsolete, but modern browsers may continue to recognize its historical parsing and display behavior for compatibility with older webpages. This legacy browser support does not make the element valid for use in modern HTML.

Existing <xmp> elements should be replaced with <pre> and, when the content represents computer code, <code>. HTML special characters within the example should be represented with character references.

For detailed legacy browser compatibility information, see Can I Use: HTML <xmp> Element .

The modern <pre> element preserves whitespace and line breaks, while the <code> element identifies computer code. The obsolete <plaintext> and <listing> elements were also historically used for displaying literal or preformatted text.

Specifications

The <xmp> element is listed as an entirely obsolete, non-conforming feature in the WHATWG HTML Living Standard . Authors should use <pre> and <code> with character references when displaying HTML source code.