HTML Guides
Learn how to identify and fix common HTML validation errors flagged by the W3C Validator — so your pages are standards-compliant and render correctly across every browser. Also check our Accessibility Guides.
Every valid HTML document follows a strict structure: a <!DOCTYPE html> declaration, followed by an <html> element containing exactly two children — <head> and <body>. All visible content and its associated markup must live inside <body>. Once the browser encounters the </body> closing tag, it expects nothing else except the closing </html> tag.
When an end tag appears after </body> has been closed, the browser's parser enters an error-recovery mode. While most browsers will attempt to handle this gracefully, the behavior is not guaranteed to be consistent. This can lead to unexpected DOM structures, which may cause issues with JavaScript that relies on the DOM tree, break CSS styling, or create accessibility problems for screen readers that depend on proper document structure.
This issue commonly arises from:
- Extra or duplicate closing tags — for example, an extra
</div>that doesn't match any open element inside<body>. - Accidentally placing elements after
</body>— such as moving a<script>or</div>outside the body during editing. - Copy-paste errors — fragments of markup pasted in the wrong location.
- Template or CMS issues — server-side templates that inject closing tags in the wrong order.
To fix this, review your document structure carefully. Make sure every opening tag inside <body> has a matching closing tag also inside <body>. Ensure nothing appears between </body> and </html> except whitespace.
Examples
Incorrect — end tag after body is closed
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<div>
<p>Hello world</p>
</div>
</body>
</div>
</html>
Here, an extra </div> sits between </body> and </html>. This triggers the validation error because the body has already been closed when the parser encounters </div>.
Incorrect — script tag placed after body
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<p>Hello world</p>
</body>
<script src="app.js"></script>
</html>
While browsers will typically still execute the script, placing it after </body> is invalid and produces this warning.
Correct — all content and tags inside body
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<div>
<p>Hello world</p>
</div>
<script src="app.js"></script>
</body>
</html>
All elements, including <script> tags, are properly nested inside the <body> element. The only thing that follows </body> is </html>.
Debugging tip
If your document is large and you're having trouble finding the stray tag, try these approaches:
- Count your tags — make sure every opening tag (like
<div>) has exactly one matching</div>inside<body>. - Use an editor with bracket matching — most code editors can highlight matching open/close tags, making orphaned tags easy to spot.
- Check the validator's line number — the W3C validator reports the line where the stray end tag was found, which points you directly to the problem.
- Indent properly — consistent indentation makes structural problems visually obvious.
The HTML parser reads element attributes by looking for a name, then an =, then a value. When it encounters = in a spot where it expects a name to start, it means something has gone wrong with the attribute syntax. This can happen in several ways: an attribute name is missing entirely before the =, there's a double == instead of a single =, a space was accidentally inserted in the middle of an attribute name, or a previous attribute's value was left unquoted causing the parser to lose track of where one attribute ends and the next begins.
This is a problem because browsers may silently discard the malformed attribute, apply it incorrectly, or even misinterpret surrounding attributes. This leads to unpredictable behavior across different browsers. Screen readers and other assistive technologies may also fail to interpret the element correctly, creating accessibility issues.
How to Fix It
- Check for missing attribute names — Make sure every
=sign is preceded by a valid attribute name likeclass,id,src, etc. - Check for double equals signs — Replace any
==with a single=. - Check for spaces in attribute names — Attribute names cannot contain spaces. Remove accidental spaces within names.
- Check for unquoted attribute values — Always quote attribute values with double quotes. An unquoted value containing special characters can cause the parser to misread subsequent attributes.
- Check for stray
=characters — Look for leftover=signs from copy-paste errors or incomplete edits.
Examples
Missing attribute name before =
An = appears with no attribute name before it:
<!-- ❌ Bad: no attribute name before = -->
<img ="photo.jpg" alt="A photo">
<!-- ✅ Fixed: added the src attribute name -->
<img src="photo.jpg" alt="A photo">
Double equals sign
A typo creates == instead of =:
<!-- ❌ Bad: double equals sign -->
<a href=="https://example.com">Link</a>
<!-- ✅ Fixed: single equals sign -->
<a href="https://example.com">Link</a>
Space inside an attribute name
A space splits what should be one attribute name into two tokens, leaving an orphaned =:
<!-- ❌ Bad: space in "data-value" splits the attribute -->
<div data- value="10">Content</div>
<!-- ✅ Fixed: no space in the attribute name -->
<div data-value="10">Content</div>
Unquoted attribute value causing a cascade
When a value isn't quoted, the parser can misinterpret where it ends, causing the next attribute's = to appear in an unexpected position:
<!-- ❌ Bad: unquoted class value with space causes parsing issues -->
<p class=my class id="intro">Text</p>
<!-- ✅ Fixed: properly quoted attribute value -->
<p class="my-class" id="intro">Text</p>
Stray = from an incomplete edit
A leftover = from a deleted or partially edited attribute:
<!-- ❌ Bad: stray = left over after removing an attribute -->
<div class="container" = >Content</div>
<!-- ✅ Fixed: removed the stray = -->
<div class="container">Content</div>
When the HTML parser reads a tag, it expects a specific sequence: the tag name, then optional attributes (each with a name, optionally followed by = and a value), and finally a closing >. If the parser encounters a < character in a position where it's looking for an attribute name, it means something has gone structurally wrong. The parser interprets the < as the beginning of a new tag, but since it's still inside the current tag's definition, it raises this error.
This issue commonly arises from three scenarios:
- A stray
<character inside a tag — perhaps from a typo or a copy-paste error. - A missing
>on a previous tag — causing the parser to treat the next tag's<as though it's still part of the previous tag's attributes. - Incorrectly nested or overlapping tags — where one element is accidentally placed inside another element's opening tag.
This matters because browsers handle malformed HTML unpredictably. One browser might silently ignore the stray character, while another might drop the entire element or render content incorrectly. Fixing these structural errors ensures consistent rendering across all browsers and improves accessibility, since screen readers and other assistive technologies rely on well-formed markup to interpret page structure.
Examples
Stray < character inside a tag
A common typo where an extra < appears before the closing of a self-closing tag or between attributes:
<!-- ❌ Stray "<" inside the img tag -->
<img src="photo.jpg" alt="smiling cat" < />
Remove the stray < character:
<!-- ✅ Fixed: no extra "<" -->
<img src="photo.jpg" alt="smiling cat" />
Missing > on a preceding tag
When a tag is missing its closing >, the parser continues reading the next tag as though it were part of the first tag's attributes:
<!-- ❌ The opening <div> is missing its closing ">" -->
<div class="wrapper"
<p>Hello, world!</p>
</div>
The parser sees <p> while still inside the <div> tag, triggering the error. Add the missing >:
<!-- ✅ Fixed: <div> is properly closed -->
<div class="wrapper">
<p>Hello, world!</p>
</div>
Missing > on a tag with multiple attributes
This often happens with tags that have many attributes, making it easy to miss the closing >:
<!-- ❌ The <a> tag is missing its closing ">" -->
<a href="/about" class="nav-link" id="about-link"
<span>About Us</span>
</a>
<!-- ✅ Fixed: closing ">" added to the <a> tag -->
<a href="/about" class="nav-link" id="about-link">
<span>About Us</span>
</a>
Accidental angle bracket in an attribute value
If an attribute value contains a < that isn't properly quoted, the parser may misinterpret it:
<!-- ❌ Unquoted or broken attribute value with "<" -->
<div title=5<10>
<p>Content</p>
</div>
Ensure attribute values containing special characters are properly quoted and use HTML entities where needed:
<!-- ✅ Fixed: value is quoted and uses an entity for "<" -->
<div title="5<10">
<p>Content</p>
</div>
How to debug this error
When you see this error, follow these steps:
- Go to the line number reported by the validator.
- Look at the tag on that line — check if it has a stray
<character. - If the tag looks fine, check the tag immediately before it — a missing
>on the previous tag is often the real culprit. - Verify all attribute values are properly quoted — unquoted values containing
<can trigger this error. - Use a code editor with syntax highlighting — mismatched or broken tags are usually easy to spot when the syntax colors look wrong.
XML processing instructions are a feature of XML, not HTML. They begin with <? and end with ?>, and are used in XML documents to carry instructions for applications processing the document. The most common example is the XML declaration: <?xml version="1.0" encoding="UTF-8"?>. While these are perfectly valid in XML, the HTML parser does not recognize them. When the parser encounters <?, it doesn't know how to handle it and treats it as a bogus comment, which leads to unexpected behavior and this validation error.
This matters for several reasons. First, standards compliance — HTML5 has a clearly defined parsing algorithm, and processing instructions are not part of it. Second, browser behavior becomes unpredictable — different browsers may handle the unexpected <? content differently, potentially exposing raw code or breaking your layout. Third, if server-side code like PHP leaks into the output, it can expose sensitive logic or configuration details to end users.
There are three common causes of this error:
1. Inlining SVG files with their XML declaration. When you copy the contents of an .svg file and paste it directly into HTML, the file often starts with an XML declaration and possibly a <?xml-stylesheet?> processing instruction. These must be removed — only the <svg> element and its children should be included.
2. Unprocessed server-side code. Languages like PHP use <?php ... ?> (or the short tag <? ... ?>) syntax. If the server isn't configured to process PHP files, or if a .html file contains PHP code without being routed through the PHP interpreter, the raw <?php tags end up in the HTML sent to the browser.
3. Copy-pasting XML content. Other XML-based formats (like RSS feeds, XHTML fragments, or MathML with XML declarations) may include processing instructions that aren't valid in HTML.
How to Fix It
- For inline SVGs: Open the SVG file in a text editor and copy only the
<svg>...</svg>element. Remove the<?xml ...?>declaration and any other processing instructions that precede the<svg>tag. - For PHP or other server-side code: Ensure your server is properly configured to process the files. Check that the file extension matches what the server expects (e.g.,
.phpfor PHP files). Verify that the server-side language is installed and running. - For other XML content: Strip out any
<?...?>processing instructions before embedding the content in HTML.
Examples
❌ Inline SVG with XML declaration
<body>
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="blue" />
</svg>
</body>
The <?xml version="1.0" encoding="UTF-8"?> line triggers the error.
✅ Inline SVG without XML declaration
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="blue" />
</svg>
</body>
❌ Unprocessed PHP code in HTML output
<body>
<h1>Welcome</h1>
<p><?php echo "Hello, World!"; ?></p>
</body>
If the server doesn't process the PHP, the raw <?php ... ?> ends up in the HTML and triggers this error.
✅ Properly processed output (or static HTML equivalent)
<body>
<h1>Welcome</h1>
<p>Hello, World!</p>
</body>
❌ SVG with an XML stylesheet processing instruction
<body>
<?xml-stylesheet type="text/css" href="style.css"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
<rect width="50" height="50" fill="red" />
</svg>
</body>
✅ SVG without the processing instruction
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
<rect width="50" height="50" fill="red" />
</svg>
</body>
In HTML, use standard <link> or <style> elements in the <head> for stylesheets instead of XML processing instructions.
The < and > characters have special meaning in HTML — they signal the start and end of tags. When the parser encounters </>, it sees what looks like a closing tag with no element name, which is invalid in HTML. This sequence can appear in your markup for two main reasons:
- Unescaped text content: You're trying to display the literal characters
</>as visible text on the page (common in tutorials, documentation, or code snippets), but the browser interprets them as markup rather than content. - Mistyped end tag: You intended to write a proper closing tag like
</p>or</div>but accidentally omitted the element name, leaving just</>.
This matters because browsers may silently discard the malformed tag or interpret it in unexpected ways, leading to broken layouts or missing content. Screen readers and other assistive technologies may also struggle with the resulting DOM structure. Properly escaping special characters and writing well-formed tags ensures consistent rendering across all browsers and devices.
To fix this, determine which scenario applies. If you want to display the literal text </>, replace < with < and > with >. If you meant to close an element, add the correct element name between </ and >.
Examples
Unescaped angle brackets in text content
This triggers the error because the parser sees </> as an invalid closing tag:
<!-- ❌ Bad: raw </> in text content -->
<p>In JSX, self-closing tags use the </> syntax.</p>
Escape the angle brackets using HTML character entities:
<!-- ✅ Good: properly escaped characters -->
<p>In JSX, self-closing tags use the </> syntax.</p>
Mistyped closing tag with missing element name
This triggers the error because the closing tag has no name:
<!-- ❌ Bad: empty closing tag -->
<div class="container">
<p>Some content here.</p>
</>
Add the correct element name to the closing tag:
<!-- ✅ Good: proper closing tag -->
<div class="container">
<p>Some content here.</p>
</div>
Displaying code snippets with angle brackets
When writing about HTML or XML in your page content, all angle brackets in text must be escaped:
<!-- ❌ Bad: unescaped tags in text -->
<p>Use <strong> to make text bold and </strong> to close it.</p>
<!-- ✅ Good: escaped tags in text -->
<p>Use <strong> to make text bold and </strong> to close it.</p>
Using the <code> element for inline code
Even inside <code> elements, angle brackets must still be escaped — the <code> element only changes visual presentation, it does not prevent HTML parsing:
<!-- ❌ Bad: unescaped inside <code> -->
<p>A React fragment looks like <code><></code> and <code></></code>.</p>
<!-- ✅ Good: escaped inside <code> -->
<p>A React fragment looks like <code><></code> and <code></></code>.</p>
Using <pre> blocks for larger code examples
The same escaping rules apply within <pre> elements:
<!-- ✅ Good: escaped characters inside pre -->
<pre><code><div>
<p>Hello, world!</p>
</div></code></pre>
If you frequently need to display code and find manual escaping tedious, consider using a JavaScript-based syntax highlighting library that handles escaping automatically, or use a build tool or templating engine that escapes HTML entities for you.
An attribute value closed with two single quotes, or an apostrophe written as '' inside a single-quoted value, leaves a stray ' where the parser expects the next attribute name.
In a single-quoted value such as alt='Spring concert', the next ' after the opening one ends the value. After that the parser expects a space and another attribute, or the end of the tag. An extra quote right after the closing one, as in alt='Spring concert'', can't start an attribute name, so the validator reports it, usually next to "No space between attributes". Browsers treat the stray quote as the name of a new, unknown attribute, and the validator flags that attribute as not allowed on the element.
The doubled apostrophe does more damage. SQL string literals escape an apostrophe by writing it twice, and some code that builds HTML copies that convention, but HTML has no such escape. In alt='Children''s choir', the value ends after Children, and 's and choir' become separate junk attributes. Screen readers read the image's text alternative as just "Children".
The "probable cause" in the message is misleading for single quotes. A missing =, as in title'Contact', produces a different message about a quote in an attribute name. This one appears when the quote sits where a new attribute should start: right after a closing quote, or after a space that follows a complete attribute, as in class='note' 'intro'.
Delete the extra quote. When the value itself contains an apostrophe, wrap it in double quotes (alt="Children's choir") or write the apostrophe as ' or '. If a CMS or template prints the attribute, fix the escaping there so every page gets the correct output.
Invalid example
<img src="concert.jpg" alt='Spring concert'' width="300" height="200">
<img src="choir.jpg" alt='Children''s choir' width="300" height="200">
Valid example
<img src="concert.jpg" alt="Spring concert" width="300" height="200">
<img src="choir.jpg" alt="Children's choir" width="300" height="200">
U+0000, commonly called the "null character" or NUL, is a non-printable control character. It's invisible in most text editors and browsers, so you won't see it just by looking at your code. However, the HTML parsing algorithm defined by the WHATWG HTML Living Standard treats U+0000 as a parse error in virtually every context — whether it appears in text content, attribute values, comments, or anywhere else in the document.
When a browser encounters a null character, it typically either ignores it or replaces it with the Unicode replacement character (U+FFFD, �). This means the character serves no useful purpose and can only cause problems.
Why Does This Happen?
Null characters usually sneak into HTML files through one of these common scenarios:
- Copy and paste from applications like word processors, PDFs, or terminal output that embed hidden control characters.
- File encoding corruption, such as when a file is converted between encodings incorrectly or transferred in binary mode.
- Editor or build tool bugs that inadvertently insert null bytes into output files.
- Database content that contains null characters which then gets rendered into HTML templates.
- Concatenation of binary data with text during a build process.
Why It Matters
- Standards compliance: The HTML specification explicitly forbids U+0000. Its presence makes your document non-conforming.
- Unpredictable rendering: Different browsers may handle null characters differently — some ignore them, others replace them with
�, which can produce visible artifacts in your content. - Accessibility: Screen readers and assistive technologies may behave unexpectedly when encountering null characters, potentially skipping content or producing garbled output.
- Data integrity: The presence of null characters is often a symptom of a deeper problem, such as encoding corruption, that could affect other parts of your content.
How to Fix It
Reveal hidden characters in your text editor. Most code editors have this option:
- In VS Code, use the setting
"editor.renderControlCharacters": true. - In Sublime Text, look for non-printable characters via Find & Replace with regex enabled.
- In Vim, null characters typically display as
^@.
- In VS Code, use the setting
Search and remove null characters. Use your editor's find-and-replace with regex support to search for
\x00or\0and replace with nothing (an empty string).Use command-line tools for a quick fix:
- On Linux/macOS:
tr -d '\0' < input.html > output.html - With
sed:sed -i 's/\x0//g' file.html
- On Linux/macOS:
Check your build pipeline. If the null characters reappear after cleaning, the issue likely originates upstream — in a database, a template engine, or a build step that concatenates files.
Re-validate your HTML after cleaning to confirm the issue is resolved.
Examples
Incorrect — null character in text content
In the example below, \0 represents the invisible U+0000 character for illustration purposes:
<p>Welcome to our site!\0 We're glad you're here.</p>
This triggers the "Saw U+0000 in stream" error.
Correct — null character removed
<p>Welcome to our site! We're glad you're here.</p>
Incorrect — null character inside an attribute value
<a href="/about\0us">About Us</a>
Correct — null character removed from attribute
<a href="/about-us">About Us</a>
Incorrect — null character in a comment
<!-- This comment has a null \0 character -->
Correct — clean comment
<!-- This comment has no null character -->
Null characters are forbidden in all contexts, so no matter where they appear — in elements, attributes, comments, or even the DOCTYPE — they must be removed. If you consistently see this error after saving files, investigate your editor's encoding settings and ensure files are saved as UTF-8 without any binary artifacts.
The HTML parser reads element tags by looking for attribute names, an = sign, and then a quoted value. When the parser finds a " character in a position where it expects an attribute name to start, it means something has gone wrong with the syntax. The validator flags this as a parse error because the browser has to guess what you intended, which can lead to attributes being silently dropped, values being misassigned, or unexpected rendering behavior.
This error most commonly occurs due to one of these causes:
- Missing
=sign between an attribute name and its value (e.g.,class"main"instead ofclass="main"). - Extra closing quote that creates a stray
"after a valid attribute (e.g.,class="main""with a doubled quote). - Stray quote from copy-paste errors or typos that leave orphan
"characters floating in the tag. - Accidentally deleted
=during editing, breaking an otherwise valid attribute.
Beyond being invalid HTML, this problem can cause real functional issues. A missing = sign may cause the browser to interpret the attribute value as a separate (unknown) attribute, effectively losing the intended value. An extra quote can cause the parser to misread subsequent attributes, potentially breaking event handlers, links, or styling. These issues can also harm accessibility, as assistive technologies rely on properly parsed attributes to convey meaning.
Examples
Missing equals sign
The = between the attribute name and value is absent, so the parser sees "container" where it expects a new attribute name.
<!-- ❌ Bad: missing = before the value -->
<div class"container">Content</div>
<!-- ✅ Fixed: = added between name and value -->
<div class="container">Content</div>
Extra closing quotation mark
A doubled " at the end of an attribute value leaves a stray quote that the parser doesn't expect.
<!-- ❌ Bad: extra " after the attribute value -->
<a href="https://example.com"">Visit</a>
<!-- ✅ Fixed: single closing quote -->
<a href="https://example.com">Visit</a>
Multiple attributes with a stray quote
When multiple attributes are present, a stray quote can also corrupt the parsing of subsequent attributes.
<!-- ❌ Bad: extra " after id value bleeds into the next attribute -->
<input id="email"" type="text" name="email">
<!-- ✅ Fixed: clean quotes on every attribute -->
<input id="email" type="text" name="email">
Missing equals sign on a later attribute
The error doesn't always occur on the first attribute — it can appear on any attribute in the tag.
<!-- ❌ Bad: missing = on the style attribute -->
<p class="intro" style"color: red;">Hello</p>
<!-- ✅ Fixed: = added before style value -->
<p class="intro" style="color: red;">Hello</p>
How to fix it
- Locate the line reported by the validator and look at the element's attributes.
- Check every attribute follows the
name="value"pattern with no missing=signs. - Count your quotes — every opening
"should have exactly one matching closing"for each attribute value. - Watch for smart quotes — curly quotes (
"") copied from word processors are not valid HTML attribute delimiters. Replace them with straight quotes ("). - Use a code editor with syntax highlighting — mismatched or extra quotes are usually easy to spot when your editor color-codes attribute values.
Nested comments are not allowed in HTML. When you place an opening comment tag <!-- within an existing comment, it causes a parsing error.
To fix this issue, you should avoid nesting comments. If you need to include multiple comments in your HTML code, you can separate them or consider alternative ways to convey the information without nested comments.
Here's an example of a nested comments, which is not allowed in HTML:
<!-- This is a valid comment -->
<!-- This <!-- is not allowed --> nested comment -->
To fix the nested comment issue, you can rewrite the comments like this:
<!-- This is a valid comment -->
<!-- Avoid nesting comments and provide comments separately -->
<!-- Another comment here -->
A <script> element sits between the closing </head> tag and the opening <body> tag, where the HTML parser doesn't expect any elements.
After </head>, the parser expects the <body> start tag, with nothing but whitespace or comments before it. When it finds a <script> there, browsers move the element back into the <head> and run it from there, so the page usually works. The markup is still invalid. An analytics or tag manager snippet pasted right after </head> is a common cause, and so is a theme or plugin that prints its scripts there.
The validator treats this error as fatal. Right after it, the report shows "Cannot recover after last error. Any further errors will be ignored." and nothing later in the page gets checked. Other HTML problems below this point stay hidden until you fix it.
To fix it, move the <script> element inside the <head>, before </head>. Browsers already run it from there, so the page behaves the same after the change. A script can also go anywhere inside the <body>, for example right before </body>.
Invalid example
<!DOCTYPE html>
<html lang="en">
<head>
<title>My page</title>
</head>
<script src="analytics.js"></script>
<body>
<p>Hello, world!</p>
</body>
</html>
Valid example
<!DOCTYPE html>
<html lang="en">
<head>
<title>My page</title>
<script src="analytics.js"></script>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
The <section> element defines a standalone thematic grouping within a document — think chapters, tabbed panels, or distinct parts of a page like "About Us" or "Contact." According to the HTML specification, each <section> should generally include a heading that identifies its content. When a <section> lacks a heading, the validator raises this warning because the element isn't being used as intended.
This matters for several reasons:
Accessibility. Screen readers and other assistive technologies use headings to build an outline of the page. When a user navigates by sections, the heading is what identifies each one. A <section> without a heading appears as an "untitled section" in the document outline, which makes it difficult or impossible for assistive technology users to understand the page structure or jump to the content they need.
Semantics. The whole purpose of <section> is to group related content under a common theme, and that theme should be labeled with a heading. If your content doesn't need a heading, it may not be a true "section" in the semantic sense, and a <div> (which carries no semantic meaning) might be a better fit.
Document outline. Browsers and tools that generate document outlines rely on headings within sectioning elements. Missing headings create gaps in the outline, reducing the usefulness of the page structure.
How to fix it
You have three main options:
Add a heading — Place an
<h2>–<h6>element at the beginning of the<section>. This is the preferred solution when the section genuinely represents a thematic block of content.Use
aria-labeloraria-labelledby— If a visible heading doesn't suit your design, you can label the section for assistive technologies using an ARIA attribute. This resolves the accessibility concern, though the validator may still show the warning.Switch to
<div>— If the content doesn't represent a meaningful, identifiable section of the document, replace<section>with a<div>. There's no expectation for a<div>to have a heading.
Examples
❌ Section without a heading
This triggers the warning because neither <section> has a heading:
<h1>All about guitars</h1>
<section>
<p>Acoustic, electric, classical... we have them all!</p>
</section>
<section>
<p>Analog, digital, portable...</p>
</section>
✅ Fixed: Adding headings to each section
<h1>All about guitars</h1>
<section>
<h2>Guitar types</h2>
<p>Acoustic, electric, classical... we have them all!</p>
</section>
<section>
<h2>Amplifiers</h2>
<p>Analog, digital, portable...</p>
</section>
✅ Fixed: Using aria-label when a visible heading isn't appropriate
<section aria-label="Guitar types">
<p>Acoustic, electric, classical... we have them all!</p>
</section>
✅ Fixed: Using aria-labelledby to reference an existing element
<section aria-labelledby="guitar-heading">
<span id="guitar-heading" class="visually-hidden">Guitar types</span>
<p>Acoustic, electric, classical... we have them all!</p>
</section>
✅ Fixed: Replacing with <div> when no section semantics are needed
If you're only using the element as a styling wrapper and the content doesn't form a distinct thematic group, use a <div> instead:
<h1>All about guitars</h1>
<div class="content-block">
<p>Acoustic, electric, classical... we have them all!</p>
</div>
<div class="content-block">
<p>Analog, digital, portable...</p>
</div>
As a general rule, reach for <section> when your content has a clear topic that deserves a heading, and use <div> when you need a generic container for layout or styling purposes.
In HTML, elements fall into two categories regarding their content model: void elements and non-void elements. Void elements like <img>, <br>, <hr>, <input>, <meta>, and <link> cannot have any content and never need a closing tag. Non-void elements like <div>, <p>, <span>, <select>, <textarea>, and <script> can (or must) contain content and require both an opening tag and a closing tag.
When you write <div /> or <select />, you might expect the browser to treat it as a complete, self-contained element — similar to how XML or XHTML would interpret it. However, the HTML parser does not work this way. According to the WHATWG HTML Living Standard, the /> on a non-void element is simply ignored. The browser treats <div /> as just <div> — an opening tag without a corresponding closing tag. This can lead to serious structural problems in your document, as the browser will keep looking for content and a closing tag, potentially swallowing subsequent elements as children.
This issue commonly arises when developers are accustomed to frameworks like React (JSX), where <Component /> syntax is standard, or when coming from an XML/XHTML background where any element can be self-closed. It also appears when developers mistakenly believe that an element with no intended content can be self-closed in HTML.
The consequences can be significant. The browser's DOM tree may end up drastically different from what you intended, causing layout problems, broken functionality, and accessibility issues. For example, a self-closed <script /> tag won't behave as expected — the browser will treat everything after it as the script's content until it finds a </script> closing tag somewhere else in the document, effectively hiding your visible page content.
How to fix it
- Remove the trailing slash from the non-void element's opening tag.
- Add a proper closing tag immediately after the opening tag if the element should be empty, or after its content.
The complete list of void elements in HTML is: <area>, <base>, <br>, <col>, <embed>, <hr>, <img>, <input>, <link>, <meta>, <source>, <track>, and <wbr>. Only these elements may use self-closing syntax (and even for these, the / is optional in HTML).
Examples
Incorrect: Self-closing syntax on non-void elements
<div />
<select />
<option value="1">one</option>
</select>
<span />
<textarea />
<script src="app.js" />
Each of these will trigger the warning. The validator ignores the slash and treats them as opening tags, meaning the browser expects content and a closing tag to follow.
Correct: Using proper closing tags
<div></div>
<select>
<option value="1">one</option>
</select>
<span></span>
<textarea></textarea>
<script src="app.js"></script>
Correct: Self-closing syntax on void elements
Void elements can optionally use the trailing slash. Both forms below are valid:
<!-- With optional trailing slash -->
<img src="photo.jpg" alt="A photo" />
<br />
<input type="text" name="username" />
<!-- Without trailing slash (equally valid) -->
<img src="photo.jpg" alt="A photo">
<br>
<input type="text" name="username">
A subtle trap: <script />
This example demonstrates one of the most dangerous cases of this issue:
<!-- Incorrect: browser treats everything after this as script content -->
<script src="app.js" />
<p>This paragraph will not be rendered — it's consumed as script text.</p>
<!-- Correct -->
<script src="app.js"></script>
<p>This paragraph renders normally.</p>
The browser interprets the incorrect version as an opening <script> tag and reads all subsequent markup as script content until it encounters </script>, effectively hiding your page content and likely producing JavaScript errors.
The HTML specification defines <a> elements as having a transparent content model, but with specific exceptions — most notably, they must not contain interactive content, which includes other <a> elements. This means you can never legally nest one link inside another, regardless of how deeply nested or how the markup is structured.
This error most commonly occurs for one of two reasons:
- A missing closing tag: You forgot to close a previous
<a>element with</a>, so the browser (and the validator) considers the next<a>tag to be nested inside the still-open one. - Intentionally nested links: You tried to place a link inside another link, perhaps to create a card component where both the card and an inner element are clickable.
Why this matters
- Unpredictable browser behavior: When browsers encounter nested
<a>elements, they attempt to fix the invalid markup using error-recovery algorithms, but different browsers may handle it differently. This can lead to broken links, missing content, or unexpected DOM structures. - Accessibility issues: Screen readers and other assistive technologies rely on a well-formed DOM. Nested links create confusing navigation — a user tabbing through links may encounter unexpected behavior or miss content entirely.
- Standards compliance: The WHATWG HTML specification explicitly states that
<a>elements must not have interactive content descendants, including other<a>elements.
Examples
Missing closing tag (most common cause)
The most frequent trigger is simply forgetting to close an <a> tag. The validator sees the second <a> as being inside the first:
<!-- ❌ Bad: first <a> is never closed -->
<nav>
<a href="one.html">Page 1
<a href="two.html">Page 2</a>
</nav>
Add the missing </a> to fix it:
<!-- ✅ Good: both <a> elements are properly closed -->
<nav>
<a href="one.html">Page 1</a>
<a href="two.html">Page 2</a>
</nav>
Intentionally nested links
Sometimes developers try to nest links when building clickable card components:
<!-- ❌ Bad: <a> nested inside another <a> -->
<a href="/article" class="card">
<h2>Article Title</h2>
<p>A short description of the article.</p>
<a href="/author">Author Name</a>
</a>
There are several valid approaches to fix this. One common pattern is to make the card a non-link container and use CSS to stretch the primary link over the entire card:
<!-- ✅ Good: no nested links, card is still fully clickable -->
<div class="card">
<h2><a href="/article" class="card-link">Article Title</a></h2>
<p>A short description of the article.</p>
<a href="/author">Author Name</a>
</div>
.card {
position: relative;
}
.card-link::after {
content: "";
position: absolute;
inset: 0;
}
.card a:not(.card-link) {
position: relative;
z-index: 1;
}
This technique uses a pseudo-element to expand the primary link's clickable area over the entire card, while the secondary link (Author Name) sits above it via z-index, remaining independently clickable.
Links wrapping block-level content
In HTML5, <a> elements can wrap block-level elements like <div> and <h2>, which is perfectly valid. Just make sure you don't accidentally nest another <a> inside:
<!-- ✅ Good: <a> wrapping block content with no nested links -->
<a href="/article">
<div class="card">
<h2>Article Title</h2>
<p>A short description of the article.</p>
</div>
</a>
Quick checklist
- Search your HTML for every
<atag and verify it has a corresponding</a>. - If you're generating HTML dynamically (with a CMS, templating engine, or JavaScript), check that your template logic doesn't produce unclosed or nested anchors.
- If you need multiple clickable areas within a single component, use the CSS pseudo-element overlay technique shown above instead of nesting links.
The HTML <table> element follows a rigid structure. A <table> can only contain <caption>, <colgroup>, <thead>, <tbody>, <tfoot>, and <tr> as direct children. A <tr> can only contain <td> and <th> elements. Placing an <a> tag (or any other inline/flow content) directly inside <table>, <tr>, or other table-structural elements violates this content model, triggering the "Start tag a seen in table" validation error.
This matters for several reasons. Browsers handle invalid table markup inconsistently — some may silently move the misplaced <a> element outside the table entirely, while others may render it in unexpected positions. This leads to unpredictable layouts and broken links. Screen readers and other assistive technologies rely on proper table structure to navigate content, so misplaced elements can make the page confusing or inaccessible. Additionally, search engine crawlers may not correctly associate the link with its intended context.
The fix is straightforward: always wrap inline content like <a> inside a <td> or <th> element. Every piece of visible content in a table must live inside a table cell.
Examples
❌ Incorrect: <a> directly inside <tr>
<table>
<tr>
<a href="/details">View details</a>
</tr>
</table>
The <a> is a direct child of <tr>, which only allows <td> and <th> children.
❌ Incorrect: <a> directly inside <table>
<table>
<a href="/details">View details</a>
<tr>
<td>Data</td>
</tr>
</table>
The <a> is a direct child of <table>, which does not allow inline content.
❌ Incorrect: <a> directly inside <tbody>
<table>
<tbody>
<a href="/details">View details</a>
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
The <a> is placed directly inside <tbody>, which only allows <tr> and <script>/<template> elements.
✅ Correct: <a> inside a <td>
<table>
<tr>
<td>
<a href="/details">View details</a>
</td>
</tr>
</table>
✅ Correct: <a> inside a <th>
<table>
<thead>
<tr>
<th>
<a href="/details">View details</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
✅ Correct: wrapping an entire row's content in a link
If you want to make an entire row clickable, place the <a> inside each <td> rather than wrapping the <tr>:
<table>
<tr>
<td><a href="/item/1">Product name</a></td>
<td><a href="/item/1">$19.99</a></td>
<td><a href="/item/1">In stock</a></td>
</tr>
</table>
This keeps the table structure valid while still providing clickable content across the row. You can use CSS to remove the visual link styling and make each <a> fill its entire cell for a seamless clickable-row effect.
An HTML document must have exactly one <body> element. According to the HTML specification, the <body> element represents the content of the document and must appear as the second child of the <html> element, right after <head>. When the browser's parser encounters content that belongs in the body before seeing an explicit <body> tag, it implicitly opens one. If a <body> tag then appears later in the markup, the parser sees it as a duplicate—hence the error "an element of the same type was already open."
There are several common causes for this error:
- Duplicate
<body>tags: A second<body>tag was accidentally typed or left behind after editing or merging templates. - Content between
</head>and<body>: Placing visible content (like text,<div>, or<script>with content) between the closing</head>tag and the opening<body>tag causes the parser to implicitly open the body, making your explicit<body>tag a duplicate. - Template or include issues: When using server-side includes, CMS templates, or JavaScript-based frameworks that inject partial HTML, multiple
<body>tags can end up in the final rendered output. - Misplaced elements inside
<head>: Placing elements that aren't valid inside<head>(such as<div>,<p>, or<img>) will cause the parser to implicitly close the<head>and open the<body>before you intended.
This matters because a malformed document structure can lead to unpredictable rendering across browsers. While modern browsers are forgiving and will attempt to recover, the implicit body opening may cause elements you intended to be in the <head> (like <meta> or <link> tags placed after the stray content) to end up in the body instead, where they have no effect. This can break stylesheets, SEO metadata, and other head-level functionality. Screen readers and other assistive technologies also rely on correct document structure.
To fix the issue, inspect your full HTML output (not just your templates individually) and make sure there is only one <body> tag. Verify that no visible content or body-level elements appear before it, and that nothing invalid is lurking inside <head>.
Examples
Duplicate <body> tag
This triggers the error because a second <body> tag appears while the first body is still open, as happens when an included template starts with its own <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome</h1>
<body>
<p>More content</p>
</body>
</html>
If the second <body> comes after </body> instead, the validator also reports "Stray start tag “body”", because no body can start once the first one is closed.
Fixed — combine everything into a single <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome</h1>
<p>More content</p>
</body>
</html>
Content placed between </head> and <body>
The stray <div> between </head> and <body> implicitly opens the body, making the explicit <body> tag a duplicate:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<div id="banner">Hello</div>
<body>
<p>Main content</p>
</body>
</html>
Fixed — move the <div> inside <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<div id="banner">Hello</div>
<p>Main content</p>
</body>
</html>
Invalid element inside <head>
Placing a <div> inside <head> forces the parser to implicitly open <body>, so the actual <body> tag becomes a duplicate. This also means the <link> tag ends up in the body where it may not work as intended:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<div>Oops</div>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p>Content</p>
</body>
</html>
Fixed — remove the <div> from <head> and place it in <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>Oops</div>
<p>Content</p>
</body>
</html>
The <table> element in HTML has a rigid content model. Directly inside <table>, only specific elements are allowed: <caption>, <colgroup>, <thead>, <tbody>, <tfoot>, <tr>, and <script>-supporting elements. Similarly, <tr> elements may only contain <td> and <th> elements. A <br> tag placed between table rows, or directly inside a <tbody> or <tr> but outside a cell, violates this content model.
This error typically happens when developers try to add vertical spacing between table rows using <br> tags. Browsers handle this invalid markup inconsistently — some will push the <br> outside the table entirely, while others may silently ignore it. This leads to unpredictable layout behavior across browsers and can confuse assistive technologies that rely on proper table structure to convey data relationships to users.
The <br> element is only valid inside phrasing content contexts, such as within a <td>, <th>, <p>, <span>, or similar elements. If you need to add spacing between rows, use CSS (margin, padding, or border-spacing) instead of inserting <br> tags into the table structure.
How to fix it
- Remove the
<br>if it was added accidentally or as a formatting attempt between rows. - Move the
<br>inside a<td>or<th>if you intended it to create a line break within a cell's content. - Use CSS for spacing if you need visual separation between rows. Apply
paddingto cells or use theborder-spacingproperty on the table.
Examples
❌ Invalid: <br> between table rows
<table>
<tr>
<th>Item</th>
<th>Description</th>
</tr>
<br>
<tr>
<td>Book</td>
<td>A guide to HTML</td>
</tr>
</table>
❌ Invalid: <br> directly inside a <tr>
<table>
<tr>
<br>
<td>Book</td>
<td>A guide to HTML</td>
</tr>
</table>
✅ Fixed: <br> removed, CSS used for spacing
<table style="border-spacing: 0 1em;">
<tr>
<th>Item</th>
<th>Description</th>
</tr>
<tr>
<td>Book</td>
<td>A guide to HTML</td>
</tr>
</table>
✅ Valid: <br> used inside a table cell
A <br> element is perfectly valid inside a <td> or <th>, where it functions as a line break within the cell's content.
<table>
<tr>
<th>Item</th>
<th>Description</th>
</tr>
<tr>
<td>Book</td>
<td>
Title: HTML & CSS<br>
Author: Jon Duckett
</td>
</tr>
</table>
✅ Fixed: Using padding for row spacing
If your goal is to create visual separation between rows, CSS padding on cells is the cleanest approach:
<style>
.spaced-table td,
.spaced-table th {
padding: 1em 0.5em;
}
</style>
<table class="spaced-table">
<tr>
<th>Item</th>
<th>Description</th>
</tr>
<tr>
<td>Book</td>
<td>A guide to HTML</td>
</tr>
</table>
The HTML specification explicitly forbids nesting interactive content inside a <button> element. This means a <button> cannot contain another <button>, nor can it contain elements like <a>, <input>, <select>, <textarea>, or any other interactive element. The <button> element's content model allows phrasing content, but specifically excludes interactive content.
The most common cause of this error is a missing </button> closing tag. When you forget to close a button, the parser considers everything that follows — including the next <button> start tag — as being inside the first button. This creates an invalid nesting situation even though you likely intended the buttons to be siblings.
This matters for several reasons:
- Accessibility: Screen readers and assistive technologies rely on proper document structure. A button nested inside another button creates a confusing interaction model — which button does the user activate when they click or press Enter?
- Browser behavior: Browsers handle this invalid markup inconsistently. Some may ignore the inner button, while others may attempt error recovery in unpredictable ways, leading to broken functionality.
- Standards compliance: Invalid HTML can interfere with JavaScript event handling, CSS styling, and overall page reliability.
Examples
Missing closing tag (triggers the error)
<button>Submit
<button>Cancel</button>
Here, the first <button> is never closed, so the parser sees the second <button> as being nested inside it.
Intentionally nested buttons (triggers the error)
<button class="card">
<span>Select this plan</span>
<button class="details">More info</button>
</button>
Even if this nesting is intentional, it is invalid HTML. A <button> cannot contain another <button>.
Fix: Close each button properly
<button>Submit</button>
<button>Cancel</button>
Adding the missing </button> tag makes the two buttons proper siblings.
Fix: Restructure nested interactive elements
If you need a clickable card with an additional action inside it, separate the interactive elements rather than nesting them:
<div class="card">
<button class="select">Select this plan</button>
<button class="details">More info</button>
</div>
Fix: Use non-interactive elements for styling purposes
If the inner element was only meant for visual grouping, use a <span> or other phrasing element instead of a <button>:
<button class="card">
<span class="label">Select this plan</span>
<span class="sublabel">Best value</span>
</button>
This keeps a single interactive <button> while using non-interactive <span> elements for internal structure and styling.
<cation> is a misspelling of <caption>: rename the opening and closing tags to <caption> so the table gets its title.
HTML has no <cation> element, so the parser handles it like any unknown tag. A <table> only accepts <caption>, <colgroup>, <thead>, <tbody>, <tfoot> and <tr> as children (plus <script> and <template>), and an unknown element isn't one of them.
When the browser finds <cation> inside a table, it moves the element and its text out and puts them just before the table. The HTML specification calls this foster parenting. The text still shows up above the table, which makes the typo easy to miss on screen, but the table itself has no caption.
Screen readers announce a table's <caption> as its name. Without one, someone jumping from table to table hears the table with no title, and the stray text before it reads as an ordinary line. A caption hidden with a "visually hidden" class, written only for screen reader users, loses its whole purpose.
The validator stops checking the page at this error and adds Cannot recover after last error. Any further errors will be ignored. right after it. Nothing below that point gets checked, so new issues further down the page can show up once you fix this one.
To fix it, correct the spelling in both tags. Keep the <caption> as the first child of the <table>, since the HTML specification doesn't allow it anywhere else.
Invalid example
<table>
<cation>Monthly sales</cation>
<tr>
<th>Month</th>
<th>Total</th>
</tr>
<tr>
<td>January</td>
<td>1,200</td>
</tr>
</table>
Valid example
<table>
<caption>Monthly sales</caption>
<tr>
<th>Month</th>
<th>Total</th>
</tr>
<tr>
<td>January</td>
<td>1,200</td>
</tr>
</table>
A table can only hold another table inside one of its cells, so a <table> start tag placed directly inside an open table, outside any <td> or <th>, is an error.
Between <table> and </table>, HTML allows only table parts: <caption>, <colgroup>, <thead>, <tbody>, <tfoot> and <tr>. Content, a nested table included, goes in a <td> or <th> cell. When the parser meets a new <table> tag anywhere else in an open table, it closes the open table at that point and starts the new one after it. The two tables end up as siblings in the DOM, and the </table> written for the outer table has nothing left to close, so the validator also reports a stray end tag.
Two mistakes usually cause this. One is a missing </table>: a table never gets closed, and the next table on the page starts inside it. The other is a wrapper table with another table directly inside it and no <tr> or <td> in between, such as one left behind by a WYSIWYG editor.
Close the first table before the next one starts, or remove the extra wrapper. If you do want one table inside another, put the inner table in a <td> of the outer one. To center a table or add space around it, use CSS on the table itself instead of wrapping it in another table.
Invalid example
<table>
<table>
<tr>
<th>Day</th>
<th>Opening hours</th>
</tr>
<tr>
<td>Monday</td>
<td>9:00 to 17:00</td>
</tr>
</table>
</table>
Valid example
<table>
<tr>
<th>Day</th>
<th>Opening hours</th>
</tr>
<tr>
<td>Monday</td>
<td>9:00 to 17:00</td>
</tr>
</table>
A <form> can't sit directly inside a <table>, <thead>, <tbody>, <tfoot> or <tr>: wrap the whole table in the form, or put the form inside a single <td> or <th> cell.
Table elements only accept specific children. A <table> holds <caption>, <colgroup>, <thead>, <tbody>, <tfoot> and <tr>, and a <tr> holds <td> and <th>. A <form> wrapped around one or more rows breaks that structure.
The browser's parser handles this case in a special way. It creates the form as an empty element at the spot where the tag appears, closes it right away, and drops the matching </form> end tag, which the validator reports as a separate Stray end tag “form” error. Browsers still link the fields in the following rows to that empty form, so submitting often appears to work. But the fields are not inside the form in the DOM: a selector like form input won't find them, and if a script moves or re-renders those rows, the fields lose their form.
When each row needs its own form, such as an "Update" button per cart item, keep the <form> elements outside the table and link each field and button to its form with the form attribute, which takes the form's id.
Invalid example
<table>
<form action="/cart" method="post">
<tr>
<td><label for="qty">Quantity</label></td>
<td><input type="number" id="qty" name="qty"></td>
<td><button type="submit">Update</button></td>
</tr>
</form>
</table>
Valid example
<form action="/cart" method="post">
<table>
<tr>
<td><label for="qty">Quantity</label></td>
<td><input type="number" id="qty" name="qty"></td>
<td><button type="submit">Update</button></td>
</tr>
</table>
</form>
Valid example with one form per row
<form id="item-1" action="/cart/1" method="post"></form>
<form id="item-2" action="/cart/2" method="post"></form>
<table>
<tr>
<td><input type="number" name="qty" form="item-1" aria-label="Quantity for item 1"></td>
<td><button type="submit" form="item-1">Update</button></td>
</tr>
<tr>
<td><input type="number" name="qty" form="item-2" aria-label="Quantity for item 2"></td>
<td><button type="submit" form="item-2">Update</button></td>
</tr>
</table>
An HTML document must contain exactly one <head> element, and it must be the first child of the <html> element. The <head> element holds metadata like the document title, character encoding, stylesheets, and scripts. When the validator encounters a second <head> start tag, it means the document structure is broken — the browser has to guess how to interpret the malformed markup, which can lead to unpredictable behavior.
This error matters for several reasons. First, metadata inside a malformed <head> may not be processed correctly, which can affect SEO, character encoding, and stylesheet loading. Second, browsers may silently "fix" the issue in different ways, leading to inconsistent rendering. Third, malformed document structure can confuse assistive technologies like screen readers.
The most common causes of this error are:
- A missing forward slash in the closing tag — writing
<head>instead of</head>. This is an easy typo to miss, especially in larger documents. - Accidentally nesting a second
<head>inside the first — this can happen during copy-paste operations or when merging code from multiple sources. - Templating errors — when using a template engine, a partial or include might inject its own
<head>tag into a document that already has one.
To fix the issue, search your HTML for every occurrence of <head and confirm that only one opening <head> tag exists and that the <head> section ends with a properly written </head> tag.
Examples
Missing forward slash on closing tag
This is the most common cause. The closing tag is written as <head> instead of </head>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<head>
<body>
<p>Hello world</p>
</body>
</html>
Fixed — add the forward slash to properly close the <head>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<p>Hello world</p>
</body>
</html>
Nested <head> elements
A second <head> block is incorrectly nested inside the first:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<head>
<meta name="description" content="A test page">
</head>
</head>
<body>
<p>Hello world</p>
</body>
</html>
Fixed — remove the inner <head> and </head> tags, keeping the metadata in the single <head> element:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<meta name="description" content="A test page">
</head>
<body>
<p>Hello world</p>
</body>
</html>
Duplicate <head> from a template include
When working with template engines, an included partial might bring its own <head>, resulting in a duplicate:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<head>
<link rel="stylesheet" href="extra.css">
</head>
<body>
<p>Hello world</p>
</body>
</html>
Fixed — merge all metadata into a single <head> element:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
<link rel="stylesheet" href="extra.css">
</head>
<body>
<p>Hello world</p>
</body>
</html>
An <input> can't sit directly inside a <table>, <thead>, <tbody>, <tfoot> or <tr>: put it inside a <td> or <th> cell, or move it outside the table.
Table elements only accept specific children. A <table> holds <caption>, <colgroup>, <thead>, <tbody>, <tfoot> and <tr>, and a <tr> holds <td> and <th>.
When the browser's parser finds a misplaced <input>, it moves the element out and inserts it just before the table. The HTML specification calls this foster parenting. The field then shows up above the table instead of in the row where you wrote it. Hidden inputs (type="hidden") are the exception: the parser leaves them in place, but the validator flags them too.
This usually comes from forms laid out with tables, where hidden fields such as CSRF tokens or page numbers end up between <tr> elements. Move hidden inputs before or after the <table>, keeping them inside the <form>, and put visible inputs in a cell. If the <form> tag itself sits inside the table, move it out so it wraps the whole <table>. A <form> between rows triggers its own validation error.
Invalid example
<form action="/search">
<table>
<tr>
<td><label for="q">Search</label></td>
<td><input type="text" id="q" name="q"></td>
</tr>
<input type="hidden" name="page" value="1">
</table>
</form>
Valid example
<form action="/search">
<input type="hidden" name="page" value="1">
<table>
<tr>
<td><label for="q">Search</label></td>
<td><input type="text" id="q" name="q"></td>
</tr>
</table>
</form>
A <p> can't sit directly inside a <table>, <thead>, <tbody>, <tfoot> or <tr>: put the paragraph inside a <td> or <th> cell, or move it outside the table.
Table elements only accept specific children. A <table> holds <caption>, <colgroup>, <thead>, <tbody>, <tfoot> and <tr>, and a <tr> holds <td> and <th>. A paragraph is allowed inside a cell or a <caption>, but not between rows.
When the browser's parser finds a misplaced <p>, it moves the paragraph out and inserts it just before the table. The HTML specification calls this foster parenting. A paragraph you wrote as a table title or a note between rows shows up above the table instead, and an empty <p></p> left between rows becomes an empty paragraph above it.
The validator stops checking the page at this error and adds Cannot recover after last error. Any further errors will be ignored. right after it. Nothing below that point gets checked, so new issues further down the page can show up once you fix this one.
To fix it, put a table title in a <caption>, move text that belongs to a row into a <td> or <th>, and delete empty <p></p> pairs between rows. If those empty paragraphs were there to add space between rows, use CSS padding on the cells or border-spacing on the table instead.
Invalid example
<table>
<p>Opening hours</p>
<tr>
<td>Monday</td>
<td>9:00 to 17:00</td>
</tr>
<p></p>
<tr>
<td>Saturday</td>
<td>Closed</td>
</tr>
</table>
Valid example
<table>
<caption>Opening hours</caption>
<tr>
<td>Monday</td>
<td>9:00 to 17:00</td>
</tr>
<tr>
<td>Saturday</td>
<td>Closed</td>
</tr>
</table>
The Document Type Declaration (commonly called the "doctype") tells browsers which version and rules of HTML the document follows. In modern HTML5, the doctype is simply <!DOCTYPE html>. It must be the very first thing in the document — before the <html> tag, before any whitespace-generating content, and before any other elements. The only thing permitted before it is an optional BOM (byte order mark) or whitespace characters.
Without a doctype, browsers fall back to quirks mode, a legacy rendering mode that emulates old, inconsistent browser behavior. In quirks mode, the CSS box model, table sizing, font inheritance, and many other layout behaviors work differently than in standards mode. This can cause your page to render inconsistently across browsers and lead to hard-to-debug visual issues. Including the doctype ensures the browser uses standards mode, giving you predictable, spec-compliant rendering.
This error commonly occurs for a few reasons:
- The doctype was simply forgotten or accidentally deleted.
- There is content before the doctype, such as an XML declaration (
<?xml version="1.0"?>), a comment, or a server-side include. - The doctype is misspelled or uses an older, incorrect format.
- The file contains a BOM or invisible characters before the doctype that shift it out of position (though the validator may report this differently).
The doctype declaration is case-insensitive — <!DOCTYPE html>, <!doctype html>, and <!Doctype Html> are all valid — but the lowercase-uppercase convention <!DOCTYPE html> is the most widely used.
Examples
❌ Missing doctype entirely
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
The validator sees the <html> start tag but no doctype has appeared yet, triggering the error.
❌ Content before the doctype
<!-- This is my website -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
Even an HTML comment placed before the doctype can trigger this warning, because the parser encounters non-doctype content first.
❌ Old or malformed doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
While older doctypes like HTML 4.01 may not always produce this exact error, they can trigger related warnings. The HTML5 doctype is the recommended standard for all new documents.
✅ Correct: doctype as the first line
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
The <!DOCTYPE html> appears first, followed by the <html> tag with a lang attribute, a <head> with a required <title>, and the <body>. This is a minimal, fully valid HTML5 document.
✅ Correct: fragment with doctype added
If you were previously only writing a fragment like <div><p>Hello</p></div>, wrap it in a proper document structure:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Fragment</title>
</head>
<body>
<div>
<p>Hello</p>
</div>
</body>
</html>
Always ensure <!DOCTYPE html> is the absolute first thing in every HTML file you create. It's a one-line addition that guarantees standards-mode rendering and keeps your document valid.
The <!DOCTYPE> declaration tells the browser which version and type of HTML the document uses. In HTML5, this is simply <!DOCTYPE html>. According to the HTML specification, the DOCTYPE must be the first thing in the document — before any elements, comments, or whitespace. There can also be only one DOCTYPE per document.
The W3C validator reports "Stray doctype" when it encounters a <!DOCTYPE> declaration in an unexpected location. This typically happens when:
- The DOCTYPE is accidentally duplicated somewhere in the document (e.g., inside the
<head>or<body>). - Multiple HTML documents have been inadvertently concatenated or pasted together.
- A template engine or build tool is injecting an extra DOCTYPE.
- The DOCTYPE was mistakenly placed after other content, such as a comment, whitespace from a BOM (byte order mark), or a stray character before it.
This matters because a misplaced DOCTYPE can force the browser into quirks mode, which causes it to emulate legacy rendering behavior. In quirks mode, CSS box model calculations, font sizing, and many other layout behaviors differ from standards mode, leading to unpredictable and inconsistent rendering across browsers. A stray DOCTYPE inside the document body is simply ignored, but it signals a structural problem that can also cause accessibility tools and parsers to behave unexpectedly.
To fix the issue:
- Search your document for all occurrences of
<!DOCTYPEand remove any duplicates. - Make sure the single remaining
<!DOCTYPE html>is the absolute first thing in the file — no whitespace, no comments, no BOM characters before it. - If you're using templates or server-side includes, check that only one template is responsible for outputting the DOCTYPE.
Examples
❌ Incorrect: DOCTYPE duplicated inside the body
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<!DOCTYPE html>
<p>Hello, world!</p>
</body>
</html>
The second <!DOCTYPE html> inside the <body> is a stray doctype and will trigger the error.
❌ Incorrect: DOCTYPE appears after other content
<!-- This is my page -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
Even a comment before the DOCTYPE can cause issues. The DOCTYPE must come first.
❌ Incorrect: Two documents concatenated together
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page One</title>
</head>
<body>
<p>First page content.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Two</title>
</head>
<body>
<p>Second page content.</p>
</body>
</html>
This often happens when templates are incorrectly assembled. The second DOCTYPE (and entire second document structure) is stray content.
✅ Correct: Single DOCTYPE at the very start
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
The <!DOCTYPE html> declaration appears exactly once, as the very first thing in the file, followed by the <html> element. This ensures the browser uses standards mode and the document is valid.
Validate at scale.
Ship accessible websites, faster.
Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.
1K Weekly
1,000 monthly checks. Cancel anytime.
Get 1K Weekly →Join teams across 40+ countries