# Non-space character after body.

> Canonical HTML version: https://rocketvalidator.com/html-validation/non-space-character-after-body
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

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

1. **Accidental text or typos** after the closing `</body>` tag.
2. **Script or analytics tags** mistakenly placed between `</body>` and `</html>` instead of at the end of the `<body>`.
3. **Server-side template engines** appending content after the body in a layout file.
4. **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>`:

```html
<!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>`:

```html
<!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>`:

```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:

```html
<!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:

```html
<!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>`:

```html
<!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.
