CHAPTER 54 · HTML

Find and fix HTML mistakes: source, browser structure and real tests

Use a repeatable checking process. Separate invalid markup, broken resource paths and incorrect behaviour instead of judging a page only by its appearance.

Download example files (ZIP)

An error is a clue to investigate

Imagine checking a written address before blaming the delivery person. A page problem also has a cause that you can narrow down. Debugging means finding and correcting a fault. Start with what you expected and what actually happened.

An error message identifies something needing attention. It does not mean you cannot learn. Make one change at a time and check the result so you can explain what fixed the problem.

The browser can repair invalid markup

HTML parsers recover from many mistakes so pages can still display. That means a page looking reasonable does not prove its source is valid. A list cannot be wrapped inside p merely because you want it to look like one paragraph.

Example fragment — use inside a complete program

Intentional error — fix it before running.

<p>Today
  <ul><li>Save the file.</li></ul>
</p>

The browser may close the paragraph before the list and create other structure while recovering. The repaired complete example uses separate paragraphs and a list. Indentation helps you read nesting, but does not change the rules about which elements fit inside others.

Saved source and the live document are different views

View Source shows the HTML supplied for the page. The browser inspector shows the current document tree after parsing and any script changes. That tree is commonly called the DOM, or Document Object Model: the browser's structured representation of the document.

A script can update text, and a parser can repair structure, so those two views need not look identical. Check both when the inspected structure does not match the file you thought you wrote. Also confirm that you saved and opened the intended file.

Run a modern HTML conformance check

A conformance checker compares markup with HTML rules. The Nu HTML Checker is one available checker for modern HTML. Use the appropriate public URL, file or direct-input method for your own non-sensitive practice source. Do not upload private documents merely to test a small example.

Read the first useful error, inspect its location and the nearby opening tags, repair the cause, then check again. One missing quote or closing tag can produce several later messages. Error wording and exact line numbers can change between tools and versions.

Recognise common structural issues

Check a complete doctype, useful title, declared language, matching quotes, valid nesting, unique ids and required alternatives for meaningful images. Use complete closing tags for non-void elements; do not add closing tags to void elements such as img or input. A trailing slash does not make an HTML div self-closing.

Not every omission that is legal HTML is a good beginner habit. We write clear structure and explicit endings where helpful so changes are easier to review. A formatting preference and a genuine content-rule error are different kinds of feedback.

Check paths and connections separately

An HTML checker cannot always prove that a local filename exists on your eventual server. For images, CSS, scripts and links, compare the saved folder, spelling and relative path. Case differences can work on one filesystem and fail on another. The browser Network panel can expose missing requests when testing a served page.

A label for must match an id, a fragment must reach a unique destination, and a field needs name when its value should be submitted. The second complete example lets you test these connections in the browser, not merely admire a form that looks finished.

Test behaviour and meaning after syntax

Click labels, follow each link and use the keyboard. Test valid and invalid form answers. Resize the page and inspect long content. Check that a script updates the intended element rather than relying on an accidental id match.

A syntactically valid link can still have vague wording or go to the wrong useful page. A valid form can still ask an unclear question. Automated checks support review; they do not decide whether the lesson, instructions or interaction make sense to a beginner.

Keep a small review record

Record a test situation, the expected result and the observed result. For example: empty required alias should stop ordinary form submission; a completed invented alias and question should reach the local result URL. The result page must still be described as static, not a real message delivery.

After a fix, repeat the check that failed and nearby behaviour the change could affect. Do not make many unrelated edits while trying to identify one fault. Keep useful earlier versions so a failed experiment does not destroy your working starting point.

Follow the idea

Investigate one problem
  1. 1State expected and actual results
  2. 2Inspect source, structure or request
  3. 3Make one focused correction
  4. 4Repeat the relevant interaction check

A reasonable appearance does not prove valid markup or a useful task.

Complete program: repaired-plan.html

Save this as repaired-plan.html. Open it in your browser.

repaired-plan.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Repaired Practice Plan</title>
  </head>
  <body>
    <h1>Keep paragraphs and lists separate</h1>
    <main>
      <p>Use this short plan before testing your page.</p>
      <ul>
        <li>Save the HTML file.</li>
        <li>Open the saved file in a browser.</li>
        <li>Check one intended change.</li>
      </ul>
      <p>A list is a separate structure, not content wrapped inside this paragraph.</p>
    </main>
  </body>
</html>
Browser previewDocument title: A Repaired Practice Plan

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: two ordinary paragraphs surrounding a three-step list. The list is not inside a p. Compare the source with the intentionally invalid fragment and explain why the complete version is easier to maintain.

Complete program: checked-question.html

Save this as checked-question.html. Open it in your browser.

checked-question.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Checked Form Connection</title>
  </head>
  <body>
    <h1>Check labels and submitted names</h1>
    <main>
      <p>
        This is a local check, not a message service. Keep form-result.html from Chapter 31 beside
        this file.
      </p>
      <form action="form-result.html" method="get">
        <p><label for="check-alias">Practice alias (required)</label></p>
        <p><input type="text" id="check-alias" name="alias" required /></p>
        <p><label for="check-question">Short practice question (required)</label></p>
        <p><textarea id="check-question" name="question" rows="3" cols="28" required></textarea></p>
        <button type="submit">Inspect the checked values</button>
      </form>
    </main>
  </body>
</html>
Browser previewDocument title: A Checked Form Connection

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.

Expected result: each visible label targets its own field. Empty required values stop ordinary validated submission. Valid invented values appear as alias and question entries at the static local destination. Save the result page too; the tutorial preview blocks submission.

Problem and solution

Problem: the page appears acceptable, but a checker reports a nesting problem and the inspector shows extra paragraph elements.

Solution: compare saved source and browser structure. Fix the invalid parent-child arrangement rather than adding more CSS to hide the recovery result.

Common mistakes and useful tips

  • Appearance alone does not prove valid source.
  • Start with the first useful error rather than every later symptom.
  • Check source, DOM and requests for their different jobs.
  • Do not upload sensitive content to online checking tools.
  • Test labels, names and destinations as well as tags.
  • Make one focused correction and verify it.

Practice questions

  1. What does debugging mean?
  2. Can the browser display invalid HTML?
  3. What is the DOM?
  4. Does a conformance check prove that every resource path works on your server?
  5. Why inspect the first useful error first?
  6. Check the two complete examples and write one test with expected and observed results.
Check your practice answers
  1. Finding and correcting the cause of a problem.
  2. Yes. It often recovers, so appearance is not proof of validity.
  3. The browser's structured representation of the current document.
  4. No. Test actual paths and requests too.
  5. An early mistake can cause many later messages.
  6. Examples include verifying the separate list structure or testing that a label focuses the correct field. Describe what you actually observed rather than claiming a broad guarantee.

Interview questions

How do source and DOM differ?

Source is the supplied markup. The DOM is the parsed current structure, which can reflect error recovery and script updates.

What can a conformance checker establish?

It can identify rule violations it checks in the submitted document. It does not by itself prove useful content, complete accessibility or every live resource connection.

Why can a working local path fail after deployment?

The server can have different case sensitivity, folder layout or URL routing. Test the deployed addresses and preserve intended file names.

Why change one thing at a time when debugging?

It helps identify which change caused the result and avoids confusing several independent problems.

Quick revision

  • State expected and actual behaviour.
  • Check source structure and live connections.
  • Use automated feedback with real interaction tests.
  • Keep evidence of the checks you actually ran.