HTML Document Structure

Difficulty: Beginner

Question

Explain the structure of an HTML document. What belongs in the head versus the body, and how should scripts be loaded?

Answer

An HTML document starts with <!DOCTYPE html>, which puts the browser in standards mode, followed by a single <html lang="..."> root with two children: <head> for metadata the user does not see (charset, viewport, title, stylesheets, SEO tags) and <body> for everything rendered on the page. Scripts can go in either place, but in the head they should use defer or type="module" so they do not block parsing.

The skeleton and why each line exists

  1. <!DOCTYPE html>: without it browsers fall back to quirks mode, which emulates old browser bugs (for example, a different box model for some elements and odd table sizing).
  2. <html lang="en">: tells screen readers which pronunciation rules to use and helps translation tools and search engines. Use lang="hi" for Hindi pages.
  3. <meta charset="utf-8">: must appear within the first 1024 bytes so the parser decodes text correctly before it reads content. Without it you can see garbled ₹ symbols.
  4. <meta name="viewport" ...>: required for responsive layouts on mobile.
  5. <title>: shown in the tab, bookmarks and as the search result headline.

What goes in the head

  • Metadata: charset, viewport, description, robots, canonical link, Open Graph tags
  • Stylesheets via <link rel="stylesheet">; these are render-blocking, which is good because it prevents a flash of unstyled content
  • Resource hints: preconnect, preload, dns-prefetch
  • Icons and manifest: <link rel="icon">, <link rel="manifest">
  • Scripts with defer, async or type="module"
  • Structured data in <script type="application/ld+json">

The body holds visible content: landmarks such as <header>, <main> and <footer>, headings, forms and media. If you put a <div> inside the head, the parser closes the head early and moves it into the body.

Script loading: the part interviewers dig into

AttributeDownloadExecutionOrder kept?Best for
noneBlocks parserImmediatelyYesRare; tiny inline config
asyncIn parallelAs soon as downloaded, pausing the parserNoIndependent scripts like analytics
deferIn parallelAfter parsing, before DOMContentLoadedYesYour app bundle
type="module"In parallelDeferred by defaultYesModern ES module apps

A classic <script src> in the head stops HTML parsing until the file downloads and runs, which delays first paint. The old fix was moving scripts to the end of the body; defer achieves the same while letting the download start early.

Practical tips and gotchas

  • Put CSS before scripts. A parser-blocking script must wait for pending stylesheets because it might read computed styles.
  • Use preconnect for critical third-party origins such as a font CDN or payment SDK.
  • One <h1> and one <main> per page keeps the outline clear.
  • Validate with the W3C validator when something renders oddly; unclosed tags and stray elements in the head are common causes.
How the browser processes a document. Head resources are discovered first, deferred scripts run only after parsing finishes.
  1. Read DOCTYPE and pick standards mode: Missing DOCTYPE triggers quirks mode.
  2. Parse head and fetch CSS: Stylesheets block rendering until loaded.
  3. Start downloading defer and async scripts: Downloads run in parallel with parsing.
  4. Parse body into the DOM: Async scripts may interrupt when ready.
  5. Run deferred scripts in order: Happens before DOMContentLoaded fires.
  6. Fire load event: After images and other subresources finish.

Code examples

Production-ready HTML5 skeleton

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Internship Tracker | Example</title>
  <meta name="description" content="Track every internship application in one place.">
  <link rel="canonical" href="https://example.com/tracker">

  <!-- Warm up connections to critical origins -->
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link rel="stylesheet" href="/css/main.css">
  <link rel="icon" href="/favicon.svg" type="image/svg+xml">

  <!-- Downloads now, runs after parsing, order preserved -->
  <script defer src="/js/app.js"></script>
  <!-- Independent third party, runs whenever it arrives -->
  <script async src="https://analytics.example.com/a.js"></script>
</head>
<body>
  <header><nav aria-label="Primary"><a href="/">Home</a></nav></header>
  <main>
    <h1>Your applications</h1>
  </main>
  <footer><p>&copy; 2026 Example</p></footer>
</body>
</html>

Metadata, CSS and scripts live in the head; defer lets the app bundle download early without blocking parsing, while async suits scripts that do not depend on anything else.

Observing load events

// Runs after the DOM is parsed and all defer scripts have run
document.addEventListener('DOMContentLoaded', () => {
  console.log('DOM ready', document.readyState); // "interactive"
});

// Runs after images, stylesheets and iframes have loaded too
window.addEventListener('load', () => {
  console.log('Everything loaded', document.readyState); // "complete"
});

DOMContentLoaded is the right hook for DOM setup; waiting for load delays your code until every image is downloaded.

Key points

  • DOCTYPE html triggers standards mode; without it you get quirks mode.
  • meta charset must be in the first 1024 bytes of the document.
  • lang on the html element helps screen readers, translation and search engines.
  • CSS in the head is render-blocking by design; classic scripts are parser-blocking.
  • defer keeps order and runs before DOMContentLoaded; async runs as soon as it downloads.
  • Module scripts are deferred by default.

Common follow-up questions

  • What is the difference between DOMContentLoaded and load?
  • What happens if you omit the DOCTYPE?
  • When would you choose async over defer?
  • What do preload and preconnect do?
  • Why should stylesheets come before scripts in the head?

Interview tips

  • Draw or describe the async vs defer timeline; it is the most common follow-up on this topic.
  • Mention quirks mode and the 1024-byte charset rule to stand out from rote answers.
  • Tie script loading to performance metrics like first paint and LCP.

Concepts covered

DOCTYPE, Standards mode, head vs body, Render-blocking resources, defer and async

More HTML & CSS Interview questions