A small piece of HTML can change what a visitor sees or how the browser understands a page. In this lesson, you will explore tree construction and automatic element insertion using a short example that you can inspect and change.
The Idea in Plain English
The browser does not display HTML by reading every character as text. Its parser tokenizes the source and constructs a tree of nodes called the DOM. When markup is malformed, the parser follows error-recovery rules and may change the tree.
You can test this using a small local HTML file. Change one part at a time and check the result in the browser before adding more code.
A Working Example
Read the sample once before copying it. Notice the element names and attribute values, then compare them with the explanation below.
<main>
<p>First paragraph.</p>
<p>Second paragraph.</p>
</main>
What Each Part Does
- The main landmark identifies the page’s primary content.
- A paragraph keeps one idea together as readable text.
In the browser Elements panel, you may see inserted or rearranged nodes that were never written explicitly in the file.
Keep the example small while learning. Once it behaves as expected, try it inside a larger page and make sure the URLs, files and surrounding HTML are correct.
Try It Yourself
Try this next: compare authored source against the DOM tree in DevTools focused on HTML Tree Construction and Automatic Element Insertion, including a working example and an incorrect example to correct. Then inspect the page and explain what changed.
Common Beginner Mistakes
- Assuming the View Source window and the live DOM must always have the same structure.
- Changing several parts of the example at once, making it difficult to see which part caused a different result.
- Choosing a tag only because its default appearance looks right, without checking whether it describes the content correctly.
Key Takeaways
- The browser does not display HTML by reading every character as text. Its parser tokenizes the source and constructs a tree of nodes called the DOM. When markup is malformed, the parser follows error-recovery rules and may change the tree.
- In the browser Elements panel, you may see inserted or rearranged nodes that were never written explicitly in the file.
- Test the markup in a browser, and check accessibility and real-world behavior where relevant.