deadhead
element/xmp

xmp element

xmp is obsolete; it shows markup as literal text but can't hold character references or its own end tag, so use pre and code with < and & escaped.

  • Avoid
  • Deprecated
Severity
DeprecatedFormally obsolete, but inert.
Basis
Obsoleted by the specA standard defined it, then removed it.
Detectable
YesMatched exactly. Autofixable when the rule carries a fix.
Matches
xmpElement rule, inside <body>.
Fix
None — reports only
Impacts
Maintainability
Related
element/listing, element/plaintext

<xmp> was a shortcut for showing HTML source on a page: everything between <xmp> and </xmp> appears as typed, in a monospace font, with no need to escape angle brackets. MDN traces it to HTML 2, and it still turns up in old pages.

Why avoid

It is obsolete. The HTML Standard lists xmp among the elements that “are entirely obsolete, and must not be used by authors”, with the instruction: “Use pre and code instead, and escape < and & characters as &lt; and &amp; respectively.”

The convenience comes with limits that are easy to trip over. The parser handles <xmp> with “the generic raw text element parsing algorithm”, which switches the tokenizer to the RAWTEXT state. In that state nothing is markup, and that includes character references: the tokenizer only watches for <, to find the end tag. Two things follow:

  • Character references show up literally. Writing &amp; inside xmp displays the five characters &amp;, not an ampersand, so there is no way to write a character you can’t type.
  • The content can never contain </xmp>. The first </xmp ends the element, so a page can’t use xmp to show markup that itself mentions xmp.

What remains is appearance. The rendering section styles it exactly like pre: display block, margin-block: 1em, and font-family: monospace; white-space: pre. It says nothing about what the content is. The HTML Accessibility API Mappings give code the WAI-ARIA code role, and xmp no role at all.

Use instead

Wrap the sample in pre and code, and escape < and &:

<pre><code>&lt;p class="note"&gt;Tom &amp; Jerry&lt;/p&gt;</code></pre>

Detectability

Fully detectable by tag name. deadhead never lints the content of an xmp, because a browser reads it as text, so only the element itself is reported. There is no autofix: removing it would delete the content, and moving to pre and code means renaming the element and escaping its content, which a fix can’t do.

Resources

This page is generated from content/rules/element/xmp.md, the same file the linter is built from. Think the rule is wrong, or that browsers moved on? Say so — that is the most useful issue you can file.