Skip to main content

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 with dir
  • What quirks mode is, and how to avoid it
  • A few useful extras for the root element

The Big Idea​

index.html
<!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:

ModeWhenWhat happens
Standards modeThe page starts with <!DOCTYPE html>The browser follows the current rules for HTML and CSS
Quirks modeThe doctype is missing or wrongThe 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.

check-mode.html
<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:

  1. There is exactly one <html> element on a page.
  2. It has exactly two children, in this order: <head> first, then <body>.
  3. Its closing tag </html> comes last.
ChildHoldsShown to the visitor?
<head>Metadata: the title, character set, links to styles and scriptsNo (except effects such as the tab title)
<body>All the visible content: headings, text, images, formsYes
Browsers are forgiving

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 benefitsHow
Screen readersChoose the right voice and pronunciation rules. Without it, a Hindi page can be read with an English voice
Translation toolsKnow whether to offer to translate, and from which language
Spell checkersCheck your text against the right dictionary
Search enginesUnderstand the language of the page
BrowsersPick 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:

CodeMeaning
enEnglish
en-GBEnglish as used in Great Britain
en-USEnglish as used in the United States
hiHindi
arArabic
pt-BRPortuguese as used in Brazil
zh-HansChinese in Simplified script
zh-HantChinese 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">
ValueMeaning
ltrLeft to right (the default)
rtlRight to left
autoThe 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.

Use dir="auto" for user content

When 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">
AttributeCommon use
classA hook for CSS or scripts, such as a theme or "script is available" flag
data-*Custom data, such as data-theme="dark"
idRarely needed on the root
stylePossible, 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​

  1. Always start with <!DOCTYPE html>.
  2. Always set lang, and make it match the main language of the page.
  3. Set dir="rtl" for right-to-left languages.
  4. Mark passages in other languages with their own lang.
  5. Write <head> and <body> explicitly, even though browsers can add them.
  6. Use one <html> element per document.
  7. Validate your page with a checker such as the W3C validator, to catch structure problems.

Accessibility Checklist​

  • lang is set on <html>, and is a valid code.
  • lang matches 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).
A wrong lang is worse than none

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 lang or dir from 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​

  1. No doctype, causing quirks mode.
  2. A comment or text before the doctype.
  3. No lang, or lang="".
  4. A wrong lang, such as en on a Hindi page.
  5. Invalid codes, such as lang="english".
  6. More than one <html> element, for example from joining two files together.
  7. Content outside <body>.
  8. Forgetting dir="rtl" for Arabic or Hebrew.
  9. Putting <body> before <head>.
  10. Using old doctypes copied from older tutorials.
  11. Relying on the browser to add missing elements, and then wondering why lang cannot be set.
  12. Using left and right in CSS when the page may be right-to-left.

Practice Time​

Try these in your editor:

  1. Write a complete page with a doctype, <html lang>, a head, and a body.
  2. Delete the doctype, open the page, and check document.compatMode in the browser's console.
  3. Change lang to hi, and write a sentence in Hindi.
  4. Make a page in Arabic or Hebrew, with dir="rtl", and see the layout flip.
  5. Add a <span lang="fr"> inside an English sentence.
  6. Add data-theme="dark" to <html>, and use it in a CSS selector.
  7. Use the W3C validator on your page, and read what it reports.
  8. Add a comment before <html>.
  9. Replace margin-left with margin-inline-start, and test your page in both directions.
  10. Bonus: look at the <html> element of three famous websites, and compare their lang values.

Quick Recap​

Click a question to check your answer.

1. What does &lt;!DOCTYPE html&gt; 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 &lt;html&gt;?

<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 lang on <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, and body elements, 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.