DOM stands for Document Object Model. The web browser uses DOM to represent the HTML document internally. Additionally, it provides a set of functions and methods to modify the HTML document programmatically. These functions and methods are often called DOM Application Programming Interfaces or DOM API.
represents the HTML document as a tree structure
allows JavaScript to access, read, and change the page
updates the page dynamically without reloading
<div class="infographic-domtree">
<style>
.infographic-domtree {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--good: #1a8a5f;
--good-soft: #e1f2ea;
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
font-family: 'Inter', system-ui, sans-serif;
color: var(--ink);
background: var(--paper);
overflow-x: hidden;
}
.infographic-domtree[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--good: #4fbf8f;
--good-soft: #16281f;
}
.infographic-domtree * { box-sizing: border-box; }
.infographic-domtree h1 { font-family: 'Fraunces', Georgia, serif; font-weight: 600; margin: 0; }
.infographic-domtree p { margin: 0; }
.infographic-domtree button { font-family: inherit; }
.dt-wrap { padding: clamp(1.25rem, 5vw, 2.5rem) clamp(1.1rem, 5vw, 3rem) 2rem; }
.dt-hero { text-align: center; margin-bottom: 1.2rem; }
.dt-hero h1 { font-size: clamp(1.35rem, 4.5vw, 1.85rem); }
.dt-hero p { color: var(--ink-soft); margin-top: 0.45rem; font-size: 0.86rem; }
.dt-stage { position: relative; width: 100%; max-width: 560px; margin: 0 auto; aspect-ratio: 560 / 320; }
.dt-svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.dt-path { stroke: var(--line); stroke-width: 2; fill: none; transition: stroke 0.3s ease, stroke-width 0.3s ease; }
.dt-path.lit { stroke: var(--accent); stroke-width: 2.5; }
.dt-path.lit-child { stroke: var(--good); stroke-width: 2.5; }
.dt-node {
position: absolute; transform: translate(-50%, -50%);
width: 66px; height: 66px; border-radius: 50%;
border: 1.5px solid var(--line); background: var(--paper-alt);
display: flex; align-items: center; justify-content: center; text-align: center;
font-family: 'IBM Plex Mono', monospace; font-weight: 700; font-size: 0.68rem; color: var(--ink-soft);
cursor: pointer; transition: border-color 0.25s ease, background 0.25s ease, color 0.25s ease, transform 0.25s ease;
padding: 0.2rem;
}
.dt-node:hover { border-color: var(--accent); }
.dt-node.is-selected { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); transform: translate(-50%, -50%) scale(1.12); }
.dt-node.is-parent { border-color: var(--ink-soft); color: var(--ink); }
.dt-node.is-child { border-color: var(--good); background: var(--good-soft); color: var(--good); }
.dt-caption { text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 1.3rem; min-height: 2.6em; width: 100%; }
.dt-caption strong { color: var(--ink); }
.dt-legend { display: flex; justify-content: center; gap: 1.4rem; margin-top: 0.8rem; font-size: 0.72rem; color: var(--ink-soft); flex-wrap: wrap; }
.dt-legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.dt-legend .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dt-legend .dot.sel { background: var(--accent); }
.dt-legend .dot.par { background: var(--ink-soft); }
.dt-legend .dot.chi { background: var(--good); }
@media (prefers-reduced-motion: reduce) {
.infographic-domtree *, .infographic-domtree *::before, .infographic-domtree *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="dt-wrap">
<div class="dt-hero">
<h1>The DOM is a tree</h1>
<p>Click a node to see its parent and children.</p>
</div>
<div class="dt-stage">
<svg class="dt-svg" viewBox="0 0 560 320" preserveAspectRatio="xMidYMid meet">
<path class="dt-path" id="p-html-head" d="M280,40 L170,160"></path>
<path class="dt-path" id="p-html-body" d="M280,40 L470,160"></path>
<path class="dt-path" id="p-head-meta1" d="M170,160 L60,280"></path>
<path class="dt-path" id="p-head-meta2" d="M170,160 L170,280"></path>
<path class="dt-path" id="p-head-title" d="M170,160 L280,280"></path>
<path class="dt-path" id="p-body-h1" d="M470,160 L470,280"></path>
</svg>
<div class="dt-node" id="n-html" style="left: 50%; top: 12.5%;" data-id="html">HTML</div>
<div class="dt-node" id="n-head" style="left: 30.4%; top: 50%;" data-id="head">HEAD</div>
<div class="dt-node" id="n-body" style="left: 83.9%; top: 50%;" data-id="body">BODY</div>
<div class="dt-node" id="n-meta1" style="left: 10.7%; top: 87.5%;" data-id="meta1">META</div>
<div class="dt-node" id="n-meta2" style="left: 30.4%; top: 87.5%;" data-id="meta2">META</div>
<div class="dt-node" id="n-title" style="left: 50%; top: 87.5%;" data-id="title">TITLE</div>
<div class="dt-node" id="n-h1" style="left: 83.9%; top: 87.5%;" data-id="h1">H1</div>
</div>
<p class="dt-caption" id="dtCaption">Every element is a node. Every node — except the root — has exactly one parent.</p>
<div class="dt-legend">
<span><span class="dot sel"></span> selected</span>
<span><span class="dot par"></span> parent</span>
<span><span class="dot chi"></span> children</span>
</div>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-domtree')
|| document.querySelector('.infographic-domtree');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const tree = {
html: { parent: null, children: ['head', 'body'], label: 'HTML', desc: 'the root — no parent' },
head: { parent: 'html', children: ['meta1', 'meta2', 'title'], label: 'HEAD', desc: null },
body: { parent: 'html', children: ['h1'], label: 'BODY', desc: null },
meta1: { parent: 'head', children: [], label: 'META', desc: null },
meta2: { parent: 'head', children: [], label: 'META', desc: null },
title: { parent: 'head', children: [], label: 'TITLE', desc: null },
h1: { parent: 'body', children: [], label: 'H1', desc: null },
};
const edgeMap = {
'html-head': 'p-html-head', 'html-body': 'p-html-body',
'head-meta1': 'p-head-meta1', 'head-meta2': 'p-head-meta2', 'head-title': 'p-head-title',
'body-h1': 'p-body-h1',
};
const nodes = wrapper.querySelectorAll('.dt-node');
const paths = wrapper.querySelectorAll('.dt-path');
const caption = wrapper.querySelector('#dtCaption');
function clearAll() {
nodes.forEach(n => n.classList.remove('is-selected', 'is-parent', 'is-child'));
paths.forEach(p => p.classList.remove('lit', 'lit-child'));
}
function litEdge(a, b, cls) {
const key1 = a + '-' + b, key2 = b + '-' + a;
const id = edgeMap[key1] || edgeMap[key2];
if (id) wrapper.querySelector('#' + id).classList.add(cls);
}
function selectNode(id) {
clearAll();
const node = tree[id];
wrapper.querySelector('#n-' + id).classList.add('is-selected');
let text = '<strong>' + node.label + '</strong>';
const parts = [];
if (node.parent) {
wrapper.querySelector('#n-' + node.parent).classList.add('is-parent');
litEdge(node.parent, id, 'lit');
parts.push('parent: <strong>' + tree[node.parent].label + '</strong>');
} else {
parts.push('no parent — it\'s the root');
}
if (node.children.length) {
node.children.forEach(c => {
wrapper.querySelector('#n-' + c).classList.add('is-child');
litEdge(id, c, 'lit-child');
});
const childLabels = node.children.map(c => tree[c].label);
parts.push('children: <strong>' + childLabels.join(', ') + '</strong>');
} else {
parts.push('no children — a leaf node');
}
caption.innerHTML = text + ' — ' + parts.join(', ') + '.';
}
nodes.forEach(n => {
n.addEventListener('click', () => selectNode(n.dataset.id));
});
})();
</script>This diagram is like a family tree of an HTML document — it shows how the browser understands and structures your HTML page in memory.
✅ Types of Nodes
Document node → the whole page
Element nodes → tags (
<div>,<p>)Text nodes → text inside elements
Attribute nodes → element attributes (
id,class)
🔗 Relationship Terms
Just like a family tree:
Parent → element above
Child → element inside another
Siblings → elements with the same parent
Example:
<ul>
<li>Apple</li>
<li>Banana</li>
</ul><ul>→ parentboth
<li>→ children + siblings
🧠 Why DOM is Important?
Because it allows JavaScript to:
✅ change content
✅ change styles
✅ add/remove elements
✅ respond to user actions (events)
✅ build dynamic UI (forms, sliders, modals, etc.)
💡 Example — DOM in Action
HTML:
<h1 id="title">Hello</h1>
<button onclick="changeText()">Click Me</button>JavaScript:
function changeText() {
document.getElementById("title").textContent = "Welcome!";
}✔️ When the button is clicked → DOM updates the text without reloading ✅
🏗️ DOM is Not:
🚫 Not a programming language
🚫 Not part of HTML itself
🚫 Not created by JavaScript — it is created by the browser
JavaScript just uses the DOM to control the page.