HTML xmlns Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The xmlns attribute declares an XML namespace URI for an element. On the root <html> element, MDN documents the default as http://www.w3.org/1999/xhtml.

It is required when the document is parsed as XML/XHTML, and optional in normal text/html documents. In HTML parsing, the correct XHTML value has little practical effect — the parser already puts HTML elements in that namespace. Prefer a clear lang on <html> for everyday pages.

Not deprecated.

Still valid. Meaningful for XML/XHTML; mostly a compatibility marker in text/html when set to the XHTML URI.

HTML vs XML parsing.

In text/html, a correct xmlns on <html> does not change layout. Wrong or prefixed namespace declarations are not how you “add XML features” to HTML pages.

HTML Syntax

syntax.html
<html xmlns=" namespace-uri">

Common value on <html>: http://www.w3.org/1999/xhtml. On <svg>: http://www.w3.org/2000/svg.

Attribute Values

Namespace URIs you will see most often:

ValueDescriptionTry it
http://www.w3.org/1999/xhtmlXHTML / HTML element namespaceTry it
http://www.w3.org/2000/svgSVG namespace (inline graphics)Try it
namespaceURI checkInspect the live DOM namespace in scriptTry it

Live Preview

Inspect this page’s root <html> and a sample <svg>. Namespace does not change the drawing — it labels the vocabulary.

inspect

Reads namespaceURI and the xmlns attribute (if present).

Demo

Click html or svg.

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Everyday pages need lang more than xmlns.

Example 1 — xmlns on <html>

XHTML-compatible root. In text/html this is optional.

HTML
<!DOCTYPE html>
<html
  xmlns="http://www.w3.org/1999/xhtml"
  lang="en"
>
  <head>
    <meta charset="UTF-8">
    <title>xmlns on html</title>
  </head>
  <body>
    <p>Root declares the XHTML namespace.</p>
  </body>
</html>
Try It Yourself

How It Works

  1. DOCTYPE still starts a normal HTML document.
  2. xmlns names the XHTML vocabulary on the root.
  3. lang remains the important everyday attribute.
  4. In text/html, layout is unchanged if the URI is correct.

Example 2 — xmlns on inline <svg>

Common when embedding SVG markup, especially for XML tools.

HTML
<svg
  xmlns="http://www.w3.org/2000/svg"
  width="120"
  height="60"
  aria-hidden="true"
>
  <circle cx="30" cy="30" r="20" fill="currentColor"></circle>
  <text x="60" y="35" font-size="14" fill="currentColor">OK</text>
</svg>
<p>Inline SVG with an explicit SVG namespace.</p>
Try It Yourself

How It Works

  1. SVG URI identifies SVG elements.
  2. HTML parser already maps <svg> into that namespace.
  3. Writing xmlns helps XML serializers and human readers.
  4. Drawing comes from SVG geometry, not from the attribute text itself.

Example 3 — Inspect namespaceURI

Prove HTML elements live in the XHTML namespace even without writing xmlns.

HTML
<p id="sample">Hello</p>
<p id="status"></p>

<script>
  var el = document.getElementById("sample");
  var status = document.getElementById("status");
  status.textContent =
    "namespaceURI = " + el.namespaceURI +
    " | xmlns attr = " + el.getAttribute("xmlns");
</script>
Try It Yourself

How It Works

  1. No xmlns on the paragraph in markup.
  2. namespaceURI still reports the XHTML namespace.
  3. getAttribute("xmlns") is null unless you wrote the attribute.
  4. Lesson: HTML parsing implies the namespace for you.

Edge Cases & Pitfalls

Most issues come from treating xmlns like a style switch.

html

Optional in text/html

Skip it on normal websites unless you care about XHTML/XML compatibility. Keep lang.

xml

Required for XML parsers

XHTML served as XML needs the XHTML namespace on the root html element.

wrong uri

Do not invent namespaces

On HTML elements in HTML parsing, only the exact XHTML URI is allowed as a harmless explicit xmlns. Random URIs do not unlock features.

svg

SVG still draws without writing it

In HTML documents the parser assigns the SVG namespace to <svg>. Explicit xmlns is documentation / XML friendliness.

prefix

Avoid prefixed HTML

Do not write html:div style prefixed HTML in text/html. Stick to plain HTML tags.

Browser Support

xmlns is an XML namespace declaration. In text/html it is optional; in XML/XHTML it is required on the root.

✓ Widely available

xmlns

Supported wherever HTML/XML documents are parsed. Effect depends on parsing mode (HTML vs XML), not on a separate feature flag.

100% Parser support (approx.)
Google Chrome All
Supported
Mozilla Firefox All
Supported
Apple Safari All
Supported
Microsoft Edge All
Supported
Opera All
Supported
Samsung Internet All
Supported
Internet Explorer All*
Supported*
xmlns Universally recognized

Bottom line: *Parsing rules differ for text/html vs XML. Correct XHTML xmlns is a talisman in HTML mode; required in XML mode.

Key Takeaways

  • Role: declare an XML namespace URI.
  • HTML root: http://www.w3.org/1999/xhtml.
  • text/html: optional; parser already assigns namespaces.
  • XML/XHTML: required on the root.

One line: xmlns names a vocabulary for XML — optional in everyday HTML, required when the document is parsed as XML/XHTML.

Frequently Asked Questions

It declares an XML namespace URI for an element. On the root html element, the default/XHTML namespace is http://www.w3.org/1999/xhtml. It is required when the document is parsed as XML/XHTML, and optional in normal text/html documents.
No. For everyday text/html pages, the HTML parser already places elements in the XHTML namespace. xmlns on html is optional and usually has no visible effect.
When the document is served/parsed as XML (XHTML). XML parsers need an explicit namespace declaration; HTML parsers do not.
The SVG namespace is http://www.w3.org/2000/svg. In HTML documents, the parser assigns the SVG namespace to svg automatically. Writing xmlns is still common for clarity and for XML serialization.
No. It is not obsolete. In text/html it is largely a compatibility talisman when set to the XHTML URI; in XML/XHTML it remains meaningful and required on the root.
In JavaScript, read element.namespaceURI. HTML elements typically report http://www.w3.org/1999/xhtml even if xmlns was omitted from the markup.

Next: HTML Tags

Continue with the HTML element reference.

HTML tags index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful