Lesson 2 of 36
HTML Tutorial

Anatomy of an HTML Document

The doctype, html, head and body - the skeleton every page shares.

3 min readBeginnerWeb FundamentalsIntroduction

Every HTML page, from a one-line experiment to a huge shop, shares the same skeleton. Learn it once and you will recognise it everywhere. In this lesson we take the skeleton apart piece by piece using the Brew and Byte cafe home page.

The full skeleton#

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Brew and Byte - Coffee and Co-working</title>
  </head>
  <body>
    <h1>Brew and Byte</h1>
    <p>Open daily from 7am to 7pm.</p>
  </body>
</html>

Read it from the top down.

The doctype#

<!DOCTYPE html> is not an element. It is a declaration that tells the browser, "this page uses modern HTML." Without it, browsers fall back to quirks mode, an old compatibility mode that renders things inconsistently. Always put it on the very first line. It is not case sensitive, but writing it exactly like this is the convention.

The html element#

<html> is the root element. Everything else lives inside it. Give it a lang attribute describing the page language, for example lang="en". This matters more than it looks:

  • Screen readers choose the right pronunciation.
  • Browsers offer correct translation prompts and hyphenation.
  • Search engines learn which audience the page targets.

The head#

<head> holds information about the page, nothing in it is displayed as page content. Typical residents:

  • <meta charset="utf-8"> declares the character encoding so accented letters and symbols display correctly. Keep it within the first 1024 bytes of the file.
  • <meta name="viewport" ...> makes the page scale properly on phones.
  • <title> sets the text shown in the browser tab, in bookmarks and in search results.

We will explore the head in detail in its own section later.

The body#

<body> contains everything the visitor actually sees and uses: headings, text, images, links, forms. A page has exactly one body.

Nesting and indentation#

Elements sit inside other elements, like boxes inside boxes. <head> and <body> are both children of <html>, and they are siblings of each other. Indenting children by two spaces makes the structure visible at a glance:

html
<body>
  <main>
    <h1>Today's menu</h1>
    <p>Everything is made fresh each morning.</p>
  </main>
</body>

Indentation is for humans only. The browser ignores it, but your teammates will thank you.

Common mistakes#

  • Forgetting the doctype, which triggers quirks mode.
  • Omitting lang, which hurts accessibility.
  • Putting visible content in <head>, or <title> in <body>. Browsers may quietly move things around, hiding the mistake.
  • Forgetting the charset meta tag, which makes characters like the rupee symbol turn into gibberish.
  • Closing tags in the wrong order. Close the most recently opened element first.

Try it yourself#

Create a file called skeleton.html. Type the skeleton from memory, without copying, changing the title to your own project name. Add one heading and one paragraph in the body. Open it in a browser and check that the tab shows your title. Then deliberately delete the <!DOCTYPE html> line, refresh, and see whether anything changes for such a simple page. Restore it afterwards.

Recap#

A document starts with the doctype, then the html root with a lang, then a head for page information and a body for visible content. Indent children consistently and always include charset, viewport and a meaningful title.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-