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 < and & 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
&insidexmpdisplays the five characters&, not an ampersand, so there is no way to write a character you can’t type. - The content can never contain
</xmp>. The first</xmpends the element, so a page can’t usexmpto show markup that itself mentionsxmp.
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><p class="note">Tom & Jerry</p></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
- HTML Standard — Non-conforming features —
xmpis obsolete; usepreandcodewith<and&escaped instead. - HTML Standard — Parsing: the “in body” insertion mode —
xmpuses the generic raw text element parsing algorithm, whose RAWTEXT state decodes no character references. - HTML Standard — Rendering: flow content —
xmpis displayed likepre: block, monospace,white-space: pre. - W3C — HTML Accessibility API Mappings —
codemaps to the ARIAcoderole. - MDN —
<xmp>— deprecated since HTML 3.2, “not implemented in a consistent way”; usepreorcode.
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.