Help, I need to fix code error 'Attempting to close HtmlElement 'body' before HtmlElement 'style'...

Topic summary

A user encountered an HTML validation error in their Shopify theme’s theme.liquid file: “Attempting to close HtmlElement ‘body’ before HtmlElement ‘style’…”

Root Cause:

  • Improper HTML nesting where the </body> tag closes before an unclosed </style> tag
  • This breaks HTML structure and affects page rendering

Solution Provided:

  • Ensure all <style> tags (and other elements) are properly closed before the </body> tag
  • Check for unintended code injection via Liquid that might add unclosed <style> tags
  • Verify conditional Liquid statements don’t leave tags open

Outcome:
The issue was successfully resolved after implementing the suggested fix.

Summarized with AI on November 3. AI used: claude-sonnet-4-5-20250929.

Can anyone help me tidy up an code error I’m getting in the theme.liquid of the site I’m building?

It’s at the bottom and saying 'Attempting to close HtmlElement ‘body’ before HtmlElement ‘style’ was closed.

Screenshots attached. URL is https://flippin-tables-rubs.myshopify.com/

password skahmo

Hey @SassInnes

This error typically occurs when the HTML structure in your theme.liquid file is not correctly nested, i.e., a tag is closed before a tag that should have been closed earlier. This breaks the HTML structure and can cause issues in rendering the page.

Here’s how the bottom of your theme.liquid file should look:


    
    

    # Welcome to My Store
    

Also Search for Unintended Code Injection: If you’re dynamically injecting code via Liquid, ensure no unclosed tags are added.

{% if some_condition %}
    
{% endif %}

It will cause the error if you have below coding structure.


    

Fix the Code Ensure the tag or any other tag which is properly closed before closing the tag.

thank you! that worked :slightly_smiling_face:

1 Like