DOM tree

DOM 트리

HTML elements become a tree of parent and child nodes as the browser parses them.

···
html
<div class="v"><header>HTML → DOM <span id="src">&lt;main&gt;</span></header><div class="stage tree"><div class="root">document</div><div class="branch" id="main">main<div class="children"><div id="h">h1</div><div id="p">p</div></div></div></div><div class="muted" id="note">Token 1 / 3</div></div>
css
.v{width:min(94vw,760px);height:min(88vh,320px);padding:clamp(9px,2.4vmin,18px);border:1px solid var(--line);border-radius:12px;background:var(--surface);font:500 clamp(12px,2.4vmin,16px)/1.3 var(--font-sans,system-ui,sans-serif);display:flex;flex-direction:column;gap:clamp(5px,1.4vmin,10px);overflow:hidden}.v header{display:flex;align-items:center;justify-content:space-between;gap:8px;color:var(--accent);font-weight:700}.v header span{color:var(--muted);font-weight:500}.v .stage{flex:1;min-height:0;position:relative}.v .muted{color:var(--muted)}.v .active{background:color-mix(in srgb,var(--accent) 18%,var(--surface));border-color:var(--accent)!important;color:var(--fg)}.v button{font:inherit;color:var(--fg);background:var(--bg);border:1px solid var(--line);border-radius:7px;padding:5px 8px;cursor:pointer}.tree{display:flex;align-items:center;justify-content:center;flex-direction:column;gap:5px}.root,.branch,.children div{border:1px solid var(--line);border-radius:7px;background:var(--bg);padding:5px 12px;text-align:center}.branch{position:relative;opacity:.25;transition:opacity .25s}.children{display:flex;gap:9px;margin-top:7px}.children div{opacity:.25;min-width:56px;transition:opacity .25s}.tree .on{opacity:1;border-color:var(--accent)}
js
let i=0;const names=["main","h","p"],tags=["<main>","<h1>","<p>"];function tick(){names.forEach((n,j)=>document.getElementById(n).classList.toggle("on",j<=i));document.getElementById("src").textContent=tags[i];document.getElementById("note").textContent="Token "+(i+1)+" / 3";i=(i+1)%3}tick();setInterval(tick,800)

The DOM represents document structure as nodes. The parser opens children on start tags and returns to parents on end tags.

In the demo, each input line adds a node to the tree. DOM changes can make the browser recompute relevant styles and display, so consider structure changes together with visual updates.

When to use

Use this model to reason about element selection, event delegation, and dynamic UI updates.

Open as page ↗