Skip to article content
FrontendXLesson 01 / 30

Document structure and the element tree

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

beginner · about 25 minutes · 2 exercises

  • Predict
  • Implement

What you will be able to do

#
  • Explain how the browser turns HTML source into a DOM tree.
  • Write a minimal valid document with doctype, language, charset, viewport and title.

Overview

#

Every page starts as a tree. The browser parses your HTML into nodes, and that tree, the DOM, is what CSS styles and JavaScript queries. Two documents that look identical on screen can expose very different trees, and the tree is what assistive technology reads.

A minimal valid document declares four things before any content: the doctype, the document language, the character encoding, and a viewport rule for mobile. Miss any of them and the browser guesses.

snippet.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Invoices | Acme</title>
  </head>
  <body>
    <h1>Invoices</h1>
  </body>
</html>

The lang attribute is not decoration. It selects pronunciation rules for screen readers and hyphenation for typography. The title is the first thing announced when a tab gains focus, and it is the text that search engines show in results. The charset must appear within the first 1024 bytes so the parser can decode the rest of the document correctly.

When you inspect the DOM in DevTools you see the parsed tree after error recovery, not your original bytes. Browsers silently fix unclosed tags, move stray elements into body, and drop invalid nesting. That forgiveness is why malformed markup can still render, and why it is worth validating structure deliberately rather than trusting that it looks right.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write a complete minimal HTML document for a page titled 'Reading list' that declares English, UTF-8, a responsive viewport and exactly one top-level heading.

    starter.html
    <!-- write the full document -->

    Reveal the reference answer
    reference.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Reading list</title>
      </head>
      <body>
        <h1>Reading list</h1>
      </body>
    </html>

    Authored expected output: The root element has lang="en", head contains a charset meta, a viewport meta and a title, and body contains exactly one h1.

    The doctype triggers standards mode, lang gives the document a language, charset decodes the rest of the bytes, and the viewport meta stops mobile browsers from rendering at a fake desktop width.

  2. Exercise 2Predict

    Which element must contain the viewport meta tag for it to take effect?

    Choose the best answer

Common misconceptions

#
  • The DOM is always identical to the HTML source you wrote.
  • A missing doctype only matters for very old browsers.

Follow-up practice

#
  • html-css-02-semantic-landmarks

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.