Skip to main content

Common HTML Mistakes & Debugging 🔍

Mentor's Note: Even experienced developers make HTML mistakes! The key isn't to avoid them completely, but to recognize them quickly and fix them efficiently. Think of debugging like being a digital detective — you're looking for clues to solve the mystery!

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Identify the most common HTML syntax and structure mistakes
  • Fix accessibility and performance issues in HTML
  • Use browser DevTools to debug HTML effectively
  • Apply validation tools to catch errors before they go live
  • Debug broken HTML using a systematic workflow

🤔 Why Do HTML Mistakes Happen?

HTML mistakes are incredibly common, even for seasoned developers. Here's why:

  • Typographical errors: A missing > or " can break an entire page
  • Misunderstanding semantics: Using wrong tags for the wrong purpose
  • Copy-paste syndrome: Reusing code without adapting it properly
  • Browser quirks: Different browsers handle errors differently
  • Rushing: Writing code too quickly without checking

The good news? Browsers are forgiving — HTML is designed to fail gracefully. But relying on that forgiveness leads to messy, hard-to-maintain code.


❌ Syntax Errors

Syntax errors are the most basic type of mistake. They happen when the HTML structure itself is invalid.

MistakeWhy It's WrongFix
Unclosed tags <p>TextBrowser doesn't know where the element endsAlways close: <p>Text</p>
Wrong nesting <b><i>Text</b></i>Crossed boundaries confuse the parserProper nesting: <b><i>Text</i></b>
Missing quotes <a href=link.html>Attribute values without quotes break on spacesQuote all values: <a href="link.html">
Wrong DOCTYPE <HTML> or missing entirelyBrowser may render in quirks modeUse <!DOCTYPE html>
Self-closing non-void tags <div />Only void elements (br, hr, img, input) can self-closeUse <div></div> or <br>

❌ Unclosed Tags

<!-- ❌ BROKEN: Missing closing tag -->
<p>This paragraph is never closed

<!-- ❌ BROKEN: Missing closing tag -->
<ul>
<li>Item one
<li>Item two
</ul>

<!-- ✅ FIXED: All tags properly closed -->
<p>This paragraph is properly closed</p>

<ul>
<li>Item one</li>
<li>Item two</li>
</ul>

❌ Wrong Nesting

<!-- ❌ BROKEN: Crossed nesting -->
<p>This is <strong>important <em>text</strong></em> in a paragraph.</p>

<!-- ✅ FIXED: Proper nesting -->
<p>This is <strong>important <em>text</em></strong> in a paragraph.</p>

❌ Missing Attribute Quotes

<!-- ❌ BROKEN: Missing quotes around attribute value -->
<img src=images/photo.jpg alt=A beautiful landscape>

<!-- ❌ BROKEN: Breaks when value has spaces -->
<a href=about us.html>About Us</a>

<!-- ✅ FIXED: Always quote attribute values -->
<img src="images/photo.jpg" alt="A beautiful landscape">
<a href="about-us.html">About Us</a>

🏗️ Structure Mistakes

Structure mistakes are about incorrect document organization, even when syntax is valid.

MistakeWhy It's WrongFix
Missing <head>No title, charset, or viewport metaAlways include <head> with <title>
Duplicate IDsIDs must be unique; JS breaksUse classes for repeated elements
Wrong heading hierarchyh1 → h3 skips levelsMaintain sequential order: h1 → h2 → h3
Block in inline element <span><div></div></span>Invalid nesting; browser moves elementsOnly inline elements inside <span>

❌ Duplicate IDs

<!-- ❌ BROKEN: Duplicate IDs -->
<div id="main-content">First section</div>
<div id="main-content">Second section</div>

<!-- ✅ FIXED: Unique IDs -->
<div id="main-content">First section</div>
<div id="secondary-content">Second section</div>

❌ Incorrect Heading Hierarchy

<!-- ❌ BROKEN: Skipped heading level -->
<h1>Page Title</h1>
<h3>Subsection</h3> <!-- should be h2 -->

<!-- ✅ FIXED: Proper hierarchy -->
<h1>Page Title</h1>
<h2>Major Section</h2>
<h3>Subsection</h3>

♿ Accessibility Mistakes

Accessibility mistakes make your content harder or impossible to use for people with disabilities.

MistakeWhy It's WrongFix
Missing alt textScreen readers can't describe imagesAlways add alt attribute (even alt="" for decorative)
Missing form labelsUsers can't identify input fieldsUse <label> or aria-label on every input
Wrong ARIA usageIncorrect roles confuse assistive techUse semantic HTML first; ARIA only when needed
Low color contrastText becomes unreadable for visually impairedEnsure 4.5:1 ratio for normal text
Keyboard trapsUsers can't navigate away with TabEnsure focusable elements are reachable and leaveable

❌ Missing Form Labels

<!-- ❌ BROKEN: No label for input -->
<input type="text" placeholder="Enter your name">

<!-- ✅ FIXED: Properly labeled -->
<label for="username">Full Name:</label>
<input type="text" id="username" name="username">

<!-- ✅ FIXED: Using aria-label when visual label isn't possible -->
<input type="search" aria-label="Search the site">

❌ Missing Alt Text

<!-- ❌ BROKEN: No alt text -->
<img src="chart.png">

<!-- ✅ FIXED: Descriptive alt text -->
<img src="chart.png" alt="Bar chart showing quarterly sales growth from Q1 to Q4">

<!-- ✅ FIXED: Decorative image with empty alt -->
<img src="decorative-border.png" alt="">

⚡ Performance Mistakes

MistakeWhy It's WrongFix
Missing loading="lazy"All images load immediately, even below the foldAdd loading="lazy" to below-fold images
Unoptimized imagesLarge images slow down page loadResize and compress images; use modern formats (WebP, AVIF)
Render-blocking scriptsJavaScript blocks HTML parsingAdd defer or async to <script> tags
No image dimensionsLayout shifts when images loadAlways set width and height attributes
Too many HTTP requestsMultiple CSS/JS files increase load timeBundle and minify assets

❌ Missing Lazy Loading

<!-- ❌ BROKEN: Below-fold image loads immediately -->
<img src="hero.jpg" alt="Hero image">

<!-- ✅ FIXED: Lazy loaded -->
<img src="hero.jpg" alt="Hero image" loading="lazy">

❌ Render-Blocking Scripts

<!-- ❌ BROKEN: Script blocks HTML parsing -->
<head>
<script src="app.js"></script>
</head>

<!-- ✅ FIXED: Script loads after HTML parses -->
<head>
<script src="app.js" defer></script>
</head>

<!-- ✅ FIXED: Script loads asynchronously -->
<head>
<script src="analytics.js" async></script>
</head>

🐛 How to Debug HTML

Debugging HTML follows a systematic process. When something looks wrong, don't panic — follow the flowchart:

Step-by-Step Debugging Process

  1. Open DevTools: Right-click → Inspect or press F12 / Ctrl+Shift+I
  2. Check the Console tab: Look for red error messages
  3. Inspect the Elements tab: See the actual rendered HTML structure
  4. Check Computed Styles: Ensure CSS is applying correctly
  5. Edit live: Double-click any element to edit text/attributes in place
  6. Verify: Refresh and confirm the fix works

🛠️ Browser DevTools for Debugging

Elements Panel

The Elements panel (called Inspector in Firefox) is your primary HTML debugging tool:

  • View the DOM tree: See how the browser parsed your HTML
  • Hover over elements: Highlights the corresponding page area
  • Edit HTML live: Double-click tags, attributes, or text to edit
  • Drag and drop: Reorder elements to test structural changes
  • Delete elements: Press Delete to remove a selected element

Live Editing Example

<!-- Original in your code -->
<p class="intro">Welcome to my site</p>

<!-- DevTools edit: double-click "Welcome" and type "Hello" -->
<p class="intro">Hello to my site</p>

<!-- You can also add/remove attributes in DevTools -->
<p class="intro" style="color: blue;">Hello to my site</p>

Checking Computed Styles

  • Click an element and look at the Styles sub-panel
  • See exactly which CSS rules apply (and which are overridden)
  • Toggle properties on/off to test changes without editing files

✅ Validation Tools

W3C HTML Validator

The official W3C Markup Validation Service checks your HTML against web standards:

  • Validate by URL: Enter a live page URL
  • Validate by file upload: Upload your .html file
  • Validate by direct input: Paste your HTML code

Common validation errors to watch for:

Validation ErrorTypical Cause
Element X not allowed as child of element YWrong nesting
Duplicate attributeSame attribute used twice on one element
Bad value for attributeInvalid attribute value
Stray end tagExtra or misplaced closing tag

Lighthouse in Chrome DevTools

Lighthouse provides performance, accessibility, SEO, and best-practice audits:

  1. Open DevTools → Lighthouse tab
  2. Select categories to audit (Accessibility, SEO, Best Practices)
  3. Click Generate report
  4. Review each failed audit with suggestions for fixing

🎯 Try It Yourself: Debug These Pages

Broken Page 1

<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is my first paragraph
<p>This is my second paragraph</p>
<img src="photo.jpg" alt="Photo">
<div id="content">
<h3>Subheading</h3>
<span><p>Text inside span</p></span>
</div>
<div id="content">
<p>More content</p>
</div>
</body>
</html>

Find and fix the issues:

  1. Unclosed <p> tag (line 8)
  2. Skipped heading level (h1 → h3, no h2)
  3. Block element <p> inside inline <span>
  4. Duplicate ID content

Broken Page 2 (Fixed Version)

<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is my first paragraph</p>
<p>This is my second paragraph</p>
<img src="photo.jpg" alt="Photo">
<div id="main-content">
<h2>Section Title</h2>
<h3>Subheading</h3>
<span>Text inside span</span>
</div>
<div id="secondary-content">
<p>More content</p>
</div>
</body>
</html>

📊 Common Error Quick Reference



📝 Summary

  • Most HTML mistakes fall into four categories: syntax, structure, accessibility, and performance
  • Unclosed tags and wrong nesting are the most common syntax errors
  • Use semantic HTML first — adding ARIA is a supplement, not a replacement
  • Browser DevTools (Elements panel) let you inspect and edit HTML live
  • W3C Validator and Lighthouse catch errors automatically
  • Follow the systematic debugging workflow: console → inspect → fix → verify

💡 Mentor's Note: Debugging is a superpower, not a chore! Every error you fix teaches you something new about how HTML works. The best developers aren't the ones who never make mistakes — they're the ones who are really good at finding and fixing them.


Purpose: Complete guide to common HTML mistakes and debugging techniques Practice Time: 25-30 minutes Next Lesson: Best Practices