Explain it simply
Explain it simply
Think of an HTML document as a letter in an envelope. The outside of the envelope carries information for the postal service, such as the address and the stamp, which nobody reads as part of the letter: that is the <head>. The sheet inside is the <body>, the content people actually read.
The <!DOCTYPE html> line at the very top is a label that tells the browser "this is a modern HTML document". The <html> element is the whole envelope, and its lang attribute says which language the letter is written in.
Problem → Why → Solution
The problem: fine on your laptop, broken on a phone
The page below has a heading and a paragraph but no doctype, no charset and no viewport. It can look fine on your laptop, yet on a phone the whole page appears shrunk, with tiny text you have to zoom to read. If the server does not state the encoding either, the browser has to guess, and a wrong guess turns the Myanmar text into unreadable symbols. Without the doctype, CSS also runs under the old rules of quirks mode.
<html>
<title>Mingalaba Cafe</title>
<h1>မင်္ဂလာ ကော်ဖီဆိုင်</h1>
<p>နေ့တိုင်း ဖွင့်ပါတယ်။</p>
</html>Why does this happen?
The lines in the head are invisible on the page, so they seem optional, but they give the browser essential instructions. Without a viewport, phone browsers lay the page out at a desktop width (typically around 980px) and shrink it to fit the screen. The charset says how to decode the bytes, and the doctype switches on standards mode.
Core concept: each part of the skeleton
| Part | Job | If it is missing |
|---|---|---|
| <!DOCTYPE html> | switches on standards mode | quirks mode: CSS behaves like very old browsers |
| <html lang="..."> | root element and the content's language | screen readers and translation guess the language |
| <meta charset="UTF-8"> | how to decode bytes into characters | Myanmar text and emoji can break |
| <meta name="viewport"> | lay out at the phone's real width | page shrunk, tiny text on phones |
| <title> | name in tabs, bookmarks and search results | the tab shows the file name or URL |
<!DOCTYPE html> is not a tag; it is a declaration that tells the browser to render by the modern standard. It must come first in the file and is not case-sensitive. Without it, the browser uses quirks mode and calculates things such as box sizes and font sizes inside tables the way browsers did many years ago. Older tutorials sometimes show a long doctype that mentions HTML 4.01 or XHTML and a DTD URL; for new pages, this short form is all you need.
The <head> holds information about the page, called metadata. Put <meta charset="UTF-8"> first in the head, because the standard requires it within the first 1024 bytes of the file. The viewport meta tells phone browsers to lay out at the screen's real width. The <title> should be a specific name for each page. Later, the <link> for a CSS file and a meta description for search results will join them in the head.
The <body> contains all the visible content. The lang attribute goes on <html> and states the language of the whole page: en for English, my for Myanmar. If one part of the page is in another language, add lang to that element as well. The order of elements in the body is the reading order for everyone, including screen reader users, so write the content in a sensible order.
- Doctype: use standards mode
- html lang: which language
- head: information about the page
- body: what people see
The head is for browsers and tools; the body is for people.
General pattern: the document skeleton
<!DOCTYPE html>
<html lang="language-code">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Specific page title</title>
</head>
<body>
visible content goes here
</body>
</html>Replace language-code with a code such as en or my. The order inside head (charset first, then viewport, then title) works for every project.
Example: a complete first document
This document has every part. Notice that the English version uses lang="en" while the Myanmar version of this lesson uses lang="my": the value must match the language of the content.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Page</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>Code breakdown
- Line 1
<!DOCTYPE html>Switches on standards mode. Type document.compatMode in the DevTools Console and you get "CSS1Compat"; without the doctype you get "BackCompat" (quirks mode).
- Line 4
<meta charset="UTF-8">Tells the browser to decode the file as UTF-8, which handles Myanmar, English and emoji alike. Your editor must save the file as UTF-8 too.
- Line 5
<meta name="viewport" content="width=device-width, initial-scale=1.0">width=device-width makes the layout width equal the phone's screen width, and initial-scale=1.0 opens the page without zoom.
- Line 6
<title>My First Page</title>Shown on the tab rather than the page, and used as the name when someone bookmarks it.
- Lines 8-11
<body>Only the h1 and p inside the body are visible on the page. </body> and </html> close the document.
The browser tab reads "My First Page". The page shows the large heading "My First Heading" with the paragraph "My first paragraph." below it. On a phone the text appears at a normal, readable size instead of shrunk.Check it yourself
1. Check the mode
Press F12, open the Console, type document.compatMode and press Enter. "CSS1Compat" means standards mode.
2. Simulate a phone screen
With DevTools open, press Ctrl+Shift+M (Cmd+Shift+M on a Mac) to open the device toolbar. Delete the viewport meta for a moment and refresh to see how small the text becomes.
3. See the tags the browser adds
Remove the <head> and <body> tags and look at the Elements panel. The browser adds them back automatically, but writing them yourself is clearer and safer.
In real projects
How this is used in real projects
In real projects you do not type this skeleton into every file: a template or a framework's root layout produces it once. In a Next.js project, for example, <html lang> is set in the root layout and every page is wrapped by it. That also means a wrong lang or viewport in that one template is wrong across the whole site.
The title is one of the most-seen pieces of text a user meets: on crowded tab bars, in bookmarks and as the link in search results. That is why sites give each page its own title, such as "Menu - Mingalaba Cafe". Bilingual Myanmar and English sites serve each locale's pages with their own lang, so screen readers pick the right voice and browsers offer translation from the right language.
Common mistakes
Common mistakes
1. Using one title for every page
Copy a template and leave <title>Home</title> on every page, and a user with five tabs open cannot tell them apart, while search results show identical link text. Give each page a specific title.
2. Getting lang wrong
Reuse an English template for Myanmar content without changing lang="en" and a screen reader tries to read Myanmar text with an English voice. The code for Myanmar is my, not mm: MM is Myanmar's country code, not a language code.
3. Putting the charset too late
Placing <meta charset> after the title and long comments can push it beyond the first 1024 bytes of the file, by which point the browser may already have guessed the encoding. Always make it the first line inside the head.
Mini challenge
Mini challenge: a Myanmar-language page
Write a Myanmar-language page with the full skeleton for a tea house, and add one English sentence marked with its own lang.
- Include a doctype so that document.compatMode is "CSS1Compat"
- Give <html> the correct lang for Myanmar
- Put charset (first), viewport and a title with the shop's name in the head
- In the body, add a Myanmar heading, a Myanmar paragraph and an English paragraph with lang="en"
<!-- TODO: the doctype that switches on standards mode -->
<!-- TODO: open html with the language code for Myanmar -->
<head>
<!-- TODO: UTF-8 encoding, as the first thing in head -->
<!-- TODO: viewport for phones -->
<!-- TODO: a title with the shop name -->
</head>
<body>
<!-- TODO: h1 with the shop name in Myanmar -->
<!-- TODO: a Myanmar paragraph with the opening hours -->
<!-- TODO: the same hours in English, marked with its own lang -->
</body>
<!-- TODO: close html -->Expected: The tab shows the shop's name, and the page shows a Myanmar heading, a Myanmar paragraph and an English paragraph, all rendered correctly. document.compatMode in the Console returns "CSS1Compat".
Show hint
lang takes an ISO language code, which for Myanmar is my. Adding lang="en" to the English sentence's <p> overrides the language for that element only.
Show solution
<!DOCTYPE html>
<html lang="my">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ရွှေပြည် လက်ဖက်ရည်ဆိုင်</title>
</head>
<body>
<h1>ရွှေပြည် လက်ဖက်ရည်ဆိုင်</h1>
<p>မနက် ၆ နာရီကနေ ညနေ ၆ နာရီအထိ နေ့တိုင်း ဖွင့်ပါတယ်။</p>
<p lang="en">Open every day from 6:00 to 18:00.</p>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- <!DOCTYPE html> switches on standards mode; without it you get quirks mode.
- <html lang> states the content's language: en for English, my for Myanmar.
- The head holds the charset (first), the viewport and a page-specific title.
- Only the content inside the body is visible on the page.
HTML Living Standard — HTML Syntax — WHATWG