Loading...

HTML essentialsAdvanced

Common mistakes & debugging HTML

5 min lesson

Next
  • Debugging HTML means finding and fixing the mistakes that make a page render wrongly or not at all.
  • Common causes are unclosed tags, wrong nesting, misspelled attributes, duplicate ids and wrong file paths.
  • DevTools Elements panel shows the real DOM the browser built, which reveals nesting errors immediately.
  • The Console shows errors, like a missing script or a failed image request.
  • Think of debugging as tracing a broken bridge: follow the path from HTML to DOM and find the missing plank.
  • This builds on Performance and Security: debugging verifies that your optimizations and safety work in the real page.
  • The example includes a deliberate typo you can spot with DevTools; fixing it makes the alert appear.
  • This finishes the course; you can now build, style, protect and debug real HTML pages.
Keep in mind
  • Open DevTools first; the DOM tree shows what the browser really built.
  • Fix unclosed tags and wrong nesting before anything else.
  • Check the Console for red errors; they often point straight to the bug.
  • Fix one problem at a time and reload after each change.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Debugging</title>
</head>
<body>
  <!-- The paragraph below has an unclosed tag on purpose -->
  <p>This paragraph is <strong>missing its closing tags.

  <!-- The script below expects an element with id="msg" -->
  <p id="msg">No message.</p>

  <script>
    // If this errors, check the id in the markup above
    document.getElementById('msg').textContent = 'Debugging works.';
  </script>
</body>
</html>

What happens: The browser shows the paragraph text with the bold part extending further than expected, then "Debugging works." in the second paragraph. DevTools Elements shows that the browser auto-closed the tags in unexpected places, which is the bug to fix.

Breaking it down

Elements panel

DevTools > Elements

Shows the real DOM the browser built. Unexpected nesting there reveals the actual bug in your HTML.

Console

DevTools > Console

Lists errors like 'null is not an object'. The message often names the missing element.

Network panel

DevTools > Network

Shows file loads and their status codes. A 404 tells you a file path is wrong.

validator

validator.w3.org

Reports unclosed tags, duplicate ids and invalid nesting with exact line numbers.

Debugging tools

ToolWhat to look for
ElementsWrong nesting and auto-closed tags
ConsoleJavaScript errors and warnings
NetworkFailed file loads (404, 500)
ValidatorMarkup errors with line numbers
SourcesBreakpoints and script behavior

A helpful analogy

Debugging is like untangling a necklace: find the knot, loosen it a little, and follow the chain until it is free. Rushing or yanking breaks it more.

Nesting rules

  • 1.If a paragraph is breaking strangely, check for an unclosed tag just before it.
  • 2.Fix one error at a time; a single unclosed tag can cause many visible problems.

Common mistakes

Ignoring an unclosed tag because the page looks fine

Avoid

<p>text <strong>bold

Use instead

<p>text <strong>bold</strong></p>

The browser guesses where to close tags, which can break layout and accessibility in subtle ways.

Using the wrong id in JavaScript

Avoid

getElementById('massage') when the id is 'message'

Use instead

getElementById('message')

A typo returns null, and the next line throws an error. The Console shows exactly this.

Quick tips

  • •Right-click a broken area and choose Inspect; DevTools jumps straight to that element.
  • •After fixing, reload the page; a cached version can hide your correction.
Previous
Next