Sub topic 1
What HTML Is and How Browsers Use It
Prerequisites
This chapter assumes you can create and save a plain text file in a text editor. It also assumes you can open a local file in a web browser and that you have used websites before. You do not need to know CSS or JavaScript.
What HTML Is
HTML (HyperText Markup Language) is a markup language. It describes the structure and meaning of content, such as "this is a heading" or "this is a list of items". It does not run logic like a programming language, and it does not decide colors or spacing. Those jobs belong to CSS and JavaScript.
A web page usually combines three technologies:
| Technology | Role |
|---|---|
| HTML | Structure and meaning of the content |
| CSS | Presentation: colors, fonts, layout |
| JavaScript | Behavior: reacting to clicks, loading data |
HTML is the base layer. A page with only HTML still works, and CSS and JavaScript build on it.
From File to Screen
An HTML file is plain text. When you request a page, the server sends this text to your browser. The browser then turns it into something you can see and use.
The main steps are:
- The browser reads the text and parses it. Parsing means breaking the text into pieces it understands, such as tags and text.
- It builds the DOM (Document Object Model). The DOM is a tree of objects in memory, where each element is a node and nested elements are children.
- It combines the DOM with CSS, works out the size and position of each box (layout), and draws the result on screen (painting).
The DOM is what the browser works with, not your original file. JavaScript can change the DOM, and the page updates, but the file on disk stays the same.
A First Example
Save this as index.html and open it in a browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My First Page</title>
</head>
<body>
<h1>Hello, HTML</h1>
<p>This is a paragraph.</p>
</body>
</html>The browser shows a large heading and a paragraph. The text in the <title> element appears in the browser tab, not in the page itself. The next chapters explain each part of this file.
Forgiving Parsing
Browsers do not stop when HTML contains mistakes. The HTML specification defines exactly how to recover from errors, so a missing closing tag or a misplaced element still produces a page. This is convenient but has costs:
- Different mistakes can produce surprising DOM structures, which makes bugs hard to find.
- You will not see an error message, so a page that looks fine may still be invalid.
- Assistive technology such as screen readers depends on the structure, so sloppy markup can hurt users even when the page looks correct.
Write valid HTML on purpose rather than relying on the browser to repair it.
Common Errors
- Saving the file with a
.txtextension, so the browser shows the raw text instead of a page. Use.html. - Using a word processor instead of a plain text editor. It adds formatting data that is not HTML.
- Expecting changes to appear without saving the file and refreshing the browser.
- Treating HTML as a way to style text. Choose elements for their meaning and use CSS for appearance.
Exercise
You edit a page's HTML file, and a script on the page adds a new paragraph after it loads. "View Page Source" does not show the new paragraph, but the Elements panel does. Why?
Show answer
"View Page Source" shows the original HTML text the server sent. The script changed the DOM, which is the in-memory tree the browser builds from that text, and the Elements panel shows the current DOM. The file itself never changed.