Skip to main content

HTML Tags

HTML <html> Tag

Written by Updated HTML <html> Tag exercises

<html> is the root: the one element that contains everything else on the page. It has exactly two children, <head> and <body>, and one attribute you should never leave off: lang.

Try it

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>The root element</title>
</head>
<body>
<h1>Hello</h1>
<p>Everything you see is inside &lt;body&gt;, which is inside &lt;html&gt;.</p>
</body>
</html>

The doctype sits above it, outside the element; it is an instruction to the browser, not part of the document tree. Everything else, including comments and the <head>, is a descendant of <html>.

The lang attribute

lang="en" tells software what language the page is written in. It is a single attribute and it affects more than people expect:

  • Screen readers switch pronunciation rules. English text read with Hindi rules, or vice versa, is close to unintelligible.
  • Browsers use it to pick hyphenation dictionaries, choose fonts for CJK text, and decide whether to offer translation.
  • Search engines use it as one signal for which language results the page belongs in.
  • CSS can target it: :lang(hi) { font-family: … }, and quotes pick the right quotation marks per language.

Use a BCP 47 tag: en, en-GB, hi, hi-Latn for romanised Hindi. Keep it to the language of the main content; mark a phrase in another language with lang on that element (<span lang="fr">), not by changing the root.

Other attributes

AttributeWhat it does
langLanguage of the document. Effectively required.
dirltr (default), rtl for Arabic, Hebrew, Urdu, or auto. Sets the base text direction for the whole page.
class, data-*Common hooks for JavaScript-set state such as a theme: <html data-theme="dark">.
xmlnsOnly needed in XHTML served as XML. In HTML it is ignored; leave it out.
manifestObsolete (the old application cache). Use a service worker instead.

A realistic example: theme switching on the root

The root is the natural place to store page-wide state, because every element can see it via CSS. This is how most dark-mode toggles work.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Theme on the root</title>
  <style>
    html { --bg: #ffffff; --fg: #0f172a; }
    html[data-theme="dark"] { --bg: #0f172a; --fg: #f8fafc; }
    body { background: var(--bg); color: var(--fg); font-family: system-ui; transition: background .2s; }
  </style>
</head>
<body>
<button id="toggle">Toggle theme</button>
<p>The colours come from custom properties set on &lt;html&gt;.</p>

<script>
  const root = document.documentElement;   // the <html> element
  document.getElementById("toggle").addEventListener("click", () => {
    const dark = root.dataset.theme === "dark";
    root.dataset.theme = dark ? "light" : "dark";
  });
</script>
</body>
</html>

document.documentElement is the JavaScript name for <html>. Setting data-theme there flips every custom property at once, and because <body> and everything below it read those properties, the whole page changes.

html vs body in CSS

Two things trip people up. First, a background on <body> is propagated to the canvas, the whole viewport, as long as <html> has no background of its own; set one on <html> and the body background suddenly stops at the body's edge. Second, height: 100% on <body> means "100% of <html>", and <html> is only as tall as its content unless you set html { height: 100% } too, which is why min-height: 100vh on body is the more reliable way to fill the screen.

Common mistakes

1. No lang

The most common accessibility failure on the web, and the easiest to fix. Every automated checker flags it; every screen reader user hears the difference.

2. The wrong lang

Templates copied from elsewhere ship with lang="en" on a page that is entirely in Hindi. That is worse than no attribute: the screen reader confidently uses the wrong rules.

3. Content outside body

Text or elements after </body>, or before <head>, is not an error you will see: browsers quietly move it inside. But it moves differently in different browsers and confuses anyone reading the source. Keep everything in the two children.

4. Setting font-size on html in px

html { font-size: 16px } overrides the user's browser text-size setting, so someone who set 20px for readability gets 16. Use a percentage, html { font-size: 100% }, or leave it alone. rem units are relative to this element, so this decision affects every rem on the page.

5. Multiple html elements or a missing one

Pasting a whole page into another page's <div>, a common mistake with copied examples, produces nested <html> and <head> elements. Browsers discard the extras, but the styles and scripts in the inner <head> may or may not run. If you omit <html> entirely, the browser inserts it, which is why old pages without it still work; there is no reason to rely on that.

Browser support

Universal: it is the document. dir="auto" is supported in all current browsers.

Related chapters

HTML <html> Tag exercises

Wrap the whole page

Write a page whose root element says the language is English (en). In the head put the title Welcome. In the body put a level 1 heading: Welcome.

Result

Check your answer to see it here.