Chrome DevTools: Elements — Debugging

The Elements panel in DevTools shows the page's HTML and CSS, and you can change both right there. Right-click, Inspect is the fastest way to open it with the…

You write .heading { color: #2563eb; }, save, refresh. The heading stays pale grey. You change the colour, refresh again, still grey. Instead of guessing again, right-click the heading and choose Inspect. In the Styles pane, your color line is crossed out: another rule won.

The Styles pane is part of Chrome DevTools. Other browsers based on Chromium (built from the same code as Chrome), such as Edge, have the same DevTools. The Elements panel in DevTools shows two things: the structure of the page that is on screen now, and the CSS of each element. You can change both right there.

Think of the Elements panel as a floor plan you can draw on in pencil. Rooms inside rooms are elements inside elements. Measuring the walls is like checking the size of an element, and trying to move the furniture is like trying different CSS. But like pencil marks that are easy to erase, your changes in Elements disappear as soon as you refresh. So you have to copy any change that works into the real code.

Opening the Elements panel

The DOM tree: the page as it lives now

The example page:

<html>
  <body>
    <main>
      <section class="hero">
        <h1 id="title" class="heading">Learning Debugging</h1>
        <a class="btn" href="/start">Start learning</a>
      </section>
    </main>
  </body>
</html>

Elements shows this page as the DOM tree: the tree of elements the browser built. You can fold and unfold each element that has contents with the triangle on its left. The DOM tree is the page's state right now, including elements JavaScript added after the page loaded. View Page Source (right-click the page, then View Page Source), on the other hand, only shows the original HTML file.

The selected element is marked == $0 next to it. $0 is the name for that element in the Console, and the next lesson covers it. Along the bottom of the panel runs the breadcrumb: the path from the root (the outermost element, html) to the selected element, such as html body main section.hero h1#title.heading. To change a text or an attribute, double-click it in the DOM tree. You can also right-click the element and choose Edit as HTML.

Finding an element with a selector

Press Ctrl+F (Mac: Cmd+F) inside Elements, then type a text or a CSS selector. #title looks for an id, .hero for a class, and h1 for a tag name. The first element that matches is selected right away.

The exercise copies this. In the exercise, the page is written as { tag, id, class, children } objects. The function you write, inspectElement(tree, selector), returns the path of tag names from the root to the first element that matches, like the breadcrumb:

Selector What matches The path
#title the h1 with id title html>body>main>section>h1
.hero the section with class hero html>body>main>section
h1 the first h1 tag html>body>main>section>h1
#nope nothing [], length 0

The code below uses two functions. matches checks whether one node (one element in the tree) matches the selector. find walks through the tree and records the path:

function matches(node, selector) {
  if (selector.startsWith("#")) return node.id === selector.slice(1);
  if (selector.startsWith(".")) return node.class === selector.slice(1);
  return node.tag === selector;
}

function find(node, selector, path) {
  const newPath = [...path, node.tag];         // the path up to this node
  if (matches(node, selector)) return newPath; // found: stop
  for (const child of node.children || []) {   // not yet: check its children one by one
    const found = find(child, selector, newPath);
    if (found) return found;
  }
  return null;                                 // this branch doesn't have it
}

The Styles pane: which rule wins

Next to the DOM tree is the Styles pane. It lists every CSS rule that applies to the selected element, with the strongest rule at the top. For the heading above, it holds these two rules:

.hero h1 { color: #cbd5e1; }   /* wins: 1 class + 1 tag */
.heading { color: #2563eb; }   /* color crossed out: only 1 class */

When two rules set the same property, the winner is the rule whose selector is more specific: an id beats a class, and a class beats a tag. .hero h1 has one class plus one tag, while .heading has only one class. So the color in .heading loses and is crossed out. That's why the heading stayed grey: your rule was read, but another rule beat it.

What you can do in Styles:

Computed and the box model

The Computed tab shows the final values after every rule has competed, for example color: rgb(203, 213, 225). That is the pale grey #cbd5e1 from .hero h1, written as rgb. Above them is the box model picture: layered boxes of margin, border, padding, then the content in the middle. Hover over each layer and that part is highlighted on the page. Take the "Start learning" button with padding: 10px 18px and border: 2px solid. Its padding and border appear exactly as the padding layer and the border layer in that picture.

Changes in DevTools are only temporary

Everything you change in Elements and Styles disappears when the page reloads. The reason: you only changed the browser's copy of the page, not the file. Use DevTools to try things until they fit, then copy the change into your CSS or HTML file.

There are two other options when your change must not disappear. Both are in the Sources panel, the panel that shows the page's code files:

For small changes, copying by hand is still the simplest way.

This exercise also peeks at Network

In the second part of the exercise, you summarise a list of requests (what the browser asks a server for), like the ones you'll see in the Network panel later. Each request has a status code, a three-digit number in the server's answer. You count the successful requests as ok and the failed ones as failed:

From the five example requests (200, 200, 401, 200, 404), total is 5, ok 3 and failed 2. byType counts the requests of each kind (type, such as fetch or img), with the kind's name as the key. For each request r: byType[r.type] = (byType[r.type] || 0) + 1. When a kind has not been counted yet, || 0 starts its count at 0. The example has 2 fetch and 2 img, and other kinds such as xhr are counted the same way.

⚠️ Wrong ideas about Elements, Styles and status codes

TL;DR: The Elements panel in DevTools shows the page's HTML and CSS, and you can change both right there. Right-click, Inspect is the fastest way to open it with the element you clicked already selected (or Ctrl+Shift+C for inspect mode, where you pick an element with the cursor). The DOM tree (the tree of elements) shows the page as it is now, including what JavaScript did. The breadcrumb along the bottom of the panel shows the path from html to the selected element, such as html body main section.hero h1#title.heading. Ctrl+F searches by selector: #id, .class, or a tag, and the first match gets selected. The Styles pane shows the strongest CSS rule at the top, and a property that loses is crossed out. The Computed tab shows the final values and the box model. Every change in DevTools is gone on refresh. Status codes (the number in the server's answer): 2xx success, 3xx redirect or cache (the copies the browser keeps), 400 and up failed.

⚡ Right-click, Inspect

Meaning: The Elements panel in DevTools (the browser's built-in developer tools) shows the page's HTML and CSS. The fastest way to open it: right-click an element on the page, then Inspect. DevTools opens with that element already selected. In the Elements panel you can see and change the HTML and CSS right in the browser.

⚡ The DOM tree: the page as it lives now

Write:

<section class="hero">
  <h1 id="title" class="heading">Learning Debugging</h1>
</section>

Meaning: The DOM tree is the tree of the page's elements as it is right now, including elements JavaScript added. The breadcrumb along the bottom of the Elements panel shows the path from html to the selected element, such as html body main section.hero h1#title.heading. Ctrl+F in the Elements panel searches for an element by selector: #title, .hero or h1.

⚡ Crossed out means it lost

Write:

.hero h1 { color: #cbd5e1; }   /* wins: 1 class + 1 tag */
.heading { color: #2563eb; }   /* color crossed out: only 1 class */
Guess first: <h1 class="heading"> is inside .hero, so both rules set its colour. What colour is the heading?

Pale grey, #cbd5e1. .hero h1 has one class plus one tag, so it is more specific than .heading with only one class. The .heading rule is still read, but it loses.

Meaning: The Styles pane (next to the DOM tree) shows the CSS rules for the selected element. The strongest rule is at the top, and a property that loses is crossed out.

⚡ Changes in DevTools are only temporary

Meaning: Everything you change in Elements and Styles disappears on refresh, because you only changed the browser's copy of the page. Try things until they fit, then copy the change into your CSS or HTML file.

What you will learn