HTML declarationDocument structure

<!DOCTYPE html>

The document type declaration on the first line of the file. It tells the browser to render the page in standards mode rather than quirks mode.

Display
none (not displayed)
Permitted content
None. It’s a single declaration with no content and no closing tag
On this page

Example

#
HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lesson 1: your first page | CodeWorlds</title>
  </head>
  <body>
    <h1>My first page</h1>
  </body>
</html>

Definition and usage

#

The <!DOCTYPE html> notation is the document type declaration, doctype for short. It isn’t an element or a tag: it has no attributes or closing tag and displays nothing. It has one job: to tell the browser that the file is a modern HTML document that should be rendered according to current standards.

Browsers have two main rendering modes. In standards mode they apply the current HTML and CSS rules. In quirks mode they imitate the bugs of browsers from over twenty years ago so that very old pages look the way they used to: tables don’t inherit the font size, percentage heights are calculated differently, and CSS even accepts lengths without units, such as width: 200. Quirks mode is switched on precisely when the declaration is missing.

The declaration must stand at the very start of the file, before the <html> tag. Before it, the browser tolerates only whitespace and comments. If it first runs into any text or tag, for example an error message printed by server code, it decides there is no declaration and switches the page to quirks mode. Letter case doesn’t matter, so <!doctype html> works the same.

Older versions of HTML and XHTML used long declarations with an identifier and the address of a document type definition (DTD). Browsers didn’t download those files; they only recognized the string and chose the mode based on it. Some old declarations switch on almost standards mode, which differs from standards mode only in how line heights are calculated, mostly visible with images in table cells. On new pages use only the short <!DOCTYPE html>.

Syntax

#
Syntax
<!DOCTYPE html>

More examples

#
Checking the document mode in JavaScript
HTML
<p>This preview runs in mode: <strong id="current"></strong></p>
<p>Document with the declaration: <strong id="with-doctype"></strong></p>
<p>Document without the declaration: <strong id="without-doctype"></strong></p>

<script>
  const parser = new DOMParser();
  const withDoctype = parser.parseFromString('<!DOCTYPE html><p>Hello</p>', 'text/html');
  const withoutDoctype = parser.parseFromString('<p>Hello</p>', 'text/html');

  document.getElementById('current').textContent = document.compatMode;
  document.getElementById('with-doctype').textContent = withDoctype.compatMode;
  document.getElementById('without-doctype').textContent = withoutDoctype.compatMode;
</script>
Preview

The document.compatMode property returns BackCompat in quirks mode and CSS1Compat otherwise. DOMParser creates two invisible documents in memory that differ only in the declaration, and that is enough for them to run in different modes.

Declarations from older versions of HTML
HTML
<!-- HTML 4.01 Strict: standards mode -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<!-- XHTML 1.0 Transitional: almost standards mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!-- HTML5 and later: this is enough -->
<!DOCTYPE html>

You’ll meet declarations like these in old templates and courses. They still work, but in new code replace them with the short <!DOCTYPE html>.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • In VS Code, type the ! character in an empty .html file and press Tab. Emmet will insert the whole document skeleton together with the declaration.
  • If the layout behaves strangely, e.g. height: 100% works differently than in tutorials or text in tables has the wrong size, type document.compatMode in the console. A result of BackCompat means a missing or misplaced declaration.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.