HTML5 Document Structure: DOCTYPE, Head, and Body
Lesson 1 of 36 in the HTML5 learning path.
How do I write a valid HTML5 document from scratch?
An HTML5 document is the full file a browser reads before it can paint a page. It starts with a DOCTYPE so the engine stays in standards mode, then a root html element that names the language, a head for the title and other metadata, and a body for everything the visitor sees. That order is the contract between your file and every browser, crawler, and screen reader that opens it. Skip the skeleton and the browser guesses the rendering mode, the character set, and the language.
Use this structure whenever you create a page, replace an old template, or paste a fragment that arrived without a root. Classroom exercises, a department homepage, and a published article all begin the same way. Later lessons hang meta tags, landmarks, headings, and scripts on this frame, so a correct first file prevents a stack of small bugs that are hard to see in the browser.
Syntax & Parameters
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Title</title></head><body></body></html>
Example
Interactive preview available<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Intro to Web Systems</title>
<!-- Visible course text stays in the body. The head is for the title and metadata. -->
<style>
body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
h2 { font-size: 1.25rem; margin: 1.4rem 0 0.45rem; }
p, li { margin: 0 0 0.85rem; }
a { color: #1d4ed8; }
footer { max-width: 42rem; margin: 0 auto; padding: 0 1.5rem 2.5rem; color: #44403c; }
</style>
</head>
<body>
<header>
<p>North Campus, Web Systems 1</p>
<h1>Intro to Web Systems</h1>
</header>
<main>
<p>This file is a complete HTML5 document. The head stores the title and the character encoding. The body stores the sentences a student reads in class.</p>
<p>Save it as index.html and open it in a browser. View the source and check three markers: the DOCTYPE is the first line, the html element names a language, and every visible sentence lives in the body.</p>
<h2>Before you submit</h2>
<p>Confirm the title names the course, the language is English, and you did not place a visible heading inside the head.</p>
<ul>
<li>DOCTYPE on the first line</li>
<li>lang on the html element</li>
<li>Visible text only in the body</li>
</ul>
<p><a href="syllabus.html">Read the syllabus</a> for weekly topics, lab rules, and the files you will turn in.</p>
</main>
<footer>
<p>Department of Computer Studies. Updated 26 November 2026.</p>
</footer>
</body>
</html>
Expected Output
The browser tab reads Intro to Web Systems. The page shows a navy band with North Campus, Web Systems 1 and the heading Intro to Web Systems, then two paragraphs, a Before you submit section, a three-item checklist, and a Read the syllabus link on an off-white background. The footer line names the Department of Computer Studies and the date 26 November 2026. View source shows <!DOCTYPE html> as the first line, lang="en" on the html element, and the visible sentences inside body. The syllabus link requests syllabus.html next to this file. If that file is not published yet, only that address returns a not-found response. The course page itself still paints.
Line-by-Line Walkthrough
- The DOCTYPE on the first line selects standards mode so widths, heights, and default styles follow current HTML rules.
- The html element wraps the document, and lang="en" tells browsers and screen readers that the page is English.
- The charset meta is early in the head so the browser decodes the rest of the file as UTF-8.
- The viewport meta is included so a phone lays the page out at device width instead of shrinking a desktop canvas.
- The title element supplies the browser tab, the bookmark label, and the default name in a search result.
- The comment in the head is a reminder that visible course sentences do not belong beside the title.
- The style element exists so this single file is readable in class. A multi-page site should link a shared stylesheet.
- The header, heading, paragraphs, checklist, and syllabus link all live in the body, which is the only place visible content belongs.
- The footer holds the department line that can repeat on every course page without moving into the head.
Open index.html in a browser and then view source. Match the DOCTYPE, the language, the title, and the body text to those nine steps before you add landmarks or scripts.
Common Pitfalls & Security
A fragment of tags with no DOCTYPE, or a heading pasted into the head, often paints something that looks almost right. The browser does not show a red error for a missing language or a quirks-mode document. Assistive technology and layout just have less reliable information to work with.
- Omitting the DOCTYPE can drop the browser into quirks mode and change how widths and heights are calculated.
- A missing or wrong lang value makes a screen reader pronounce the page with the wrong language rules.
- Visible text placed in the head is not shown, so a heading typed there looks like a blank body.
- A charset that does not match the file encoding garbles apostrophes and accented names when a host assumes another encoding.
- A second html or body element copied from a template makes the parser repair the tree in ways that are hard to see from the painted page.
Best Practices & Modern Standards
HTML5 expects one document per response, a DOCTYPE written as <!DOCTYPE html>, and a language on the root. Keep the head for metadata and the body for content. Write the title as a human label, such as the course name, not the file name index.html. Save the file as UTF-8 so the charset you declare matches the bytes on disk.
When the page grows, keep this skeleton and add to it. Link a shared stylesheet instead of copying a large style block into every exercise, and load scripts so they do not block parsing. Validate the file when the structure changes. A validator catches a missing end tag long before a classmate reports a broken layout.
Real-World Use
The Web Systems 1 course home is this skeleton in production form. The instructor publishes index.html as the first file in the class folder. The tab title is Intro to Web Systems, the header names North Campus, and the body points students to the syllabus and the checklist they must confirm before lab one is accepted.
The department site uses the same pattern for every public URL, from an admissions article to the faculty directory. Each address returns one HTML5 document with its own title and body. Shared colors come from CSS. They are not a reason to skip the document shell.
Frequently Asked Questions
Does the DOCTYPE have to be uppercase?
Browsers accept a DOCTYPE in any letter case, and HTML5 writes it as uppercase DOCTYPE with lowercase html. Use that form on every page so reviews stay consistent. The declaration is not an element, so it has no end tag.
Can a document have two body elements?
No. A document has one head and one body. If you paste a second body, the parser folds the extra tags into the first body and the tree will not match the source you think you wrote. Keep every visible region in that single body.
Where does the visible h1 go?
The h1 belongs in the body, near the start of the main content or in the page header. A heading typed inside the head is not shown as a heading. The outline lesson covers how the levels under that h1 should nest.
Is a full document required for a one-paragraph exercise?
Yes. Even a one-paragraph exercise should be a complete HTML5 document so the browser stays in standards mode and the file matches what you submit. Fragments belong in templates that a server inserts into a document that already exists.