Chapter 2 Beginner 22 min min read Updated 2026-04-11

HTML Document Structure and Boilerplate

Practice Questions →

In This Chapter

What Is It?

What Is an HTML Boilerplate?

A boilerplate is the standard skeleton code that every HTML page needs to start with. Think of it like the pre-printed lines on a notebook page — before you can write your story, the lines need to be there. Before you can write any HTML content, you need the boilerplate.

This boilerplate tells the browser three important things: (1) which version of HTML you are using, (2) what language the page is in, (3) how the page should look on phones. Without the boilerplate, the browser has to guess, and it often guesses wrong.

The Standard HTML5 Boilerplate

Here is what every modern HTML page starts with:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page Title</title>
</head>
<body>
  <!-- Your content goes here -->
</body>
</html>

Every professional web page uses some version of this. Memorise it. Type it out by hand a few times. Soon it will feel natural.

Why Does It Matter?

Why Does Every Page Need a Boilerplate?

You might wonder — why can I not just write some <p> tags and call it a day? Here are the five big reasons.

1. Tells the Browser What Version of HTML You Use

Over the years, there have been many versions of HTML: HTML 3, HTML 4, XHTML, HTML5. Each works slightly differently. The <!DOCTYPE html> declaration at the top tells the browser 'render this in modern HTML5 mode'. Without it, some browsers fall back to 'quirks mode', which is an ancient weird rendering from the 1990s that can break your layout.

2. Sets the Language for Accessibility and SEO

The lang="en" attribute on the <html> tag tells screen readers to pronounce words in English, helps browsers offer translation to other languages, and helps search engines show your page to the right audience.

3. Handles Special Characters Correctly

Without <meta charset="UTF-8">, characters like 'e', 'n', 'a', or emoji may show as weird garbled symbols. UTF-8 is the universal encoding that handles every language in the world — English, Hindi, Tamil, Chinese, Arabic, everything.

4. Makes Your Page Look Right on Phones

The viewport meta tag is critical. Without it, your web page will look tiny and zoomed out on a phone, like you are looking at a desktop site through a keyhole. With it, the page fits the screen nicely. More than half of all web browsing happens on phones, so this is not optional.

5. Separates Metadata From Content

The <head> is for information about the page (title, language, styles). The <body> is for the actual visible content. This separation keeps your code organised and works better with browsers, screen readers, and search engines.

Detailed Explanation

Detailed Explanation

1.

This is always the very first line. No spaces above it, nothing before it. It tells the browser 'this is an HTML5 document'. In older versions of HTML (like HTML 4), the DOCTYPE was a long, complicated URL. In HTML5, it is just <!DOCTYPE html> — short and simple.

If you skip the DOCTYPE, browsers may render your page in 'quirks mode', which emulates ancient buggy behaviour from the 1990s. Your CSS might break, your layout might look wrong. Always include it.

2. The Element

The <html> element wraps everything else. Inside it are two main children: <head> and <body>. It usually has a lang attribute to set the page's language:

  • lang="en" for English
  • lang="hi" for Hindi
  • lang="ta" for Tamil
  • lang="fr" for French

3. The Element

The <head> contains metadata — information about the page that is not shown on the page itself. Things that go inside head:

  • <title> — the text shown on the browser tab
  • <meta charset="UTF-8"> — the character encoding
  • <meta name="viewport" ...> — mobile settings
  • <meta name="description" content="..."> — description for search engines
  • <link rel="stylesheet" href="style.css"> — link to CSS file
  • <script src="main.js"></script> — link to JavaScript file

Nothing in the head is displayed as visible content (except the title, which appears on the browser tab, not on the page itself).

4. The Element</h3><p>The title is shown:</p><ul><li>On the browser tab at the top of the window</li><li>When you bookmark the page</li><li>In Google search results as the clickable blue link</li><li>When you share the page on social media</li></ul><p>Write a clear, descriptive title. 'My Page' is boring. 'Priya's Art Portfolio - Paintings, Sketches, and Drawings' is better.</p><h3>5. The meta charset Tag</h3><p><code><meta charset="UTF-8"></code> should be one of the first things in your head. It tells the browser how to decode the bytes of your file into characters. UTF-8 is the universal standard and supports every language. Without it, special characters might show as gibberish.</p><h3>6. The Viewport Meta Tag</h3><p><code><meta name="viewport" content="width=device-width, initial-scale=1.0"></code></p><p>This is the most important tag for mobile. Here is what it does:</p><ul><li><strong>width=device-width</strong> — make the page as wide as the phone's screen</li><li><strong>initial-scale=1.0</strong> — do not zoom in or out when the page loads</li></ul><p>Without this tag, phones assume your page is a desktop site and show it zoomed out. Users see tiny text and have to pinch to zoom. With this tag, the page fits the phone naturally.</p><h3>7. The <body> Element</h3><p>Everything the user sees goes inside <code><body></code> — headings, paragraphs, images, videos, buttons, forms, everything. Think of it as the stage where the show happens.</p><h3>8. Comments</h3><p>HTML comments are notes for yourself or other developers. The browser ignores them:</p><p><code><!-- This is a comment. It is invisible on the page. --></code></p><p>Use comments to explain tricky parts, mark sections of your HTML, or temporarily hide code you are testing. Never write private information in comments because anyone can see them by viewing the page source.</p><h3>9. Nesting Rules</h3><p>HTML elements can be nested inside each other, but the rules are strict:</p><ul><li><strong>Proper:</strong> <code><p><strong>Bold text</strong></p></code></li><li><strong>Wrong:</strong> <code><p><strong>Bold text</p></strong></code></li></ul><p>Think of nesting like brackets in maths: <code>[{()}]</code>. Each opening bracket must be closed in the reverse order it was opened. Same with HTML tags.</p><h3>10. Indentation Best Practices</h3><p>HTML does not care about spaces or tabs — you can write everything on one line and it still works. But good developers indent their code so it is easy to read. Every time you nest an element, indent it by 2 spaces. Compare:</p><p><strong>Hard to read:</strong><br><code><ul><li>Apple</li><li>Mango</li></ul></code></p><p><strong>Easy to read:</strong></p><pre><code><ul> <li>Apple</li> <li>Mango</li> </ul></code></pre> </section> <section id="code-examples" class="content-section"> <h2>Code Examples</h2> <div class="code-example"><div class="code-example-title">The Complete HTML5 Boilerplate</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My First Web Page</title> </head> <body> <h1>Hello, World!</h1> <p>This is my very first HTML5 page with the proper boilerplate.</p> </body> </html></code></pre><div class="code-example-explanation">This is the standard HTML5 boilerplate. Save this as <code>index.html</code> and open it in a browser. You will see a heading and a paragraph. The DOCTYPE on line 1 tells the browser this is HTML5. The <code>lang="en"</code> sets the language to English. The two meta tags in the head set the character encoding and mobile viewport. The title appears on the browser tab. Everything visible goes in the body.</div><div class="code-example-output">A clean page with a big heading 'Hello, World!' and a paragraph below it. The browser tab shows 'My First Web Page'.</div></div> <div class="code-example"><div class="code-example-title">A Page With Comments and Proper Structure</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>About Aarav</title> </head> <body> <!-- Main heading section --> <h1>About Aarav</h1> <p>Hi, I am Aarav. I am learning HTML.</p> <!-- Hobbies section --> <h2>My Hobbies</h2> <ul> <li>Cricket</li> <li>Painting</li> <li>Reading</li> </ul> <!-- Footer note --> <!-- TODO: add a contact form here later --> </body> </html></code></pre><div class="code-example-explanation">This page uses HTML comments to label different sections. The browser does not show comments — they are only visible when you view the page source. Comments help you and other developers understand what each part of the code does. Notice the 'TODO' comment reminding yourself to add a contact form later. This is a professional habit.</div><div class="code-example-output">A page with 'About Aarav' as the heading, an intro paragraph, a 'My Hobbies' sub-heading, and a bulleted list of three hobbies. Comments are not visible.</div></div> <div class="code-example"><div class="code-example-title">A Page With Proper Nesting</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Recipe Page</title> </head> <body> <h1>Chocolate Cake</h1> <p>This is a <strong>delicious</strong> recipe that takes only <em>30 minutes</em> to prepare.</p> <h2>Ingredients</h2> <ul> <li>2 cups of flour</li> <li>1 cup of <strong>cocoa powder</strong></li> <li>3 eggs</li> </ul> </body> </html></code></pre><div class="code-example-explanation">This example shows proper nesting. The <code><strong></code> and <code><em></code> tags are nested inside the <code><p></code> tag. The <code><li></code> items are nested inside the <code><ul></code>. Notice how each opening tag is closed before the parent tag closes. This is the correct way to nest HTML.</div><div class="code-example-output">A recipe page with the title, a paragraph where 'delicious' is bold and '30 minutes' is italic, and a bulleted list of ingredients where 'cocoa powder' is bold.</div></div> <div class="code-example"><div class="code-example-title">Viewport Meta Tag Demo</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Mobile Friendly Page</title> </head> <body> <h1>Mobile Friendly</h1> <p>Because of the viewport meta tag, this page fits your phone screen nicely. Try resizing your browser window or opening this page on your phone.</p> <p>Without the viewport tag, phones would show this page tiny and zoomed out.</p> </body> </html></code></pre><div class="code-example-explanation">Save this and open it on your phone (or use your browser's mobile preview with F12). The text will be readable at phone size. Now try removing the viewport meta tag and reload — the text will look tiny and you will have to pinch to zoom. This tag is what makes your page mobile-friendly.</div><div class="code-example-output">A page that looks normal on desktop and also looks proper on phone screens.</div></div> <div class="code-example"><div class="code-example-title">Using Description and Keywords Meta Tags</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="A beginner-friendly guide to learning HTML and CSS by Priya Sharma."> <meta name="author" content="Priya Sharma"> <title>Learn HTML with Priya</title> </head> <body> <h1>Welcome to My HTML Guide</h1> <p>This page teaches you the basics of HTML in a simple, fun way.</p> </body> </html></code></pre><div class="code-example-explanation">The <code>description</code> meta tag appears in Google search results below the blue link — it is your chance to tell users what the page is about. The <code>author</code> meta tag credits the creator. These are invisible on the page itself but very important for SEO (Search Engine Optimisation).</div><div class="code-example-output">A simple page with a welcome heading and paragraph. The description appears in Google search results.</div></div> <div class="code-example"><div class="code-example-title">Nested Elements with Proper Indentation</div><pre><code class="language-python"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Menu</title> </head> <body> <h1>Cafe Menu</h1> <h2>Drinks</h2> <ul> <li> Hot Drinks <ul> <li>Coffee</li> <li>Masala Chai</li> </ul> </li> <li> Cold Drinks <ul> <li>Mango Lassi</li> <li>Lemon Soda</li> </ul> </li> </ul> </body> </html></code></pre><div class="code-example-explanation">This example shows nested lists. A <code><ul></code> is placed inside an <code><li></code> to create a sub-list. Notice the clean indentation — every time something is nested inside something else, it is indented by two spaces. This makes the code much easier to read. The browser shows indented sub-lists automatically.</div><div class="code-example-output">A menu with a 'Drinks' heading and a bulleted list that has 'Hot Drinks' with a sub-list (Coffee, Masala Chai) and 'Cold Drinks' with a sub-list (Mango Lassi, Lemon Soda).</div></div> </section> <section id="common-mistakes" class="content-section"> <h2>Common Mistakes</h2> <div class="mistake-card"> <h3>Forgetting the DOCTYPE Declaration</h3> <div class="wrong-code"><pre><code class="language-python"><html> <head> <title>My Page</title> </head> <body> <h1>Hello</h1> </body> </html></code></pre></div><div class="error-message">The browser may fall into 'quirks mode', causing CSS to behave strangely and layouts to break in unpredictable ways.</div><div class="correct-code"><pre><code class="language-python"><!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>Hello</h1> </body> </html></code></pre></div><div class="mistake-explanation">The <code><!DOCTYPE html></code> must be the very first line of every HTML page — no spaces or comments before it. It tells the browser to use modern HTML5 rendering. Without it, you get unpredictable behaviour, especially when you start adding CSS.</div> </div> <div class="mistake-card"> <h3>Wrong Meta Viewport Tag Syntax</h3> <div class="wrong-code"><pre><code class="language-python"><meta name="viewport" content="width=device, scale=1"></code></pre></div><div class="error-message">The page may still look zoomed out on phones because the viewport values are wrong.</div><div class="correct-code"><pre><code class="language-python"><meta name="viewport" content="width=device-width, initial-scale=1.0"></code></pre></div><div class="mistake-explanation">The viewport tag has a specific syntax. The width must be <code>device-width</code> (with a hyphen), and the scale attribute is <code>initial-scale</code>, not just <code>scale</code>. Memorise this tag exactly. Copy-paste is fine — even professionals do it.</div> </div> <div class="mistake-card"> <h3>Writing Visible Content Inside the Head</h3> <div class="wrong-code"><pre><code class="language-python"><!DOCTYPE html> <html> <head> <title>My Page</title> <h1>Welcome</h1> <p>This is wrong.</p> </head> <body> </body> </html></code></pre></div><div class="error-message">The browser may still show the heading and paragraph, but it is invalid HTML and causes issues with CSS, JavaScript, and screen readers.</div><div class="correct-code"><pre><code class="language-python"><!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>Welcome</h1> <p>This is correct.</p> </body> </html></code></pre></div><div class="mistake-explanation">The <code><head></code> is only for metadata — title, meta tags, links to CSS, links to scripts. All visible content (headings, paragraphs, images, lists, etc.) must go inside <code><body></code>. Mixing them causes invalid HTML and breaks many tools that read your page.</div> </div> <div class="mistake-card"> <h3>Wrong Nesting Order</h3> <div class="wrong-code"><pre><code class="language-python"><p><strong>This text is bold</p></strong></code></pre></div><div class="error-message">The browser tries to guess what you meant, which can cause layout issues and invalid HTML.</div><div class="correct-code"><pre><code class="language-python"><p><strong>This text is bold</strong></p></code></pre></div><div class="mistake-explanation">When you nest tags, they must be closed in the reverse order you opened them. Opening <code><p><strong></code> means you must close <code></strong></p></code>. Think of it like closing a nested pair of brackets: the inner one closes first.</div> </div> <div class="mistake-card"> <h3>Broken HTML Comment Syntax</h3> <div class="wrong-code"><pre><code class="language-python"><!- This is a comment -></code></pre></div><div class="error-message">The comment is not recognized and shows as plain text on the page.</div><div class="correct-code"><pre><code class="language-python"><!-- This is a comment --></code></pre></div><div class="mistake-explanation">HTML comments start with <code><!--</code> (with two dashes) and end with <code>--></code> (with two dashes). One dash is wrong. Also, comments cannot contain two dashes in a row inside them — avoid writing <code>--</code> inside a comment.</div> </div> </section> <section id="summary" class="content-section"> <h2>Summary</h2> <div class="summary-box"><ul><li>The HTML boilerplate is the standard skeleton every web page needs: DOCTYPE, html, head, body.</li> <li><!DOCTYPE html> must be the very first line. It tells the browser to use modern HTML5 rendering instead of old quirks mode.</li> <li>The <html lang="en"> attribute sets the page language, which helps screen readers, translators, and search engines.</li> <li>The <head> contains metadata — information about the page that is not visible on the page itself.</li> <li>The <title> tag sets the text shown on the browser tab and in search results. It goes inside the head.</li> <li><meta charset="UTF-8"> ensures special characters and emoji render correctly. It should be near the top of the head.</li> <li>The viewport meta tag <meta name="viewport" content="width=device-width, initial-scale=1.0"> makes your page look right on phones.</li> <li>The <body> contains everything the user sees — headings, paragraphs, images, links, lists, and more.</li> <li>HTML comments use <!-- and --> and are invisible on the page. Use them to label sections and leave notes for yourself.</li> <li>Indent nested elements by 2 spaces to make your code easy to read. HTML does not require indentation, but it is a professional habit.</li> <li>Close tags in the reverse order you opened them — just like closing brackets in mathematics.</li></ul></div> </section> </article> <!-- Related Topics --> <section class="related-topics"> <h2>Related Topics</h2> <div class="related-topics-list"><a href="/resources/html-and-css/introduction-to-html" class="related-topic-link">Introduction to HTML - The Language of the Web</a> <a href="/resources/html-and-css/headings-paragraphs-and-text" class="related-topic-link">Headings, Paragraphs, and Text Formatting</a></div> </section> <!-- Chapter Navigation --> <nav class="chapter-nav" aria-label="Chapter navigation"> <a href="/resources/html-and-css/introduction-to-html" class="chapter-nav-card prev"> <span class="chapter-nav-label">Previous Chapter</span> <span class="chapter-nav-title">Introduction to HTML - The Language of the Web</span> </a> <a href="/resources/html-and-css/headings-paragraphs-and-text" class="chapter-nav-card next"> <span class="chapter-nav-label">Next Chapter</span> <span class="chapter-nav-title">Headings, Paragraphs, and Text Formatting</span> </a> </nav> <!-- Practice CTA --> <section class="cta-section"> <h2>Ready to Practice?</h2> <p>Test your understanding with 50+ practice questions on this topic.</p> <a href="/resources/html-and-css/html-structure-and-boilerplate/practice" class="cta-button-primary">Go to Practice Questions</a> </section> <!-- Course CTA --> <section class="cta-section cta-course"> <p>Want to learn web development with a live mentor?</p> <a href="/courses/frontend-development-masterclass-for-teens" class="cta-button-outline">Explore our Frontend Masterclass</a> </section> </main> <div id="footer-placeholder"></div> <script src="/js/components-loader.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script> <script src="/js/resource-interactive.js"></script> </body> </html>