The HTML Element, Doctype and lang
Every web page, no matter how big, begins the same way: with a doctype and one <html> element. Think of <html> as the cover of a book. Everything else (the title page, the contents, the chapters) sits inside it. In this lesson you learn what the root element does, why a single attribute, lang, matters so much, and what a browser does when you forget these basics.
What You Will Learnβ
- What the doctype is, and why
<!DOCTYPE html>goes first - What the
<html>element is, and why there is only one - How the page is split into
<head>and<body> - Setting the language with
lang, and the direction withdir - What quirks mode is, and how to avoid it
- A few useful extras for the root element
The Big Ideaβ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My First Page</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Switch the parts off one at a time, and read what the page loses. The line-by-line notes below explain why.
Part 1: The Doctypeβ
<!DOCTYPE html>
The very first line of every page. It is not an HTML element, and it is not a tag you close. It is an instruction to the browser: "This page follows the modern HTML rules. Please use them."
- It is not case-sensitive (
<!doctype html>works too), but most people write it as above. - It must come first, with nothing in front of it, apart from (in rare cases) a byte-order mark. A comment or text before it can push some older browsers into quirks mode.
- Older HTML versions had long, hard-to-remember doctypes. Modern HTML has just this one.
Standards Mode and Quirks Modeβ
Browsers have two ways of reading a page:
| Mode | When | What happens |
|---|---|---|
| Standards mode | The page starts with <!DOCTYPE html> | The browser follows the current rules for HTML and CSS |
| Quirks mode | The doctype is missing or wrong | The browser imitates the buggy behaviour of 1990s browsers, so old pages keep working |
Quirks mode changes real things: how box sizes are calculated, how some table and image spacing works, and some font-size rules. Your layout may look fine in one browser and strange in another. The fix is simple: always write the doctype.
Try it below. The page at the bottom reports its own mode, and two small frames show the difference in box sizing.
<script>
// "CSS1Compat" means standards mode. "BackCompat" means quirks mode.
console.log(document.compatMode);
</script>
Part 2: The <html> Elementβ
<html lang="en">
<head> ... </head>
<body> ... </body>
</html>
The <html> element is the root of the document. Every other element is inside it, directly or indirectly. Three rules:
- There is exactly one
<html>element on a page. - It has exactly two children, in this order:
<head>first, then<body>. - Its closing tag
</html>comes last.
| Child | Holds | Shown to the visitor? |
|---|---|---|
<head> | Metadata: the title, character set, links to styles and scripts | No (except effects such as the tab title) |
<body> | All the visible content: headings, text, images, forms | Yes |
If you leave out <html>, <head>, or <body>, the browser quietly creates them for you while it reads the page. That is why sloppy pages still display. But you should always write them, so that your pages are predictable, and so that you can add the lang attribute, which you cannot do on an element that is not there.
Part 3: The lang Attributeβ
If you remember only one thing from this lesson, remember this: put lang on <html>.
<html lang="en">
It says what human language the page is written in. That one attribute helps in many ways:
| Who benefits | How |
|---|---|
| Screen readers | Choose the right voice and pronunciation rules. Without it, a Hindi page can be read with an English voice |
| Translation tools | Know whether to offer to translate, and from which language |
| Spell checkers | Check your text against the right dictionary |
| Search engines | Understand the language of the page |
| Browsers | Pick suitable fonts, hyphenation, and quotation marks |
| CSS | :lang() selectors and language-specific styling |
Writing Language Codesβ
Codes follow the BCP 47 standard. They are a short language code, optionally followed by a region or script:
| Code | Meaning |
|---|---|
en | English |
en-GB | English as used in Great Britain |
en-US | English as used in the United States |
hi | Hindi |
ar | Arabic |
pt-BR | Portuguese as used in Brazil |
zh-Hans | Chinese in Simplified script |
zh-Hant | Chinese in Traditional script |
Keep it simple. A plain en is usually enough. Add a region only when the difference matters.
Different Languages Inside One Pageβ
If a section is in another language, put lang on that element too:
<html lang="en">
<body>
<p>The French word for "thank you" is <span lang="fr">merci</span>.</p>
<blockquote lang="es">
<p>MΓ‘s vale tarde que nunca.</p>
</blockquote>
</body>
</html>
A screen reader can then switch voices for just that part.
Part 4: The dir Attribute (Reading Direction)β
Most languages are read left to right. Some, such as Arabic, Hebrew, Persian, and Urdu, are read right to left. Set the direction on the root element:
<html lang="ar" dir="rtl">
| Value | Meaning |
|---|---|
ltr | Left to right (the default) |
rtl | Right to left |
auto | The browser works it out from the first strong letter |
The builder above adds dir="rtl" automatically when you choose Arabic or Hebrew. With rtl, the browser mirrors the page flow: text aligns to the right, and CSS logical properties (such as margin-inline-start) flip to match. Prefer logical properties over left and right, so your layout works in both directions.
dir="auto" for user contentWhen you show text written by visitors, such as comments, you do not know which language they used. Put dir="auto" on that element, and let the browser decide.
Part 5: Other Things You Can Put on <html>β
The root element accepts the global attributes that every element has. A few are commonly useful:
<html lang="en" dir="ltr" class="no-js" data-theme="light">
| Attribute | Common use |
|---|---|
class | A hook for CSS or scripts, such as a theme or "script is available" flag |
data-* | Custom data, such as data-theme="dark" |
id | Rarely needed on the root |
style | Possible, but better kept in a stylesheet |
You will often see CSS variables set on the root with the :root selector, which targets <html>:
:root {
--brand: #1d4ed8;
}
Part 6: Comments and Whitespaceβ
You may put comments before the <html> element, but not before the doctype in a page you want to be safe in all browsers:
<!DOCTYPE html>
<!-- Last updated: 2026 -->
<html lang="en">
Browsers ignore blank lines and extra spaces between the doctype, <html>, <head>, and <body>. Use indentation to make the structure clear for people.
Good Habitsβ
- Always start with
<!DOCTYPE html>. - Always set
lang, and make it match the main language of the page. - Set
dir="rtl"for right-to-left languages. - Mark passages in other languages with their own
lang. - Write
<head>and<body>explicitly, even though browsers can add them. - Use one
<html>element per document. - Validate your page with a checker such as the W3C validator, to catch structure problems.
Accessibility Checklistβ
-
langis set on<html>, and is a valid code. -
langmatches the actual language of the content. - Passages in a different language have their own
lang. -
dir="rtl"is set for right-to-left pages. - The page has a
<title>(see the next lessons). - Zooming is not blocked (see the Viewport lesson).
If you copy a template that says lang="en" and write your page in another language, screen readers will pronounce your words with English rules, and the result can be hard to understand. Always check it.
Security and Privacy Notesβ
- Do not build
langordirfrom raw visitor input on a server without checking it against a list of allowed values. Unescaped input in an attribute can allow injected markup. - Remove comments before
<html>that reveal private details, such as names, internal paths, or notes about unreleased work. Comments are public. - A page that is missing a doctype can behave differently in different browsers, and subtle layout differences can break security-related interface elements (such as a consent banner that ends up hidden behind another element). Standards mode makes your page predictable.
Global Audience Notesβ
- Choose the language code for your readers, not for your own location. A page for Portuguese speakers in Brazil is
pt-BR, wherever you live. - Translated versions should each have their own
lang. You can also link them together with<link rel="alternate" hreflang="β¦">(see the Link lesson). - Right-to-left support is more than
dir="rtl": use logical CSS properties, and test icons that point in a direction (arrows, back buttons). - Mixed-direction text (for example, an English brand name in an Arabic sentence) is handled by the browser, but use the
<bdi>element for names from unknown sources. - Do not guess the language from the visitor's country. Offer a visible language switcher.
Common Mistakes to Avoidβ
- No doctype, causing quirks mode.
- A comment or text before the doctype.
- No
lang, orlang="". - A wrong
lang, such asenon a Hindi page. - Invalid codes, such as
lang="english". - More than one
<html>element, for example from joining two files together. - Content outside
<body>. - Forgetting
dir="rtl"for Arabic or Hebrew. - Putting
<body>before<head>. - Using old doctypes copied from older tutorials.
- Relying on the browser to add missing elements, and then wondering why
langcannot be set. - Using
leftandrightin CSS when the page may be right-to-left.
Practice Timeβ
Try these in your editor:
- Write a complete page with a doctype,
<html lang>, a head, and a body. - Delete the doctype, open the page, and check
document.compatModein the browser's console. - Change
langtohi, and write a sentence in Hindi. - Make a page in Arabic or Hebrew, with
dir="rtl", and see the layout flip. - Add a
<span lang="fr">inside an English sentence. - Add
data-theme="dark"to<html>, and use it in a CSS selector. - Use the W3C validator on your page, and read what it reports.
- Add a comment before
<html>. - Replace
margin-leftwithmargin-inline-start, and test your page in both directions. - Bonus: look at the
<html>element of three famous websites, and compare theirlangvalues.
Quick Recapβ
Click a question to check your answer.
1. What does <!DOCTYPE html> do?
It tells the browser to use standards mode, so it follows the modern rules for HTML and CSS. Without it, the browser may use quirks mode.
2. What are the two children of <html>?
<head> (information about the page) and <body> (the visible content), in that order.
3. Why is lang important?
It helps screen readers pronounce text correctly, lets translation tools and spell checkers work properly, and helps search engines understand the page.
4. How do you mark a right-to-left page?
Set dir="rtl" on the <html> element, together with the right lang, for example <html lang="ar" dir="rtl">.
5. What is quirks mode?
A browser mode that imitates the old, buggy behaviour of 1990s browsers, used when the doctype is missing or wrong. It can make layouts behave differently from what you expect.
6. How do you mark a French word inside an English page?
Wrap it in an element, such as a <span>, and give that element lang="fr".
Key Takeawaysβ
- Start every page with
<!DOCTYPE html>to avoid quirks mode. - The
<html>element is the single root. It contains one<head>and one<body>. - Always set
langon<html>, and make it match the real language. - Use
dir="rtl"for right-to-left languages, and prefer logical CSS properties. - Mark passages in other languages with their own
lang. - Browsers add missing
html,head, andbodyelements, but you should write them yourself. - Comments are public, and input used in attributes must be checked.
Next, look inside the first child of the root: Head.