Head Not First in HTML

No Comments
Head not first in html

Element Code: HT-039

TL;DR: Per the HTML spec, head has to be the first child inside html, before body. When something (a stray tag, a script, whitespace from a template bug) sits in front of it, browsers and crawlers start guessing where your metadata actually lives, and guessing is not a plan. Fix the source order and it goes away.
Element Code
HT-039
Category
HTML structure
Impact
Metadata parsing risk
Detection
View source, validator
Fix effort
Low, template fix

What This Issue Actually Is

The HTML standard defines a strict document structure: <html> contains exactly one <head> followed by exactly one <body>, in that order, with head coming first. This check flags pages where something other than head appears as the first content inside html, commonly a stray text node, an errant <script> tag, an HTML comment placed oddly by a template engine, or in the worst cases actual visible markup like a <p> or <div> sitting ahead of the head element in source order.

This usually is not something a developer typed on purpose. It creeps in from CMS templates that inject a tracking snippet or an ad script above the doctype-to-head boundary, from server-side includes that leave a stray newline-plus-comment before the head tag, or from broken conditional logic that outputs a fragment before the rest of the document assembles. It is a source-order bug, not a visual one, so it is easy to ship without anyone noticing in a normal browser preview.

Why This Matters

Browsers run HTML through an error-correction algorithm defined in the parsing spec, so a page with head not first will usually still render something reasonable, browsers are extremely forgiving and will silently move things around to build a valid DOM. That forgiveness is exactly the problem: it means the bug can sit in production for months with zero visible symptom while quietly putting your metadata parsing at risk.

The real danger is with crawlers and less-forgiving parsers. If non-head content such as a paragraph or an image appears before your actual head element and its metadata, some crawlers and validators will treat that as the effective end of head, meaning your title tag, meta description, canonical, hreflang, and structured data that were meant to live in head can end up interpreted as being inside body instead, where they are not supposed to be and may not be honored the way you intended. Screaming Frog's SEO Spider calls this out explicitly as a validation issue for exactly that reason, metadata correctness depends on the parser reliably knowing where head ends and body begins.

There is also a second-order cost: pages with malformed head boundaries are more likely to have duplicate head elements too, since whatever broke the ordering often also duplicates a script or meta block during the same templating failure. I have traced more than one "our meta description keeps reverting" ticket back to two head-shaped blocks fighting for the browser's attention, and it always starts with a source-order bug like this one.

Correct vs Broken Source Order

Broken <html> <!-- stray include --> <head> <title>...</title> </head> <body> ... </body> </html>

Correct <html> <head> <title>...</title> </head> <body> ... </body> </html> head is first child

How to Detect It

Start with view-source on the raw HTML response, not the rendered DOM in DevTools, DevTools shows you the browser's already-corrected version, which hides the bug you are trying to find. Search the raw source for anything between <html> and <head> that is not pure whitespace.

Screaming Frog SEO Spider flags "Head Not First In HTML Element" directly as a validation issue during a crawl, running your whole site through it is the fastest way to find every affected template at once rather than checking pages one at a time. The W3C Markup Validator and other HTML validators will also catch this as a structural error if you paste in raw source. Because this bug tends to come from a shared template or include, if you find it on one page, crawl the site to see how many templates it touches, it is rarely isolated to a single URL.

Server logs and Googlebot's rendered HTML (available via the URL Inspection tool in Search Console, "View Crawled Page") are worth checking too, comparing what you sent against what Google says it received tells you whether the parser correction is masking the bug for search engines the same way it does for browsers.

How to Fix It, Step by Step

  1. Pull the raw HTML source, not the DevTools-rendered DOM, and locate exactly what sits between the opening <html> tag and the opening <head> tag.
  2. Identify the source. Common culprits: a server-side include or partial injected above the head block, a tag manager snippet pasted in the wrong template region, leftover debug output, or a templating engine emitting a stray comment or newline from a conditional block.
  3. Move the offending content to where it belongs, inside head if it is a script or meta tag meant to be there, or removed entirely if it is debug leftovers or a misplaced include.
  4. Check for a duplicate head while you are in the template, this bug often travels with a second head-like block elsewhere in the page.
  5. Validate the fixed template against the raw source, confirm head is now the literal first child of html with nothing but the doctype and opening html tag ahead of it.
  6. Re-crawl the site to confirm the fix propagated across every page using that template, not just the one you tested by hand.

Where This Commonly Comes From

SourceTypical fix
Tag manager snippet pasted above headMove script into head, or use the container's native head injection slot
Server-side include or partialReorder the include chain so head loads first
Templating engine stray comment/whitespaceTrim conditional output, confirm no leading text nodes
CMS plugin auto-inserting markupConfigure plugin's injection point, or disable and hardcode instead
Debug or logging output left in templateRemove entirely before deploy
DO

  • Check raw source, not the DevTools-rendered DOM
  • Confirm head is the literal first child of html
  • Crawl the whole site once you find it on one page
  • Compare your source against Googlebot's rendered HTML in Search Console
  • Check for a duplicate head block while you are fixing this
DON'T

  • Assume it is fine because the page looks normal in a browser
  • Only check the rendered DOM, it hides the actual source-order bug
  • Paste tracking snippets above the head tag without checking placement
  • Treat it as fixed after patching just one page of a shared template
  • Ignore it because "browsers handle it," crawlers and validators are less forgiving

FAQ

If my page looks fine in the browser, do I really need to fix this?
Yes. Browsers auto-correct malformed source order using their built-in parsing algorithm, so visually you will not notice anything wrong. The risk sits with how crawlers and validators interpret your metadata boundary, not with what a human sees on screen, so a clean visual render tells you nothing about whether this is fixed.
Can this actually cause my title tag or meta description to be ignored?
It can, in the specific case where visible markup like a paragraph or div sits ahead of your intended head block. Some parsers treat that as the natural end of head, so real metadata sitting after it can end up read as body content instead. Comments and whitespace alone are less likely to trigger this, but non-whitespace elements are a genuine risk.
Is this the same issue as having two head elements?
Related but not identical. Head not first is a source-order problem, something appears before head. Duplicate head is a count problem, head appears more than once. In my experience they often show up together because both usually trace back to the same broken include or template injection.
How do I check the raw source instead of the rendered DOM?
Use view-source in your browser or curl the URL directly, both give you exactly what the server sent before any browser correction happens. DevTools' Elements panel shows the corrected DOM after parsing, which is the wrong place to look for this specific bug.
Which crawlers or tools actually flag this?
Screaming Frog SEO Spider surfaces it directly as a validation issue during a standard crawl. General HTML validators, including the W3C Markup Validator, will flag it as a structural error too. It is worth adding to a recurring crawl schedule rather than a one-time check, since it tends to reappear after template or plugin updates.

Tools

  • View source / curl: the only reliable way to see true source order before browser correction
  • Screaming Frog SEO Spider: flags head-not-first as a validation issue during crawl
  • W3C Markup Validator: structural HTML validation against the spec
  • Search Console URL Inspection: compare your source to Googlebot's rendered/crawled HTML
Want your templates checked for structural HTML issues like this one across the whole site?

Get an Advanced SEO Audit →

Claude Vincent is a technical SEO consultant focused on crawlability, rendering, and AI-search visibility. He writes the field guides and case studies at SEO ProCheck, with a bias toward the durable, unglamorous work that decides whether search engines and AI answer engines can actually read and cite a site.

About SEO ProCheck

Technical SEO consulting and GEO strategy with 20 years of enterprise experience. Case studies, resources, and tools for search and AI visibility.

Work With Me

Technical SEO audits, GEO strategy, site migrations, and international SEO. Hourly consulting for teams who need hands-on support, not just reports.

Subscribe to our newsletter!

More from our blog