document.createElementNS() is an instance method that creates a new element with an explicit namespace URI and qualified name (see MDN Document: createElementNS()). Learn when you need it for SVG and MathML, how it differs from createElement(), common namespace strings, and five try-it labs.
01
Kind
Instance method
02
Args
ns + name
03
Returns
Element
04
Best for
SVG / MathML
05
vs HTML
createElement
06
Status
Baseline
Fundamentals
Introduction
HTML pages often mix languages: HTML for layout, SVG for icons and charts, MathML for equations. Each language lives in its own namespace — a URI that tells the browser which vocabulary a tag belongs to.
document.createElementNS(namespaceURI, qualifiedName) creates an element in a chosen namespace. MDN notes this is useful when the parser cannot reliably infer the namespace — for example building SVG from JavaScript inside an HTML document.
💡
Remember the SVG gotcha
MDN: createElement("svg") in an HTML document returns an HTMLUnknownElement — the drawing will not render. Always use the SVG namespace with createElementNS.
For plain HTML tags (div, button, p), prefer the simpler createElement().
Document.createElementNS() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Optional options features may have varying support.
✓ Baseline Widely available
Document.createElementNS()
Create SVG, MathML, and other namespaced elements across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createElementNS()Wide
Bottom line: Use createElementNS for SVG and MathML. Prefer createElement for everyday HTML tags.
Wrap Up
Conclusion
document.createElementNS(namespaceURI, qualifiedName) builds elements that belong to a specific vocabulary. For SVG and MathML inside HTML pages, it is the correct tool — createElement alone will not produce real SVG nodes.
Use the SVG namespace for every SVG tag you create
Keep namespace URIs in named constants
Prefer createElement for plain HTML
Check el instanceof SVGSVGElement when debugging
Append the root so the graphic can render
❌ Don’t
Call createElement("svg") expecting a real SVG (MDN)
Mix HTML-namespace children into SVG without knowing the rules
Typo the namespace URI string
Set both is and customElementRegistry
Forget to insert the detached element into the DOM
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createElementNS()
Namespaced elements for SVG, MathML, and more.
5
Core concepts
📝01
Needs
namespace URI
MDN
🎨02
SVG
must use NS
gotcha
📄03
HTML
createElement OK
simpler
⚡04
Returns
Element
typed
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createElementNS() creates a new element with a specified namespace URI and qualified name. Use it for SVG, MathML, or other namespaces in mixed documents.
No. MDN marks Document.createElementNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some optional options features may vary.
MDN: createElement() is simpler for plain HTML elements. Prefer createElementNS() when the parser cannot reliably infer the namespace — especially SVG or MathML inside HTML.
MDN: In an HTML document, createElement("svg") returns an HTMLUnknownElement, so the SVG will not render correctly. You must use createElementNS with the SVG namespace http://www.w3.org/2000/svg.
MDN highlights: XHTML http://www.w3.org/1999/xhtml, SVG http://www.w3.org/2000/svg, and MathML http://www.w3.org/1998/Math/MathML.
A new Element (MDN). For SVG tags in the SVG namespace you get SVG-specific interfaces such as SVGSVGElement or SVGCircleElement.
Did you know?
The SVG namespace string http://www.w3.org/2000/svg looks like a web address, but the browser does not fetch it. It is an identifier — a unique name for the SVG vocabulary — so every SVG element can be recognized correctly in mixed documents.