Get Started With Viewing And Changing The DOM
Complete these interactive tutorials to learn the basics of viewing and changing a page's DOM using Chrome DevTools.
This tutorial assumes that you know the difference between the DOM and HTML. See Appendix: HTML versus the DOM for an explanation.
View DOM nodes
The DOM Tree of the Elements panel is where you do all DOM-related activities in DevTools.
Inspect a node
When you're interested in a particular DOM node, Inspect is a fast way to open DevTools and investigate that node.
Right-click Michelangelo below and select Inspect.
The Elements panel of DevTools opens.
<li>Michelangelo</li>is highlighted in the DOM Tree.
Click the Inspect icon in the top-left corner of DevTools.
Click the Tokyo text below.
<li>Tokyo</li>is highlighted in the DOM Tree.
Inspecting a node is also the first step towards viewing and changing a node's styles. See Get Started With Viewing And Changing CSS.
Navigate the DOM Tree with a keyboard
Once you've selected a node in the DOM Tree, you can navigate the DOM Tree with your keyboard.
Right-click Ringo below and select Inspect.
<li>Ringo</li>is selected in the DOM Tree.
Press the Up arrow key 2 times.
Press the Left arrow key. The
Press the Left arrow key again. The parent of the
<ul>node is selected. In this case it's the
<li>node containing the instructions for step 1.
Press the Down arrow key 2 times so that you've re-selected the
<ul>list that you just collapsed. It should look like this:
Press the Right arrow key. The list expands.
Scroll into view
When viewing the DOM Tree, sometimes you'll find yourself interested in a DOM node that's not currently in the viewport. For example, suppose that you scrolled to the bottom of the page, and you're interested in the
<h1> node at the top of the page. Scroll into view lets you quickly reposition the viewport so that you can see the node.
Right-click Magritte below and select Inspect.
Go to the Appendix: Scroll into view section at the bottom of this page. The instructions continue there.
After completing the instructions at the bottom of the page you should jump back up to here.
Search for nodes
You can search the DOM Tree by string, CSS selector, or XPath selector.
Focus your cursor on the Elements panel.
Press Control+F or Command+F (Mac). The Search bar opens at the bottom of the DOM Tree.
The Moon is a Harsh Mistress. The last sentence is highlighted in the DOM Tree.
As mentioned above, the Search bar also supports CSS and XPath selectors.
Edit the DOM
You can edit the DOM on the fly and see how those changes affect the page.
To edit a node's content, double-click the content in the DOM Tree.
Right-click Michelle below and select Inspect.
In the DOM Tree, double-click
Michelle. In other words, double-click the text between
</li>. The text is highlighted blue to indicate that it's selected.
Leela, then press Enter to confirm the change. The text above changes from Michelle to Leela.
To edit attributes, double-click the attribute name or value. Follow the instructions below to learn how to add attributes to a node.
Right-click Howard below and select Inspect.
<li>. The text is highlighted to indicate that the node is selected.
Press the Right arrow key, add a space, type
style="background-color:gold", and then press Enter. The background color of the node changes to gold.
Edit node type
To edit a node's type, double-click the type and then type in the new type.
Right-click Hank below and select Inspect.
<li>. The text
button, then press Enter. The
<li>node changes to a
Reorder DOM nodes
Drag nodes to reorder them.
Right-click Elvis Presley below and select Inspect. Notice that it's the last item in the list.
- Stevie Wonder
- Tom Waits
- Chris Thile
- Elvis Presley
In the DOM Tree, drag
<li>Elvis Presley</li>to the top of the list.
You can force nodes to remain in states like
Hover over The Lord of the Flies below. The background color becomes orange.
- The Lord of the Flies
- Crime and Punishment
- Moby Dick
Right-click The Lord of the Flies above and select Inspect.
<li class="demo--hover">The Lord of the Flies</li>and select Force State > :hover. See Appendix: Missing options if you don't see this option. The background color remains orange even though you're not actually hovering over the node.
Hide a node
Press H to hide a node.
Right-click The Stars My Destination below and select Inspect.
- The Count of Monte Cristo
- The Stars My Destination
Press the H key. The node is hidden.
Press the H key again. The node is shown again.
Delete a node
Press Delete to delete a node.
Right-click Foundation below and select Inspect.
- The Illustrated Man
- Through the Looking-Glass
Press the Delete key. The node is deleted.
Press Control+Z or Command+Z (Mac). The last action is undone and the node reappears.
Access nodes in the Console
Reference the currently-selected node with $0
When you inspect a node, the
== $0 text next to the node means that you can reference this node in the Console with the variable
Right-click The Left Hand of Darkness below and select Inspect.
- The Left Hand of Darkness
Press the Escape key to open the Console Drawer.
$0and press the Enter key. The result of the expression shows that
<li>The Left Hand of Darkness</li>.
Hover over the result. The node is highlighted in the viewport.
<li>Dune</li>in the DOM Tree, type
$0in the Console again, and then press Enter again. Now,
Store as global variable
If you need to refer back to a node many times, store it as a global variable.
Right-click The Big Sleep below and select Inspect.
- The Big Sleep
- The Long Goodbye
<li>The Big Sleep</li>in the DOM Tree and select Store as global variable. See Appendix: Missing options if you don't see this option.
temp1in the Console and then press Enter. The result of the expression shows that the variable evaluates to the node.
Copy JS path
Right-click The Brothers Karamazov below and select Inspect.
- The Brothers Karamazov
- Crime and Punishment
<li>The Brothers Karamazov</li>in the DOM Tree and select Copy > Copy JS Path. A
document.querySelector()expression that resolves to the node has been copied to your clipboard.
Press Control+V or Command+V (Mac) to paste the expression into the Console.
Press Enter to evaluate the expression.
Break on DOM changes
That covers most of the DOM-related features in DevTools. You can discover the rest of them by right-clicking nodes in the DOM Tree and experimenting with the other options that weren't covered in this tutorial. See also Elements panel keyboard shortcuts.
Check out the Chrome DevTools homepage to discover everything else you can do with DevTools.
See Community if you want to contact the DevTools team or get help from the DevTools community.
Appendix: HTML versus the DOM
This section quickly explains the difference between HTML and the DOM.
When you use a web browser to request a page like
https://example.com the server returns HTML like this:
<p>This is a hypertext document on the World Wide Web.</p>
<script src="/script.js" async></script>
The browser parses the HTML and creates a tree of objects like this:
This tree of objects, or nodes, representing the page's content is called the DOM. Right now it looks the same as the HTML, but suppose that the script referenced at the bottom of the HTML runs this code:
const h1 = document.querySelector('h1');
const p = document.createElement('p');
p.textContent = 'Wildcard!';
That code removes the
h1 node and adds another
p node to the DOM. The complete DOM now looks like this:
See Introduction to the DOM to learn more.
Appendix: Scroll into view
This is a continuation of the Scroll into view section. Follow the instructions below to complete the section.
<li>Magritte</li>node should still be selected in your DOM Tree. If not, go back to Scroll into view and start over.
<li>Magritte</li>node and select Scroll into view. Your viewport scrolls back up so that you can see the Magritte node. See Appendix: Missing options if you can't see the Scroll into view option.
Appendix: Missing options
Many of the instructions in this tutorial instruct you to right-click a node in the DOM Tree and then select an option from the context menu that pops up. If you don't see the specified option in the context menu, try right-clicking away from the node text.