Difficulty: Beginner
Explain the structure of an HTML document. What belongs in the head versus the body, and how should scripts be loaded?
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.
<!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).<html lang="en">: tells screen readers which pronunciation rules to use and helps translation tools and search engines. Use lang="hi" for Hindi pages.<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.<meta name="viewport" ...>: required for responsive layouts on mobile.<title>: shown in the tab, bookmarks and as the search result headline.charset, viewport, description, robots, canonical link, Open Graph tags<link rel="stylesheet">; these are render-blocking, which is good because it prevents a flash of unstyled contentpreconnect, preload, dns-prefetch<link rel="icon">, <link rel="manifest">defer, async or type="module"<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.
| Attribute | Download | Execution | Order kept? | Best for |
|---|---|---|---|---|
| none | Blocks parser | Immediately | Yes | Rare; tiny inline config |
async | In parallel | As soon as downloaded, pausing the parser | No | Independent scripts like analytics |
defer | In parallel | After parsing, before DOMContentLoaded | Yes | Your app bundle |
type="module" | In parallel | Deferred by default | Yes | Modern 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.
preconnect for critical third-party origins such as a font CDN or payment SDK.<h1> and one <main> per page keeps the outline clear.<!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>© 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.
// 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.
DOCTYPE, Standards mode, head vs body, Render-blocking resources, defer and async