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 Englishlang="hi"for Hindilang="ta"for Tamillang="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
The title is shown:
- On the browser tab at the top of the window
- When you bookmark the page
- In Google search results as the clickable blue link
- When you share the page on social media
Write a clear, descriptive title. 'My Page' is boring. 'Priya's Art Portfolio - Paintings, Sketches, and Drawings' is better.
5. The meta charset Tag
<meta charset="UTF-8"> 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.
6. The Viewport Meta Tag
<meta name="viewport" content="width=device-width, initial-scale=1.0">
This is the most important tag for mobile. Here is what it does:
- width=device-width — make the page as wide as the phone's screen
- initial-scale=1.0 — do not zoom in or out when the page loads
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.
7. The Element
Everything the user sees goes inside <body> — headings, paragraphs, images, videos, buttons, forms, everything. Think of it as the stage where the show happens.
8. Comments
HTML comments are notes for yourself or other developers. The browser ignores them:
<!-- This is a comment. It is invisible on the page. -->
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.
9. Nesting Rules
HTML elements can be nested inside each other, but the rules are strict:
- Proper:
<p><strong>Bold text</strong></p> - Wrong:
<p><strong>Bold text</p></strong>
Think of nesting like brackets in maths: [{()}]. Each opening bracket must be closed in the reverse order it was opened. Same with HTML tags.
10. Indentation Best Practices
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:
Hard to read:<ul><li>Apple</li><li>Mango</li></ul>
Easy to read:
<ul>
<li>Apple</li>
<li>Mango</li>
</ul>
Code Examples
<!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>index.html 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 lang="en" 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.<!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><!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><strong> and <em> tags are nested inside the <p> tag. The <li> items are nested inside the <ul>. Notice how each opening tag is closed before the parent tag closes. This is the correct way to nest HTML.<!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><!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>description meta tag appears in Google search results below the blue link — it is your chance to tell users what the page is about. The author meta tag credits the creator. These are invisible on the page itself but very important for SEO (Search Engine Optimisation).<!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><ul> is placed inside an <li> 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.Common Mistakes
Forgetting the DOCTYPE Declaration
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html><!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html><!DOCTYPE html> 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.Wrong Meta Viewport Tag Syntax
<meta name="viewport" content="width=device, scale=1"><meta name="viewport" content="width=device-width, initial-scale=1.0">device-width (with a hyphen), and the scale attribute is initial-scale, not just scale. Memorise this tag exactly. Copy-paste is fine — even professionals do it.Writing Visible Content Inside the Head
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<h1>Welcome</h1>
<p>This is wrong.</p>
</head>
<body>
</body>
</html><!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is correct.</p>
</body>
</html><head> is only for metadata — title, meta tags, links to CSS, links to scripts. All visible content (headings, paragraphs, images, lists, etc.) must go inside <body>. Mixing them causes invalid HTML and breaks many tools that read your page.Wrong Nesting Order
<p><strong>This text is bold</p></strong><p><strong>This text is bold</strong></p><p><strong> means you must close </strong></p>. Think of it like closing a nested pair of brackets: the inner one closes first.Broken HTML Comment Syntax
<!- This is a comment -><!-- This is a comment --><!-- (with two dashes) and end with --> (with two dashes). One dash is wrong. Also, comments cannot contain two dashes in a row inside them — avoid writing -- inside a comment.Summary
- The HTML boilerplate is the standard skeleton every web page needs: DOCTYPE, html, head, body.
- <!DOCTYPE html> must be the very first line. It tells the browser to use modern HTML5 rendering instead of old quirks mode.
- The <html lang="en"> attribute sets the page language, which helps screen readers, translators, and search engines.
- The <head> contains metadata — information about the page that is not visible on the page itself.
- The <title> tag sets the text shown on the browser tab and in search results. It goes inside the head.
- <meta charset="UTF-8"> ensures special characters and emoji render correctly. It should be near the top of the head.
- The viewport meta tag <meta name="viewport" content="width=device-width, initial-scale=1.0"> makes your page look right on phones.
- The <body> contains everything the user sees — headings, paragraphs, images, links, lists, and more.
- HTML comments use <!-- and --> and are invisible on the page. Use them to label sections and leave notes for yourself.
- Indent nested elements by 2 spaces to make your code easy to read. HTML does not require indentation, but it is a professional habit.
- Close tags in the reverse order you opened them — just like closing brackets in mathematics.