This page is the HTML syntax that authors write, the rules of chapter 13.1 of the HTML Living Standard. The rules for what a browser does with anything else, valid or not, are on the parsing page.
A document
A document is, in this order: an optional byte order mark, any number of comments and
ASCII white space, a DOCTYPE, more comments and white space, the html
element, and finally comments and white space again. White space here is space, tab,
line feed, form feed and carriage return.
The DOCTYPE is required by the syntax, but its only job is to keep browsers out of
quirks mode. Its two conforming forms are <!DOCTYPE html> and the
legacy string for tools that cannot write the short one:
<!DOCTYPE html SYSTEM "about:legacy-compat">. Names are not case
sensitive, so <!doctype HTML> is the same thing. See
versions and doctypes for the older strings.
Elements and tags
An element is a start tag, content, and an end tag: <p>text</p>.
Tag names are not case sensitive. A start tag is <, the name, optional
attributes, optional white space and >; an end tag is </,
the name, optional white space and >. A / before the
> of a start tag is allowed on void elements and on foreign elements
(SVG and MathML), where it closes the element; on any other element it has no effect,
and the element stays open.
The six kinds of element
| Kind | Elements | Content and end tag |
|---|---|---|
| Void | area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr | No content and no end tag |
| The template element | template | Contents held apart from the document |
| Raw text | script, style | Text up to the matching end tag; no references, no tags |
| Escapable raw text | textarea, title | Text with character references, no tags |
| Foreign | elements in the MathML and SVG namespaces | Self-closing allowed; CDATA sections allowed |
| Normal | all the others | Text, character references, other elements and comments |
The parser also treats noscript, iframe, noembed,
noframes, xmp and plaintext as raw text,
though their content models are defined separately. The void elements
basefont, bgsound, frame, keygen
and param are obsolete but still have no end tag in the parser.
Attributes
Attributes follow the tag name in any order, each name once, separated by white
space. A name may hold any characters except control characters, the space and
" ' > / =, and non-characters; names on HTML elements are not case
sensitive. Values come in four forms:
| Form | Example | Rules for the value |
|---|---|---|
| Empty | <input disabled> | Just the name: the value is the empty string |
| Unquoted | <input value=yes> | No white space and none of " ' = < > `, and not empty |
| Single-quoted | <input type='checkbox'> | Anything but '; references are decoded |
| Double-quoted | <input name="be evil"> | Anything but "; references are decoded |
White space is allowed around the =. A boolean attribute is true by
being there at all; its value may be empty or the attribute’s own name. See the
attribute index.
Text and comments
Comments
A comment is <!--, text, -->. The text may not start
with > or ->, may not contain <!--,
--> or --!>, and may not end with
<!-.
CDATA sections
CDATA sections, <![CDATA[ … ]]>, exist only inside foreign
content, in svg and math. In ordinary HTML the parser reads
one as a comment that ends at the first >.
Processing instructions
A processing instruction such as <?php echo 1 ?> is read as its
own node, ending at the first >. The targets xml and
xml-stylesheet are not allowed and become comments, like any other
<? that does not start a valid target. PHP’s own
?> ends the instruction, so a > inside PHP code ends it
early.
Character references
Text and attribute values can use character references:
& by name, é in decimal, é
in hexadecimal. The rules are on the
character references page.
Characters that need care
An ampersand before a character that could continue a reference, and
<, are written as references in text; a quote that matches the
delimiter is written as a reference inside a quoted attribute. The file itself must
not contain the control characters U+0000 to U+001F other than ASCII white space, or
noncharacters. Line endings can be CR, LF or CRLF; the parser turns them all
into LF.