BeginnerLesson 3 of 307 min read

HTML document structure: doctype, head and body

Understand every line of the standard HTML5 skeleton, from doctype to body, and write a good title.

In this lesson you will

  • Write the HTML5 skeleton from memory
  • Explain what head and body are for
  • Write a clear page title and description
  • Use comments and understand whitespace

About 10 minutes

The skeleton every page shares

Open any website, right-click, and choose View Page Source. Beneath the clutter you will find the same frame every time. Learning this skeleton once means you can start any project in seconds.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Page title shown on the tab</title>
  <meta name="description" content="A short summary for search results.">
</head>
<body>
  <h1>Visible content goes here</h1>
</body>
</html>

Line by line

<!DOCTYPE html>
Tells the browser this is modern HTML5. Without it browsers drop into “quirks mode”, an old compatibility behaviour that makes layouts unpredictable. It is not a tag, so it has no closing partner.
<html lang="en">
The root element that holds everything else. The lang attribute states the page language. Screen readers use it to choose the right pronunciation and search engines use it to match visitors. Use en, es, hi, fr and so on.
<head>
Information about the page. Nothing inside it appears on screen, except the title on the tab.
<meta charset="UTF-8">
Chooses the character set. UTF-8 covers every language and emoji. Without it, apostrophes and accents can turn into garbage characters.
<meta name="viewport" ...>
Makes the page fit phone screens instead of shrinking a desktop layout. Leave it out and your site looks tiny on mobile.
<title>
The tab text, the bookmark name and the blue link in search results. One of the most important pieces of text on the page.
<body>
Everything the visitor sees: headings, paragraphs, images, links and forms.
Good to know

Think of the page as a person. The head is the passport and notes about them. The body is the person you meet.

Writing a good title and description

These two lines do more for your visibility than most beginners expect. Search engines show the title as the clickable headline and often show the description under it.

Weak Strong
Title Home Free HTML Course for Beginners | Skills in Minutes
Description Page about HTML Learn HTML step by step with live examples, quizzes and projects. Free, no sign-up.
  • Keep titles under about 60 characters and descriptions under about 155 so they are not cut off.
  • Make every page title unique.
  • Write for people first: say what the visitor will get.

Indentation and readability

Browsers ignore extra spaces and line breaks, so you could write the whole page on one line and it would still work. Humans cannot read that. Indent every nested element by two spaces so you can see the family tree at a glance. VS Code can do it for you with Alt+Shift+F.

<body>
  <main>
    <h1>Welcome</h1>
    <p>Indented children are easy to scan.</p>
  </main>
</body>

How browsers treat whitespace

Here is a surprise. Whatever number of spaces or line breaks you type between words, the browser shows exactly one space. This is called whitespace collapsing, and it is why pressing Enter in your editor does not start a new line on the page. To create real separation you use elements such as paragraphs, which you will meet in lesson 4.

The text below is spread over many lines and spaces. Look at the result: it collapses to one line. Then wrap each sentence in its own paragraph tag to separate them.

<body>
First sentence here.


     Second sentence here.
</body>

Wrap each sentence like this: <p>First sentence here.</p>.

<p>First sentence here.</p>
<p>Second sentence here.</p>

Comments: notes the browser ignores

A comment is text that lives in your file but never appears on the page. It is perfect for leaving yourself reminders or temporarily switching a piece of code off while testing.

<!-- This is a comment. Visitors cannot see it on the page. -->
<p>This paragraph is visible.</p>
<!-- <p>This one is switched off for now.</p> -->
Common mistake

Comments are hidden on the page but not secret. Anyone can read them in View Page Source. Never put passwords, private notes or unfinished client gossip inside a comment.

Common mistakes with the skeleton

  • Forgetting <!DOCTYPE html>, which triggers quirks mode.
  • Placing visible content in the head, where it will not show properly.
  • Using two <body> elements. A page has exactly one.
  • Skipping the viewport meta tag, leaving mobile visitors with a microscopic page.
  • Leaving the title as “Untitled” or “Document”.
Use the shortcut

In VS Code, type ! in an empty .html file and press Tab. It writes the whole skeleton instantly. Add the description and viewport lines if they are missing.

Try a complete page

Add a <title>-style heading, a paragraph and a comment inside the body. The result panel shows only the body, so check that the comment does not appear.

<h1>My page</h1>

Put <!-- your note --> anywhere inside the code. It will vanish from the result.

<!-- Page created on day 3 -->
<h1>My page</h1>
<p>This page has a hidden comment.</p>

Worked example: reading a real page skeleton

Professionals read other people’s code constantly, so practise now. Imagine you open the source of a small bakery website and see this head section.

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sunrise Bakery | Fresh Bread in Pune</title>
  <meta name="description" content="Order fresh sourdough and pastries baked every morning in Pune.">
  <link rel="icon" href="favicon.ico">
  <link rel="stylesheet" href="style.css">
</head>

You can already read it. The page uses UTF-8 and fits phones. The tab will say “Sunrise Bakery | Fresh Bread in Pune”, and Google may show the description beneath it. The link with rel="icon" adds the little picture on the browser tab, and the second link loads a CSS file that holds the styling. The head is where a page connects to its helpers: styles, scripts, icons and more.

Why the order inside the head matters

Browsers read from top to bottom, so put the charset declaration first, ideally within the first 1,024 bytes. The browser needs to know how to decode the text before it reads the title. After that the order is flexible, but a tidy habit is: charset, viewport, title, description, icons, stylesheets, scripts.

Practice exercises

  1. In your own editor, create skeleton.html and type the skeleton from memory without looking. Open it, then compare with the lesson and note what you forgot.
  2. Change lang="en" to lang="hi" and write a paragraph in Hindi or any other language you know. Notice the page still works. The attribute is a label, not a translator.
  3. View the source of three websites you use and find the title, the description and the viewport tag in each.
  4. Delete the doctype line and refresh. Nothing dramatic may happen on a tiny page, but you have just removed a safety net for larger layouts. Put it back.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which element holds everything the visitor sees?

  1. <head>
  2. <body>
  3. <html>
  4. <meta>

The body contains the visible content. The head holds information about the page.

What does <meta charset="UTF-8"> prevent?

  1. Slow loading
  2. Broken or garbled characters
  3. Missing images
  4. Broken links

UTF-8 supports every language and symbol, so accents, apostrophes and emoji display correctly.

You type five blank lines between two words. How many spaces does the browser show?

  1. Five line breaks
  2. One space
  3. None
  4. A paragraph break

Browsers collapse any run of whitespace into a single space.

Which line is a valid comment?

  1. // note
  2. <!-- note -->
  3. /* note */
  4. # note

HTML comments start with <!– and end with –>. The other styles belong to other languages.

Why does the viewport meta tag matter?

  1. It adds colour
  2. It makes the page fit phone screens properly
  3. It speeds up the server
  4. It adds a title

Without it mobile browsers render a zoomed-out desktop layout.

Summary

  • Every page uses the same skeleton: doctype, html, head, body.
  • The head describes the page; the body is what people see.
  • Good titles and descriptions help visitors and search engines.
  • Browsers collapse whitespace and ignore comments.

Next lesson

Headings and paragraphs: the building blocks of readable text.

Questions

Do I have to type the skeleton every time?

No. In VS Code type ! and press Tab. Professionals use templates and shortcuts, but they still know what every line does, which is why we studied it.

What happens if I leave out the doctype?

Browsers switch to quirks mode, which imitates very old browsers. Layouts can break in odd ways, so always include it.

Can the title be different from the main heading?

Yes, and it often is. The title appears on the tab and in search results; the h1 is the headline on the page itself. They should be related.

Does the description appear on the page?

No. It is for search engines and social previews. Search engines may rewrite it, but a good one improves your chance of a click.

Progress is saved in this browser.