Chapter 03 of 26

HTML Document Structure

In Your First Web Page we typed a starter document and saw it work. But why does it begin with <!DOCTYPE html>? Why are there two sections called head and body? What happens if you leave something out?

In this chapter we take that starter document apart, line by line, and understand the job of each piece. Once you understand this skeleton, every HTML page on the internet will look familiar.


The Basic Skeleton

Here is a complete, valid, modern HTML document:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Structure Demo</title>
</head>
<body>
    <h1>Namaste!</h1>
    <p>Hindi: नमस्ते · Tamil: வணக்கம் · Symbols: ₹ 499, 5 × 3 = 15, x ≤ y</p>
</body>
</html>

Output in the browser:

Browser tab:  Structure Demo

Namaste!                                             ← heading
Hindi: नमस्ते · Tamil: வணக்கம் · Symbols: ₹ 499, 5 × 3 = 15, x ≤ y

All the Hindi, Tamil and mathematical characters display correctly — thanks to one small line in the head, as we'll see.

Visually, the document is a set of nested boxes:

<!DOCTYPE html>            ← not an element; an instruction to the parser
+-------------------------------------------------------+
| html  (root element, lang="en")                       |
|  +-------------------------------------------------+  |
|  | head  (information ABOUT the page — not shown)  |  |
|  |   meta charset · meta viewport · title          |  |
|  +-------------------------------------------------+  |
|  +-------------------------------------------------+  |
|  | body  (the content you SEE in the window)       |  |
|  |   h1 · p · ...                                  |  |
|  +-------------------------------------------------+  |
+-------------------------------------------------------+

Let's go through each part.


1. The DOCTYPE Declaration

<!DOCTYPE html>

The DOCTYPE (document type declaration) is the first line of an HTML document. It tells the browser to render the page in standards mode, following the modern HTML and CSS rules.

It is not an HTML element and not a tag — it has no closing tag and no content. It is a special instruction read by the parser before anything else.

Why is it needed? Standards mode vs quirks mode

In the late 1990s, browsers implemented CSS in inconsistent and often wrong ways, and millions of pages were written to depend on those bugs. When browsers later fixed their behaviour, they needed a way to avoid breaking old pages. The solution was:

  • If a page has a proper DOCTYPE, use standards mode (correct, modern behaviour).
  • If the DOCTYPE is missing (or is a very old one), use quirks mode — imitate the old buggy behaviour.

There is also a third mode, limited-quirks mode (sometimes called "almost standards"), triggered by certain old transitional DOCTYPEs; it differs from standards mode in only a few small layout details.

You can see the mode yourself with JavaScript's document.compatMode. We tested two files in Chrome:

File starts withdocument.compatModeMeaning
<!DOCTYPE html>CSS1CompatStandards mode (or limited-quirks)
No DOCTYPE (starts directly with <html>)BackCompatQuirks mode

In quirks mode, things like percentage heights, line-height calculations, gaps under images and font-size inheritance inside tables behave differently, which causes layout bugs that are very confusing to debug. So: always write the DOCTYPE.

Old vs new DOCTYPE

Older HTML versions had long DOCTYPEs pointing to a DTD (Document Type Definition) file. For example, HTML 4.01 Strict:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Modern HTML is not based on a DTD, so the DOCTYPE was reduced to the shortest string that still triggers standards mode in all browsers: <!DOCTYPE html>. It is case-insensitive (<!doctype html> works too), but the uppercase form is the most common.


2. The <html> Element — The Root

<html lang="en">
    ...everything else...
</html>

The <html> element is the root element: every other element of the page lives inside it. It has exactly two children: <head> followed by <body>.

The lang attribute

lang="en" declares the main human language of the page. It seems small, but it matters:

  • Screen readers choose the correct pronunciation and voice. Without it, a Hindi page might be read out with English pronunciation rules.
  • Browsers use it for automatic translation offers, spell-checking, hyphenation and choosing suitable fonts.
  • Search engines can use it as one signal of the page's language.

Values are language codes (BCP 47 tags):

ValueLanguage
enEnglish
en-INEnglish as used in India
hiHindi
taTamil
bnBengali

If a small part of the page is in a different language, you can put lang on that element only — we'll see this in Elements & Attributes.


3. The <head> Element — Information About the Page

The <head> element contains metadata — data about the document, such as its title, character encoding, linked stylesheets and scripts. Its contents are not displayed in the page area.

Typical things inside <head>:

ElementPurpose
<meta charset="utf-8">Character encoding
<meta name="viewport" …>Mobile-friendly display
<title>Page title (tab, bookmarks, search results)
<meta name="description" …>Short description, often shown by search engines
<link rel="stylesheet" href="style.css">Attach a CSS file
<link rel="icon" href="favicon.ico">The small icon on the tab
<style>CSS written inside the page
<script>JavaScript (often with defer)

In this chapter we focus on the three essentials. All the other head elements and meta tags get a full chapter of their own: The Head Element & Meta Tags.

3a. <meta charset="utf-8">

Computers store text as bytes. A character encoding is the rulebook that maps bytes to characters. UTF-8 is an encoding of Unicode that can represent every character of every script — English letters, ₹, ×, ≤, Devanagari, Tamil, Chinese, emoji, everything.

Rules to remember:

  • In modern HTML the only allowed value is utf-8 (case does not matter).
  • The declaration must appear within the first 1024 bytes of the file, because the browser looks for it before it starts decoding the text. That's why we put it as the first element inside <head> — even before <title>, which may itself contain non-English text.
  • Your editor must also save the file as UTF-8 (VS Code does this by default; you can see "UTF-8" in its status bar).

If the encoding is wrong or missing, a word like "₹ 499" can turn into garbage characters such as ₹ 499. This garbled text is called mojibake.

3b. <meta name="viewport" content="width=device-width, initial-scale=1">

Without this line, mobile browsers assume your page was designed for a desktop screen (around 980px wide) and shrink the whole thing down, making text tiny. This line says: "make the page width equal to the device's screen width and don't zoom out". Every modern page should include it.

3c. <title>

<title>Structure Demo</title>

The title is shown:

  • on the browser tab,
  • as the default name when someone bookmarks the page,
  • as the clickable blue headline in most search engine results,
  • and it is the first thing a screen reader announces when the page opens.

A normal HTML page must have exactly one <title>, inside <head>. It can contain only text (no tags). Make it short, specific and unique per page, for example Binary Search Trees – DSA Notes rather than just Home.


4. The <body> Element — Visible Content

The <body> element contains all the content that is displayed in the browser window: headings, paragraphs, images, links, lists, tables, forms, videos and so on.

A document has exactly one <body>, and it comes right after </head>. Everything you learn in the rest of this tutorial goes inside it.


The Document as a Tree: Parent, Child, Sibling

Because elements are nested inside each other, the document forms a tree. Developers describe the relationships using family words:

html                      ← root (has no parent element)
├── head                  ← child of html
│   ├── meta              ← child of head
│   ├── meta              ← sibling of the first meta
│   └── title
└── body                  ← sibling of head, child of html
    ├── h1                ← child of body
    └── p                 ← sibling of h1
TermMeaningExample
ParentThe element directly containing anotherbody is the parent of h1
ChildAn element directly inside anothertitle is a child of head
SiblingElements that share the same parenthead and body
DescendantAnything inside, at any depthp is a descendant of html
AncestorAny element above, at any depthhtml is an ancestor of title

These words appear constantly in CSS (child and descendant selectors) and JavaScript (parentElement, children), so it's worth getting comfortable with them now.


What If You Leave Tags Out? (Optional Tags)

Here is a surprising fact: the HTML standard allows the start and end tags of <html>, <head> and <body> to be omitted in many cases. The parser creates these elements automatically. We tested this tiny file in Chrome:

<!DOCTYPE html>
<title>Tiny</title>
<h1>Hello</h1>
<p>No html, head or body tags written!

The DOM that Chrome built (as shown by DevTools):

<!DOCTYPE html>
<html><head><title>Tiny</title>
</head><body><h1>Hello</h1>
<p>No html, head or body tags written!
</p></body></html>

The browser added html, head and body by itself, put title into the head (because title is metadata), and started the body as soon as it saw <h1> (because a heading is visible content). It even closed the paragraph.

So why do we still write them? Because:

  • Writing them explicitly makes the structure obvious to every reader of your code.
  • You need the <html> start tag anyway to add lang="en".
  • Relying on implicit rules is error-prone — one misplaced element can push the rest of your head content into the body.

Treat this as knowledge, not a style to copy. Always write the full skeleton.


Formatting the Source: Indentation

Browsers ignore indentation between elements, so this is only for humans — but it matters a lot when reading code. The common convention:

  • Indent child elements by 2 or 4 spaces relative to their parent (pick one and be consistent).
  • <head> and <body> are often not indented inside <html>, because almost everything would otherwise be indented one level for no benefit.
  • Use lowercase tag and attribute names.

Tools like Prettier (available as a VS Code extension) can format your HTML automatically on save.


Common Mistakes

MistakeWhat goes wrong
Missing <!DOCTYPE html>Page renders in quirks mode; CSS layouts behave strangely.
Putting text or <h1> inside <head>The parser closes the head early and moves that content (and everything after it) into the body. Any <meta> or <link> written later lands in the body too.
Writing <title> inside <body>Invalid HTML. Chrome still uses it for the tab, but the element sits in the body, and validators report an error.
Two <body> elementsInvalid. The browser does not create a second body: it ignores the extra tag, copies any new attributes onto the first body, and puts the content into the first body.
<meta charset> placed late in a long head, or missingThe browser may guess the wrong encoding and show mojibake.
Forgetting langScreen readers and translation tools may treat the language incorrectly.
Forgetting the viewport metaPage appears zoomed-out and tiny on phones.

Interview / Exam Questions

1. What is the purpose of <!DOCTYPE html>? Is it an HTML tag?

It is a document type declaration, not an element or tag. It tells the browser to render the page in standards mode. Without it, browsers fall back to quirks mode, which emulates old, non-standard behaviour.

2. What is the difference between <head> and <body>?

<head> holds metadata about the document (title, charset, stylesheets, scripts) and is not displayed in the page area. <body> holds the visible content of the page.

3. Why should <meta charset="utf-8"> come first in the head?

The encoding declaration must be within the first 1024 bytes of the document so the browser can find it before decoding the rest. Placing it first also guarantees that the title and other metadata are decoded correctly.

4. What is quirks mode? How can you check which mode a page is using?

Quirks mode is a rendering mode in which browsers imitate the non-standard behaviour of 1990s browsers for backward compatibility. It is triggered by a missing or very old DOCTYPE. In the console, document.compatMode returns "BackCompat" for quirks mode and "CSS1Compat" for standards (or limited-quirks) mode.

5. Why is the lang attribute on <html> important?

It declares the page's language so screen readers can pronounce text correctly and browsers can offer translation, apply correct hyphenation and spell-checking. It is also a basic accessibility requirement.

6. If you omit the <html>, <head> and <body> tags, does the page still work? Why?

Yes. The HTML parser creates these elements automatically, because their tags are optional in many situations. However, writing them explicitly is recommended for clarity and to add attributes like lang.


Summary

  • Every page starts with <!DOCTYPE html> to trigger standards mode.
  • <html lang="…"> is the root element and declares the page's language.
  • <head> holds metadata: meta charset="utf-8" (first!), the viewport meta, and the <title>.
  • <body> holds everything visible; there is exactly one.
  • The document is a tree — learn the words parent, child, sibling, ancestor and descendant.
  • Browsers can infer missing html/head/body tags, but you should always write them.

In the next chapter, Elements & Attributes, we'll zoom in on the building blocks themselves: what exactly a tag, an element and an attribute are, and the rules for writing them correctly.