HTML Guides for non-space character
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.
The HTML specification defines a strict structure for documents: the <html> element contains a <head> and a <body>, and all visible page content must live inside the <body>. Once the browser encounters the </body> closing tag, it expects nothing but optional whitespace (spaces, tabs, newlines) before the closing </html> tag. Any non-space characters in that gap — whether text, HTML elements, script tags, or even stray punctuation — are considered outside the document body and trigger this validation error.
Why this matters
- Standards compliance: The HTML living standard (WHATWG) is clear that content after
</body>is not valid. W3C validation will flag this as an error. - Unpredictable rendering: Browsers use error recovery to handle misplaced content, and different browsers may handle it differently. Most will implicitly reopen the
<body>and move the content inside it, but relying on this behavior is fragile and unpredictable. - Accessibility: Screen readers and assistive technologies parse the DOM structure. Content that lands in unexpected places due to error recovery may be announced out of order or missed entirely.
- Maintainability: Stray content after
</body>is often a sign of a code organization problem — a misplaced include, a template rendering issue, or an accidental paste — and cleaning it up improves long-term maintainability.
Common causes
- Accidental text or typos after the closing
</body>tag. - Script or analytics tags mistakenly placed between
</body>and</html>instead of at the end of the<body>. - Server-side template engines appending content after the body in a layout file.
- Comments or debugging output left behind after
</body>.
Examples
Stray text after </body>
This triggers the validation error because the text this is invalid appears after </body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<p>Content goes here.</p>
</body> this is invalid
</html>
Move all content inside the <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<p>Content goes here.</p>
</body>
</html>
Script placed outside the body
A common mistake is placing a <script> tag between </body> and </html>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<p>Main content</p>
</body>
<script src="analytics.js"></script>
</html>
Place the script at the end of the <body> instead:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<p>Main content</p>
<script src="analytics.js"></script>
</body>
</html>
Stray HTML element after </body>
Sometimes an element like a <div> ends up after the body due to a template issue:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<main>Page content</main>
</body>
<div id="overlay"></div>
</html>
Move it inside <body>:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<main>Page content</main>
<div id="overlay"></div>
</body>
</html>
The fix is always the same: ensure that everything between </body> and </html> is either whitespace or nothing at all. All content — text, elements, scripts, and comments — belongs inside the <body> element.
A <noscript> inside the <head> contains text, but there it may only hold <link>, <style> and <meta> elements.
The <head> is never rendered, so a <noscript> placed there can only change metadata, for example by loading a fallback stylesheet. Whitespace and comments are fine. Any other character counts, including , because a non-breaking space isn't whitespace to the HTML parser.
When JavaScript is off, the parser treats that text as the start of the page content. It closes the <noscript> and the <head> early and opens the <body> there. The text and everything after it end up in the body, which is why the validator usually follows this error with Stray end tag “noscript”, Stray end tag “head” and a complaint about a second <body> start tag. Any <meta> or <link> that comes after the <noscript> also lands in the body.
A message for visitors without JavaScript belongs in a <noscript> inside the <body>. Keep the <noscript> in the <head> for metadata only, or remove it.
Invalid example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<noscript>Please enable JavaScript to use this site.</noscript>
</head>
<body>
<p>Hello</p>
</body>
</html>
Valid example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<noscript>
<link rel="stylesheet" href="no-js.css">
</noscript>
</head>
<body>
<noscript>
<p>Please enable JavaScript to use this site.</p>
</noscript>
<p>Hello</p>
</body>
</html>
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