{module 2}
A first look at the developer tools
How the web works, and your tools20 min read
What you will be able to do
- Open the developer tools in Chrome, Edge or Firefox, and find the Elements panel, the Console and the device toolbar.
- Read the page as the browser understood it in the Elements panel, and find where it differs from your file.
- Find an error message in the Console, copy it exactly, and use it to fix a missing file.
Why it matters
Mekong Bikes hires out bicycles by the river in Phnom Penh, Cambodia. Its owner, Sokha Chan, added a picture of a bike to the page, so that tourists could see what they would ride. A week later, a customer mentioned that the page had no picture. Sokha looked at the file. The line for the picture was there. He looked at the picture file. It was there too.
The answer was in a panel he had never opened: the browser was asking for bikes.svg, and the file was called bike.svg.
Browsers have built-in tools that show what they did with your page and what went wrong. This lesson opens them.
The idea
The developer tools
The developer tools are panels inside your browser for looking at a page from the inside. You met the Network panel in lesson 1 and the Console in orientation.
To open them on Windows or Linux, press F12 or Ctrl+Shift+I. On a Mac, press Cmd+Option+I. This works in Chrome, Edge and Firefox. Press the same keys again to close them. You can also right-click anything on a page and choose Inspect, which opens the tools with that part of the page selected.
A row of tabs across the top of the tools has one tab for each panel.
The Elements panel: the page as the browser understood it
In lesson 3 you saw that the browser guesses when your HTML has a mistake. The Elements panel shows the result of its guesses: every element on the page, as the browser built it. In Firefox this panel is called the Inspector.
The panel shows elements as a tree. An element inside another is shown below it, further to the right, and a small triangle beside it opens and closes it. Move the mouse over an element in the panel, and the browser highlights it on the page.
To open it directly, press Ctrl+Shift+C (Cmd+Option+C on a Mac) in Chrome, Edge or Firefox, then click a part of the page.
The Elements panel is not your file. It is what the browser made from your file. When the page looks wrong, compare the two.
The Console: where errors appear
The Console is where the browser writes messages for the people who build pages. When the page asks for a file that is not there, the browser writes an error here. The visitor sees nothing.
- In Chrome and Edge, press Ctrl+Shift+J (Cmd+Option+J on a Mac).
- In Firefox, press Ctrl+Shift+K (Cmd+Option+K on a Mac).
Errors are shown in red. Reload the page with the Console open, to see every message.
The device toolbar: the page at phone width
The device toolbar shows the page about as wide as a phone screen.
- In Chrome, open the tools and click Toggle device toolbar, a button at the top of the tools. Or press Ctrl+Shift+M (Cmd+Shift+M on a Mac) while the tools are open.
- In Edge, open the tools and click Toggle device emulation in the bar at the top.
- In Firefox, press Ctrl+Shift+M (Cmd+Option+M on a Mac). Firefox calls it Responsive Design Mode.
A bar appears above the page with a width and a height. Type 390 in the width box, the width of a common phone. It is an approximation: a real phone is the final test.
Your pages may look very small in this view. A phone assumes that a page without one particular line at the top was made for a wide screen, and shrinks it. Module 2 adds that line, and module 3 makes pages fit a phone.
Worked example
Here is Sokha's page. The picture is new: img is an element that shows a picture. Module 2 teaches it. Today it is here because it asks the browser for a second file. Make a folder for this lesson in free-track, called mekong, and save this in it as index.html:
<h1>Mekong Bikes</h1>
<p>Bicycle hire by the river in Phnom Penh.</p>
<img src="bikes.svg" alt="A blue city bicycle">
<h2>Prices</h2>
<p>Half a day, $4</p>
<p>A full day, $7</p>The picture file is in the same folder. To make it, create a file there called bike.svg and copy into it the drawing below. You do not need to read it: SVG is a way to describe a drawing as text.
<svg xmlns="http://www.w3.org/2000/svg" width="240" height="140" viewBox="0 0 240 140">
<circle cx="55" cy="95" r="35" fill="none" stroke="#1d4e89" stroke-width="6"/>
<circle cx="185" cy="95" r="35" fill="none" stroke="#1d4e89" stroke-width="6"/>
<path d="M55 95 L95 45 L160 45 L185 95 M95 45 L120 95 L160 45 M120 95 L55 95 M88 30 L108 30 M150 45 L155 25 L172 25" fill="none" stroke="#1d4e89" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
</svg>Open index.html in the browser. The text is all there, but there is no picture of a bike. Here is what the checker that tested this lesson saw. It prints the text of the page, then each error the page caused, after the words page error::
Mekong Bikes
Bicycle hire by the river in Phnom Penh.
Prices
Half a day, $4
A full day, $7
page error: Failed to load resource: net::ERR_FILE_NOT_FOUNDOpen the Console and reload. In Chrome and Edge, you see the same message in red: Failed to load resource: net::ERR_FILE_NOT_FOUND. At the right end of the line, the Console names the file it could not find, bikes.svg. Firefox words its message differently, and names the same file.
Read the message. Failed to load resource means the browser asked for a file and did not get it. ERR_FILE_NOT_FOUND means there is no file with that name in that place. As in lesson 1, a code that starts with ERR_ comes from the browser.
Open the Elements panel. Click the triangles to open the tree. The img element is there, with src="bikes.svg": the name of the file the page asks for. The folder holds bike.svg, with no s.
The tree also shows three elements your file does not have: html, head and body. The browser added them, because every page has them. Counting each one, the checker finds one of each, and the same error again:
1
1
1
page error: Failed to load resource: net::ERR_FILE_NOT_FOUNDFix it. In VS Code, change the line in index.html to:
<img src="bike.svg" alt="A blue city bicycle">Save and reload. The bike appears, and the Console has no new error.
Look at it at phone width. Open the device toolbar and set the width to 390. Either the text wraps onto more lines, or the whole page is shrunk and looks small, for the reason given in "The idea".
Common mistakes
Trusting an empty Console
Here is a second page from Sokha, with a mistake from lesson 3:
<h1>Mekong Bikes</h1>
<h2>Prices<h2>
<p>Half a day, $4</p>
<p>A full day, $7</p>Both prices show as large, bold headings, and the Console is empty. A mistake in HTML causes no error, because the browser guesses and goes on. The Elements panel shows the result. Here are the number of h2 elements, and the number of paragraphs inside an h2:
2
2Two headings from a file with one, and both paragraphs inside a heading. In the Elements panel, the two p elements are indented under the second h2. When the page looks wrong and the Console is empty, open the Elements panel.
Fixing the page in the developer tools
In the Elements panel you can double-click text and change it, and the page changes at once. This is useful for trying an idea. It does not change your file. Reload, and your change is gone. Make the real change in VS Code, save and reload.
Think it through
Nadia, a learner, has written a page for a juice stall. Its title should be a large heading. On the screen it is ordinary text, the same size as the paragraph below it. The Console is empty.
Understand. No file is missing, because the Console has no error. So the browser read her HTML differently from what she meant.
Break down. What does the file say? What did the browser build? Where do the two differ?
Plan. Open the Elements panel, find the title in the tree, and compare its tag with the file, one character at a time.
Build. In the tree there is no h1. In its place is an element called hl. In VS Code, the file says <hl> too. In her editor's font, the letter l and the number 1 look almost the same.
Test. Her guess: the browser did not know the name hl, so it showed plain text. She types the number 1 in both tags, saves and reloads. The title is large and bold, and the tree shows h1. As an odd case, she checks the Console: still empty, because the browser never reports an unknown tag.
Reflect. The Console finds missing files. The Elements panel finds HTML that the browser read differently. Each tool answers its own question.
At work
A good question copies the message instead of describing it. "There was an error about loading" loses the two facts that matter: the code and the name of the file. The exact words let the reader search for them, and they often hold a name or a number you did not think mattered. In the Console, select the message and copy it.
Here is how Sokha could have asked a friend:
The picture on the Mekong Bikes page does not appear. The Console in Chrome shows this, in red, when I reload:
Failed to load resource: net::ERR_FILE_NOT_FOUNDOn the right it says
bikes.svg. The picture file in my folder is calledbike.svg. What does this message mean?
His friend would answer in one line. More likely, Sokha would see the answer himself while he wrote the two names next to each other.
Your tasks
Task 1: Open each tool
On any web page, open the developer tools and find the Elements panel (the Inspector in Firefox), the Console and the device toolbar.
You are done when you can open the Elements panel and the Console with their own keys, without looking them up.
Task 2: Mend Sokha's page
Make the mekong folder with index.html and bike.svg, as in the worked example. Open it, find the error in the Console, then fix the name and reload.
You are done when you have copied the exact message from the Console into your notes, and the bike appears with no new error.
Task 3: Find what the browser added
Open your menu page from lesson 3 with the Elements panel open. Find the elements the browser added that your file does not have.
You are done when you can name the three elements that the browser added, and say which of them holds your headings and paragraphs.
Task 4: Two mistakes, two tools
Make a copy of your menu page. In it, remove the slash from the closing tag of one h2. Then add a picture line that asks for a file that does not exist, such as <img src="stall.svg" alt="Our stall">. Before you open it, write down which tool will show each mistake.
You are done when you have found each mistake in the tool you predicted, or written down why your prediction was wrong.
Recap
- F12, or Ctrl+Shift+I (Cmd+Option+I on a Mac), opens the developer tools in Chrome, Edge and Firefox.
- The Elements panel (Inspector in Firefox) shows the page as the browser understood it, which may differ from your file.
- The Console shows errors, such as a file the page asks for that does not exist. HTML mistakes do not appear there.
- The device toolbar shows the page at phone width, as an approximation.
- Copy a message exactly. Do not describe it.
Go deeper
- Chrome DevTools: Open Chrome DevTools lists every way to open the tools and each panel, with the keys for each system.
- Firefox: Keyboard shortcuts for the developer tools gives the keys for the Inspector, the Console and Responsive Design Mode.
- Microsoft Edge: Overview of DevTools shows where each tool is in Edge.
- Chrome DevTools: Simulate mobile devices with device mode explains the device toolbar and what it cannot simulate.