Course topics

By WebNest Studio Engineering Team

HTML Tutorial

HTML Document Structure

Every HTML document follows a predictable skeleton that browsers rely on to parse and render a page correctly. Before you touch tags, attributes, or styling, you need to understand this skeleton, because getting it wrong — even subtly — can cause a browser to fall back into "quirks mode" and render your page inconsistently.

The structure exists for a reason: it tells the browser which version of HTML you're using, separates metadata from visible content, and gives assistive technology and search engines a predictable place to look for information about the page.

Why it matters

Every page you ever write starts from the same skeleton. Leave out the doctype and browsers fall back to an old compatibility mode with different layout rules; leave out the language or the character encoding and screen readers and special characters go wrong. Getting the skeleton right once means every page starts correct.

The DOCTYPE Declaration

The very first line of any HTML5 document must be <!DOCTYPE html>. This is not a tag in the traditional sense — it's an instruction to the browser that says "render this page in standards mode, using the modern HTML5 rules." Older HTML versions had long, complex DOCTYPE strings tied to DTDs (Document Type Definitions); HTML5 simplified this to a single short line. Omitting the DOCTYPE, or placing anything before it, can trigger quirks mode, where the browser emulates old, inconsistent rendering behavior from the 1990s.

The html, head, and body Elements

Immediately after the DOCTYPE comes the root <html> element, which wraps the entire document and should carry a lang attribute (e.g. lang="en") so screen readers and translation tools know the page's language. Inside <html> there are exactly two top-level children in order:

  • <head> — contains metadata that is not directly displayed: the page title, character encoding, linked stylesheets, scripts, and SEO-related meta tags.
  • <body> — contains everything the user actually sees and interacts with: text, images, forms, links, and other visible content.

Why Order and Nesting Matter

Browsers are forgiving and will attempt to fix malformed HTML, but relying on that forgiveness produces fragile pages. The <head> must come before the <body>, tags should be properly nested (closing in the reverse order they were opened), and every element that requires a closing tag should have one. Well-formed structure is also what makes a page validate cleanly and behave consistently across different browsers and parsers.

Example

A minimal, complete HTML5 document skeleton

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My First Page</title>
</head>
<body>
  <h1>Welcome to Webnest Studio</h1>
  <p>This is a minimal, valid HTML5 document.</p>
</body>
</html>
Output
Renders a page with the browser tab titled "My First Page", showing a heading and a paragraph of text in the body.

Common Mistakes

  • Forgetting the <!DOCTYPE html> declaration, which can push older or non-standard browsers into quirks mode.
  • Placing visible content or stylesheets directly inside <head> instead of <body>.
  • Omitting the lang attribute on <html>, which hurts accessibility and search engine language detection.
  • Nesting tags out of order (e.g. closing a <div> before a <p> that was opened inside it), producing invalid, unpredictable markup.

Exercise

Complete the skeleton of a valid HTML5 document: add the doctype, the language of the page (English), the character encoding and a title that reads My first page.

Expected result: the page shows the heading Hello, HTML, and the browser tab title is My first page.

HTML · starter code
<!-- TODO: doctype -->
<html>
  <head>
    <!-- TODO: character encoding -->
    <!-- TODO: title -->
  </head>
  <body>
    <h1>Hello, HTML</h1>
  </body>
</html>
Show hints
  • The HTML5 doctype is <!DOCTYPE html> and must be the very first line.
  • The language goes on the html element as lang="en", and the encoding is <meta charset="utf-8">.
Show solution
HTML · solution
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My first page</title>
  </head>
  <body>
    <h1>Hello, HTML</h1>
  </body>
</html>

Quick quiz

1. What happens if the doctype is missing?

  • A. The browser renders the page in quirks mode, with older layout behaviour
  • B. The page does not load
  • C. The CSS is ignored
  • D. Nothing changes
Show answer

A. The doctype tells the browser to use standards mode.

2. Which element contains the content that is displayed in the browser window?

  • A. <body>
  • B. <title>
  • C. <meta>
  • D. <head>
Show answer

A. The head holds information about the page; the body holds what the visitor sees.

3. What is the purpose of lang="en" on the html element?

  • A. It translates the page
  • B. It tells browsers, screen readers and search engines the language of the content
  • C. It sets the font
  • D. It is required for CSS to work
Show answer

B. A screen reader uses it to choose the correct pronunciation.

Interview questions

What is the purpose of the DOCTYPE declaration?

Show answer

<!DOCTYPE html> tells the browser that the document is HTML5 and should be rendered in standards mode. Without it, browsers use quirks mode, which imitates the behaviour of very old browsers and changes how the box model and other layout rules work. It is not an HTML element and has no closing tag.

What is the difference between the head and the body of a document?

Show answer

The head contains information about the document that is not shown as page content: the title, character encoding, viewport settings, description, and links to stylesheets and scripts. The body contains everything that is rendered for the user: text, images, links, forms. There is exactly one of each, both inside the html element.

Key Points to Remember

  • Every HTML5 document starts with <!DOCTYPE html> as the very first line.
  • The <html> element wraps exactly two children: <head> (metadata) and <body> (visible content), in that order.
  • Always set lang on <html> for accessibility, translation tools, and search engines.
  • Well-nested, well-closed tags produce predictable rendering across browsers.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering