👀 Live Preview
Inline SVG with the correct namespace on this page:

The xmlns attribute stands for XML Namespace. It declares which vocabulary an element belongs to, so the browser or parser knows whether a tag like <title> is HTML, SVG, or something else.
HTML5 pages written as normal text/html do not require xmlns on <html>. You will most often see it on inline <svg> or <math> elements, or on the root when serving XHTML as XML.
What it means.
Inline graphics.
Root on html.
Not fetched.
When optional.
createElementNS.
xmlns AttributeThe purpose of xmlns is to define a namespace for elements and attributes in a document tree. Namespaces prevent collisions when multiple vocabularies share tag names. For example, both HTML and SVG can have a title element, but they mean different things in different namespaces.
In modern web development, the most practical use is embedding SVG or MathML inside HTML. The foreign root element gets an xmlns attribute so its children are parsed with the correct rules.
Standard HTML5 documents do not need xmlns on <html>. Adding xmlns="http://www.w3.org/1999/xhtml" with <!DOCTYPE html> is optional and usually ignored for layout. True XHTML requires XML syntax and is served as application/xhtml+xml.
Set xmlns to a namespace URI on the element that starts a foreign vocabulary:
<!-- Inline SVG in HTML -->
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle cx="50" cy="50" r="40" fill="blue"/>
</svg>
<!-- XHTML root (XML documents) -->
<html xmlns="http://www.w3.org/1999/xhtml">...</html>xmlns="URI".xmlns:prefix="URI" then <prefix:tag>.document.createElementNS(uri, name) for namespaced elements.xmlns on HTML <div> expecting XML behavior in HTML parsers.The value of xmlns is a namespace URI. Common URIs on the web:
http://www.w3.org/1999/xhtml (root element in XHTML documents)http://www.w3.org/2000/svg (inline or standalone SVG)http://www.w3.org/1998/Math/MathML (mathematical notation)https://example.com/ns/app) for XML vocabularies<html xmlns="http://www.w3.org/1999/xhtml">...</html>
<svg xmlns="http://www.w3.org/2000/svg">...</svg>
<math xmlns="http://www.w3.org/1998/Math/MathML">...</math>When the parser sees xmlns="http://www.w3.org/2000/svg" on <svg>, it treats nested elements like <circle> as SVG, not as unknown HTML tags. The URI is an identifier only; browsers do not fetch it over the network.
| Context | xmlns value | Required? |
|---|---|---|
HTML5 text/html | None on html | No |
Inline svg | http://www.w3.org/2000/svg | Yes (recommended) |
Inline math | http://www.w3.org/1998/Math/MathML | Yes (recommended) |
| XHTML XML document | http://www.w3.org/1999/xhtml | Yes |
| JS API | Same URI in createElementNS | When building SVG/MathML |
| Element | Typical xmlns | Notes |
|---|---|---|
<html> | XHTML URI | XHTML/XML documents only |
<svg> | SVG URI | Most common HTML5 use case |
<math> | MathML URI | Equations in HTML |
| Any XML root | Custom or standard URI | Defines vocabulary scope |
<div>, <p> | Not used | HTML elements use the HTML namespace implicitly |
| Feature | HTML5 | XHTML (XML) | Inline SVG |
|---|---|---|---|
| Root xmlns | Not required | Required on html | N/A (svg is child) |
| MIME type | text/html | application/xhtml+xml | Inside HTML |
| Self-closing tags | Optional style | Must follow XML rules | SVG XML rules |
| Typical xmlns | On foreign roots only | 1999/xhtml | 2000/svg |
Declare SVG namespace inline, see XHTML root declaration, and build SVG with JavaScript namespaces.
Inline SVG with the correct namespace on this page:
Using xmlns on inline SVG inside an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Document</title>
</head>
<body>
<h1>This is a Heading</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80">
<circle cx="40" cy="40" r="30" fill="#3b82f6"/>
</svg>
</body>
</html>The xmlns on <svg> puts that subtree in the SVG namespace so elements like <circle> render as vector graphics instead of invalid HTML tags.
Declaring the XHTML namespace on the document root (for XML/XHTML, not typical HTML5):
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example Document</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>On a true XHTML document served as XML, xmlns="http://www.w3.org/1999/xhtml" on <html> identifies the default vocabulary. On ordinary HTML5 pages it is optional and usually unnecessary.
Create namespaced SVG nodes with the DOM API (preferred over setAttribute('xmlns') on HTML elements):
<div id="canvas"></div>
<script>
var SVG_NS = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(SVG_NS, "svg");
svg.setAttribute("width", "120");
var rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("fill", "#22c55e");
svg.appendChild(rect);
document.getElementById("canvas").appendChild(svg);
</script>createElementNS passes the namespace URI directly. The browser creates a proper SVG element tree without manually setting xmlns on each node.
role="img" and aria-label (or <title> inside SVG) for meaningful graphics.aria-hidden="true" when purely decorative.xmlns does not automatically make content accessible.URI on root.
To descendants.
HTML vs SVG.
Mixed vocabularies work.
Namespace handling is built into all modern browsers. Inline SVG with xmlns works everywhere. XHTML-as-XML is niche; most sites use HTML5 text/html without root xmlns.
Use SVG xmlns for inline graphics; skip root xmlns on typical HTML5 pages.
Bottom line: Safe for inline SVG and MathML; root XHTML xmlns only when serving true XHTML/XML.
<svg> rootscreateElementNS when building SVG in JShtmlxmlns when integrating HTML with SVG, MathML, or other XML vocabularies.While xmlns is rarely needed on everyday HTML5 documents, it is essential for inline SVG, MathML, and XML-based workflows. It keeps vocabularies separate so mixed markup parses and renders correctly.
Understanding namespaces helps you embed graphics, work with XHTML when required, and use the right JavaScript APIs for namespaced content.
xmlnsBookmark these before your next xmlns implementation.
xmlns declares an XML namespace URI for an element subtree.
HTML5 pages usually do not need xmlns on <html>.
Inline SVG needs http://www.w3.org/2000/svg.
Use createElementNS in JavaScript for namespaced elements.
text/html do not require it. Add xmlns when using XHTML as XML or on foreign roots like SVG.xmlns="http://www.w3.org/2000/svg" on the root svg element is a common cause when SVG is pasted into HTML.xmlns sets the default namespace; prefixed forms like xmlns:xlink bind a prefix to another URI for qualified names.createElementNS or insert a proper svg subtree.Draw inline SVG with the correct xmlns declaration.
5 people found this page helpful