Sub topic 1
What HTML is and how browsers use it
Prerequisites
This chapter assumes you can do three things:
- Create and save a plain text file in a text editor.
- Open a local file in a web browser.
- Browse websites and recognize a page, a link and an address.
You don't need to know CSS or JavaScript. They are mentioned only to show where HTML fits.
What HTML is
HTML (HyperText Markup Language) is a text format that describes the structure and meaning of a web page's content. It tells the browser "this is a heading", "this is a paragraph", "this is a link to another page" and "this is an image".
Three words in the name matter:
- Hypertext is text that links to other text. Links are what make the web a web.
- Markup means you annotate plain text with labels, called tags, that say what each piece is.
- Language means there are rules about which labels exist and where they may appear.
HTML is not a programming language. It has no variables, loops or conditions. It declares what content exists, and the browser decides how to show it. This is why the same HTML can appear on a phone, on a wide monitor, in a screen reader or in a search engine's index.
HTML is maintained as a "living standard" by the WHATWG, a group of browser vendors. There are no numbered releases to track anymore. The current specification is simply called HTML. What you learn here is what browsers implement today.
The three languages of the front end
A web page is usually built from three technologies, each with its own job:
| Technology | Job | Example question it answers |
|---|---|---|
| HTML | Structure and meaning | What is on this page? |
| CSS | Presentation | What does it look like? |
| JavaScript | Behavior | What happens when the user clicks? |
Keep the separation in mind. If you find yourself using HTML only to make text bigger or to add space, that job belongs to CSS. Choosing an element for its meaning, not its default appearance, is the main habit this tutorial builds.
From file to pixels
When you type an address or click a link, the browser does not receive a picture of the page. It receives text and turns that text into what you see.
- The browser sends a request to a server.
- The server replies with a response. The response has headers and a body. For a web page, the body is HTML text.
- The browser checks the
Content-Typeheader. A value oftext/htmltells it to treat the body as HTML. - The parser reads the text and builds a tree of objects called the DOM (Document Object Model).
- While parsing, the browser finds references to other files, such as stylesheets, scripts and images, and requests them.
- The browser combines the DOM with the styles, works out the position and size of every box (layout), and paints the result on screen.
The parser works while the text is still arriving, so a large page can start to appear before the download finishes.
A first complete document
Save this as index.html and open it in a browser. It is the smallest document you should consider complete. Later chapters cover each part in detail.
<!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 tab shows "My First Page", which comes from the title element in the head. The window shows a large heading and a paragraph, which come from the body. The head holds information about the page. The body holds what the reader sees.
You will see a few extra words that you did not write nowhere on screen: the lang value, the charset declaration and the viewport settings all work silently. They control language handling, text decoding and mobile layout.
The DOM is not your source file
A common point of confusion is that the browser does not display your file directly. It parses the file and builds the DOM, then renders the DOM. The two usually match, but not always.
Consider this deliberately sloppy source:
<!DOCTYPE html>
<title>Sloppy</title>
<p>First paragraph
<p>Second paragraph
<table><tr><td>Cell</td></tr></table>There is no html, head or body tag, and neither paragraph is closed. The browser still produces a full tree. It adds html, head and body, places title in the head, ends the first p when the second begins, and inserts a tbody inside the table. If you inspect the page in the developer tools, you see the repaired tree, not your text.
This has two consequences:
- View Source and the Elements panel show different things. View Source shows the text the server sent. The Elements panel in the developer tools shows the live DOM, including repairs and any changes JavaScript has made since.
- CSS and JavaScript act on the DOM. If your selector expects an element that you wrote one way and the parser built another way, it will not match. The inserted
tbodyis a classic example.
Error tolerance and why it is a trap
HTML parsing is defined to be forgiving. Browsers never refuse to show a page because of a syntax error. The specification describes exactly how to recover from each mistake, so every compliant browser repairs a given error in the same way.
That is convenient, but it hides problems. A page can look fine in your browser while containing mistakes that cause trouble elsewhere: a screen reader may announce a confusing structure, a search engine may read the page differently, and a small edit may suddenly move content to an unexpected place. Treat the browser's silence as "no crash", not as "no errors". Chapter 15 shows how to check a document with a validator.
Standards mode and quirks mode
Before the web standardized, browsers each implemented layout rules differently. To avoid breaking old pages, modern browsers keep a second set of rules called quirks mode. A document gets standards mode or quirks mode depending on its first line.
- With
<!DOCTYPE html>at the very top, the browser uses standards mode and follows current rules. - With no doctype, or an old broken one, the browser falls back to quirks mode and imitates legacy behavior.
Quirks mode does not produce an error message. It changes the layout, for example how widths and heights are computed, and the result looks subtly wrong. The doctype has no other function. It is a mode switch, not a version number. Always include it, and keep it as the first line.
Text, encoding and the Content-Type
HTML files are plain text, so the browser must know how to turn bytes into characters. The server can declare the encoding in the Content-Type header, and the document can declare it with <meta charset="utf-8">. If neither is present, the browser guesses, and accented letters or symbols may turn into garbage such as é. Use UTF-8 and declare it near the top of the head.
Two practical points follow:
- The file extension (
.html) is a convention for your computer. Over the network, theContent-Typeheader decides how the response is handled. - Save your file in UTF-8 in the editor. Declaring UTF-8 while saving in another encoding produces the same garbled text.
Common errors and how to spot them
| Symptom | Likely cause | How to check |
|---|---|---|
| Browser shows the raw tags as text | File saved as `.txt`, or served with the wrong type | Check the extension and the `Content-Type` header |
| Page opens as a download | The server sent a type other than `text/html` | Look at the response headers in the Network panel |
| Odd characters such as `é` | Encoding mismatch | Declare UTF-8 and save the file as UTF-8 |
| Layout differs from what you expect | Quirks mode from a missing doctype | Confirm `<!DOCTYPE html>` is the first line |
| Changes do not show after saving | The browser cached the old file, or you did not reload | Reload, and use a hard reload if needed |
| Elements are not where you wrote them | The parser repaired invalid nesting | Compare View Source with the Elements panel |
Tools you will use throughout
Every modern browser has developer tools, usually opened with F12 or by right-clicking and choosing Inspect. Three panels matter now:
- Elements shows the live DOM and lets you see how the browser repaired your markup.
- Network shows each request and response, including the
Content-Typeheader. - Console reports problems and warnings.
Get used to switching between your editor and the Elements panel. Seeing the tree the browser built is the fastest way to learn how HTML really works.
Exercise
You save a file without a doctype and without a body tag, and the page still displays. Name two things the browser did that you did not write, and say why seeing the page work is not proof the file is correct.
Show answer
The browser added the missing html, head and body elements to the DOM, and it switched to quirks mode because there was no doctype. The page displays because the HTML parser is defined to recover from errors, not because the file is valid. Quirks mode can change layout, and the repaired structure can differ from what you intended, so a validator and the Elements panel are the way to check.
Exercise
View Source shows <p>One<p>Two, but the Elements panel shows two separate closed p elements. Explain the difference.
Show answer
View Source shows the text the server sent. The Elements panel shows the DOM the parser built from it. The parser closes the first p when the second one starts, so the DOM contains two complete paragraphs even though the source never closed them.