Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Elements and Styles

The Elements Panel and the DOM Tree

Explore the Elements panel: the live DOM tree, selecting and editing nodes, breakpoints on DOM changes, search, and copy selector, with runnable DOM-tree stand-ins.

The Elements panel shows the page the way the browser understands it: as a tree of nodes called the DOM (Document Object Model). It is the most-used panel in DevTools because most bugs a visitor can see are about structure or styling: an element that is missing, nested in the wrong place, hidden, or styled by a rule you did not expect.

An important point first: the Elements panel shows the live DOM, not your HTML file. If JavaScript added, removed or moved nodes, you see the result. If the browser fixed invalid markup (for example by closing a tag you forgot), you see the fixed tree. To see the original text sent by the server, use View page source or the Network panel's Response tab.

Reading the tree

Each line in the panel is a node. You can expand and collapse them with the small arrows. A few details help you read it quickly:

  • Element nodes show as tags: <div class="card">. Click the arrow to open children.
  • Text appears as plain lines inside elements.
  • A ::before or ::after line is a pseudo-element generated by CSS, not real markup.
  • #shadow-root marks a shadow DOM, used by web components and some built-in controls such as <video> and <input type="date">.
  • <!-- ... --> lines are HTML comments.
  • == $0 after a node means this node is your current selection, and you can reach it in the Console as $0 (covered in the Console chapter).

Hovering a node highlights the element on the page with overlays for margin, border, padding and content, plus a tooltip with its size.

Selecting nodes

There are four common ways to select an element:

  1. Click the Select an element arrow at the top left (Ctrl+Shift+C), then click on the page.
  2. Right-click the element on the page and choose Inspect.
  3. Click a node in the tree and use the arrow keys (Up and Down move between visible nodes, Right and Left expand and collapse).
  4. Use Ctrl+F inside the Elements panel to search by tag text, class, id, CSS selector or XPath.

The search box understands three kinds of queries: plain text (matches inside tag names, attributes and text), a CSS selector such as nav a.active, and an XPath such as //ul/li[3]. The count of matches appears next to the box, and Enter jumps to each one.

Editing the DOM

Everything below is temporary and lasts until reload:

  • Double-click an attribute value or a tag name to edit it.
  • Press F2 on a selected node to edit it as HTML (the whole element and its children).
  • Right-click a node to find Add attribute, Edit as HTML, Duplicate element, Delete element and Hide element (which applies a visibility: hidden rule temporarily).
  • Drag a node to move it to a new place in the tree.
  • Press Delete to remove the node.
  • Ctrl+Z undoes the last DOM change in the panel.

This is a good way to test: "what would the page look like if this paragraph were removed?" or "does this class name fix the style?" before editing source code.

Do not trust a fixed result just because it looks right in DevTools. If you fix a layout by editing the live DOM, you still need to make the same change in your template or component, or it will vanish on reload.

Break on DOM changes

Sometimes you do not know which script changed a node. Right-click the node and choose Break on, then one of:

  • Subtree modifications: pause when a child is added, removed or moved.
  • Attribute modifications: pause when an attribute of this node changes.
  • Node removal: pause when this node is removed from the page.

When one fires, the Sources panel pauses on the exact line of JavaScript that made the change, with the call stack. This is one of the most powerful tricks for hunting "who is adding this class?" bugs.

Copying useful things

Right-click a node and open Copy to get:

  • Copy selector: a CSS selector that matches the node, for test scripts.
  • Copy JS path: a document.querySelector(...) expression, including shadow roots.
  • Copy XPath and Copy full XPath.
  • Copy outerHTML: the node's HTML text.
  • Copy styles: the CSS that applies to it.

Copy selector is handy but not always good: it may produce a fragile chain like body > div:nth-child(2) > ul > li:nth-child(3). Prefer a hand-written selector that uses a stable class or data- attribute.

Stand-in: walking a DOM tree

Here is a small model of a DOM tree. It prints the tree with indentation, as the Elements panel does, and builds a simple selector path for a node. It is plain JavaScript data, not the real DOM.

JS Stand-in: print a DOM tree and its selector path

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The printed selector is a path, much like the one Copy selector builds. Real selectors also add :nth-child() when siblings are alike.

Stand-in: find elements like Ctrl+F does

The search box accepts selectors. This second stand-in finds nodes by tag, class or text across the same tree, then reports how many matched.

JS Stand-in: search the tree

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Shadow DOM, iframes and hidden nodes

  • Shadow DOM: expand #shadow-root to see inside. Use Settings, Preferences, Elements, Show user agent shadow DOM to reveal the built-in structure of controls.
  • Iframes appear as <iframe> nodes with their own #document inside. Cross-origin frames are shown, but selecting inside them changes which context the Console uses.
  • Hidden nodes are not removed: display: none, visibility: hidden and hidden attributes keep the node in the tree. If an element is missing visually, first check that it exists in Elements, then check why it is invisible.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can the Elements panel show different HTML than 'View page source'?

    View source shows the original text from the server; Elements shows the current DOM.
  2. 2. What is the quickest way to find who adds a class to a node?

    A DOM breakpoint pauses execution on the line of JavaScript that changes the attribute.
  3. 3. What does '== $0' next to a node mean?

    $0 refers to the most recently selected element.
  4. 4. Which edits survive a page reload?

    Reloading rebuilds the DOM from source, so live edits are discarded.

Summary

  • Elements shows the live DOM, which may differ from the source HTML.
  • Select with the Inspect arrow, right-click, arrow keys or Ctrl+F (text, CSS selector or XPath).
  • Edit attributes, tags and markup temporarily, and use Break on to catch scripts that change nodes.
  • Copy selector and Copy outerHTML help, but prefer stable, hand-written selectors.

Next: The Styles Pane and the CSS Cascade