Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 122 ยท Getting Started

Viewing Source and Using DevTools

Learn the difference between a page's source and its live DOM, inspect and edit elements in DevTools, use the console, test mobile sizes and spot repaired HTML.

Every web browser comes with two tools that let you look behind any page: View Source, which shows the HTML file exactly as it arrived, and developer tools (DevTools), which show what the browser built from that file and let you change it live. Professional developers keep DevTools open almost all day.

In this lesson you will learn how to open both, what the difference between them is, and how to use DevTools to understand โ€” and debug โ€” your own pages. You do not need to install anything: Chrome, Edge, Firefox and Safari all include them.

View Source: the file as delivered

Right-click an empty part of any page and choose View Page Source, or use the keyboard:

Browser Shortcut
Chrome, Edge, Firefox (Windows/Linux) Ctrl + U
Chrome, Edge (Mac) Cmd + Option + U
Firefox (Mac) Cmd + U
Safari (Mac) Cmd + Option + U

A new tab opens with the raw HTML text the server sent. It is read-only, and it never changes: if JavaScript later adds a menu or loads comments, those additions do not appear here.

View Source is great for quick questions such as "which stylesheet does this page load?" or "what is in the <head>?". Use Ctrl + F (Cmd + F on a Mac) to search it.

DevTools: the live page

To open DevTools, press F12 or Ctrl + Shift + I on Windows and Linux, or Cmd + Option + I on a Mac. Even faster: right-click any part of a page and choose Inspect. DevTools opens with that exact element selected.

Safari users: first turn on the developer features under Settings โ†’ Advanced โ†’ "Show features for web developers". After that, Inspect appears in the right-click menu.

DevTools is a panel with several tabs. The names differ slightly between browsers, but the important ones are the same everywhere:

  • Elements (called Inspector in Firefox) โ€” the page's structure, with its styles.
  • Console โ€” messages from the page, and a place to run JavaScript.
  • Network โ€” every file the page downloaded, with sizes and timings.

This lesson focuses on the first two. You will meet the others later, for example in Fast-Loading HTML.

Source vs DOM: why they differ

When a browser loads a page, it reads the HTML text and builds a tree of objects in memory called the DOM (Document Object Model). The page you see is drawn from that tree, not from the text. The Elements panel shows the DOM, so it can look different from View Source in two ways:

  1. JavaScript may have changed it. Scripts can add, remove or edit elements at any time. The Elements panel shows the current state; View Source shows only the starting point.
  2. The browser may have repaired it. HTML is forgiving. If you forget a closing tag or nest elements the wrong way, the browser does not give up โ€” it follows precise repair rules and builds a valid tree anyway.

You will learn to work with the DOM from JavaScript in The Document Object Model. For now, the key idea is: View Source shows what you wrote; the Elements panel shows what the browser understood.

How browsers repair broken HTML

The example below contains three mistakes. The first paragraph is never closed, the <b> and <i> tags overlap instead of nesting, and the table has no <tbody>. Run it and read what the browser actually built:

See the repaired HTML

Look closely at the output:

  • The first <p> was closed automatically when the second <p> started.
  • The overlapping <b> and <i> were split so that every element is properly nested โ€” there are now two <i> elements.
  • A <tbody> appeared inside the table, because table rows always live in a table section.

Every modern browser applies the same repairs, because the HTML standard defines them exactly. But repairs are guesses about what you meant, and they are not always the guess you wanted. When a page looks wrong, compare your code with the Elements panel: the difference often points straight at the mistake. The Validating Your HTML lesson shows how to catch these errors before they reach a browser.

Inspecting and editing elements

In the Elements panel, the DOM is shown as an expandable tree. As you hover over a line, the matching part of the page is highlighted, with its content, padding, border and margin shown in different colors. Next to the tree, the Styles pane lists every CSS rule that applies to the selected element.

You can change almost anything, and the page updates instantly:

  • Edit text: double-click the text inside an element and type.
  • Edit an attribute: double-click an attribute name or value. Try changing an image's src or a link's href.
  • Edit as HTML: right-click an element and choose Edit as HTML to rewrite a whole block.
  • Delete or hide: select an element and press Delete, or press H in Chrome and Edge to toggle its visibility.
  • Move: drag an element to a new place in the tree.
  • Change styles: click a CSS value in the Styles pane to edit it, or click the checkbox next to a declaration to switch it off.

Your edits are temporary. DevTools changes only the copy of the page in your browser's memory. Reload and everything is back to normal. That makes DevTools a perfect, risk-free playground: try ideas there, then copy what works into your real files.

Try it on this page: right-click this paragraph, choose Inspect, double-click the text in the Elements panel and rewrite it. Nobody else will see your change.

Console basics

The Console tab does two jobs. First, it shows messages: errors from broken scripts, warnings from the browser (for example about a missing image), and anything your own code prints with console.log(). Second, it is a command line where you can type JavaScript and press Enter to run it immediately.

A few useful commands to type into the console of any page:

JavaScript
document.title                         // the page's title
document.querySelectorAll('a').length  // how many links it has
document.body.style.background = 'pink'
$0                                     // the element selected in the Elements panel

The last one is a DevTools shortcut: $0 always refers to whichever element you last selected, so you can inspect something visually and then work with it in code.

Every TryIt editor on this site has its own small console under the preview, which receives the same kinds of messages. Run this example and compare the three message levels:

Console messages

The last line produces a ReferenceError. Reading error messages like this one is a big part of debugging: the console tells you what went wrong and, in the real DevTools, which line caused it.

Inspecting TryIt previews: each preview runs in its own sandboxed frame. To use the full browser console on it, open DevTools and pick the preview frame from the context menu at the top of the Console tab (it usually says top).

The console object has more tools than log, warn and error:

  • console.table(array) prints an array of objects as a table; a second argument chooses the columns (console.table(rows, ["name", "age"])).
  • console.group("Title") and console.groupEnd() indent the messages between them, and console.groupCollapsed("Title") starts the group folded so the output stays tidy until you open it.
  • console.assert(condition, "message") prints only when the condition is false.
  • console.count("label") counts how often a line ran, and console.time("t") with console.timeEnd("t") measures how long code takes.
Table and groups

Testing other screen sizes

Most visitors will see your pages on a phone. You do not need one to test: DevTools can simulate one.

  • Chrome and Edge: with DevTools open, click the phone-and-tablet icon (or press Ctrl + Shift + M, Cmd + Shift + M on a Mac) to show the device toolbar.
  • Firefox: open Responsive Design Mode with Ctrl + Shift + M (Cmd + Option + M on a Mac).
  • Safari: choose Develop โ†’ Enter Responsive Design Mode.

You can then pick a preset device, type an exact width and height, rotate the screen and simulate touch. It is a good approximation, though not a perfect one: real phones have different fonts, real fingers and real network speeds. You will put this to good use in Responsive Web Design.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A script on a page adds a cookie banner after it loads. Where can you see the banner's HTML?

    View Source shows the original file from the server. Elements added later by JavaScript only exist in the live DOM, which the Elements panel shows.
  2. 2. You change a heading's text in the Elements panel. What happens when you reload the page?

    DevTools edits only the in-memory copy of the page. To keep a change, copy it into your source file.
  3. 3. You wrote <p>One<p>Two without closing tags. What does the Elements panel show?

    Browsers repair HTML using rules from the standard. A new p start tag automatically closes the open paragraph.
  4. 4. In the DevTools console, what does $0 refer to?

    $0 is a DevTools shortcut for the most recently selected element, which lets you jump from visual inspection to JavaScript.

Summary

  • View Source shows the HTML file exactly as the server sent it; it never changes.
  • DevTools (F12 or right-click โ†’ Inspect) shows the live DOM, which may differ because of JavaScript or automatic repairs.
  • Browsers fix broken HTML with standard rules โ€” closing paragraphs, splitting misnested tags, adding <tbody>.
  • In the Elements panel you can edit text, attributes and styles; all changes vanish on reload.
  • The Console shows messages and runs JavaScript; $0 is the selected element.
  • The device toolbar or responsive mode simulates phone and tablet screens.

Next: Elements and Tags โ€” a closer look at the building blocks you have been inspecting.