Opened 8 weeks ago
Last modified 7 weeks ago
#66156 new defect (bug)
W3C Error : Element style not allowed as child of element body in this context.
| Reported by: | gaet | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Script Loader | Version: | |
| Severity: | minor | Keywords: | |
| Cc: | Focuses: | css |
Description
Hi,
Whenever I run a W3C check on WordPress sites, this error always pops up. How can I avoid it?
Element style not allowed as child of element body in this context. <style id="core-block-supports-inline-css">
Attachments (1)
Change History (6)
#2
@
8 weeks ago
Hello,
Thank you for these explanations; I fully understand why this code is included in the wp_footer.
However, even though browsers understand how this code works and execute it correctly, the W3C still generates this error, meaning it isn’t valid code according to web standards… Should we contact the W3C? Is there a way within WordPress to do things differently or to ignore this error?
Thanks
#3
follow-up:
↓ 5
@
8 weeks ago
Printing styles in the BODY is normal and expected. The W3C Validator is being overly strict according to the spec.
However, if you are running a block theme, the core-block-supports inline style is already moved to the head due to #55148.
For classic themes, this hasn't been an option because the head is printed before the blocks in the body are constructed, meaning it doesn't know what styles to print until it gets to the footer.
That said, this is what #64099 in WordPress 6.9 should have eliminated as a hard requirement. And actually, when I test in a block theme or classic theme, I do not see <style id="core-block-supports-inline-css"> printed in the BODY.
Note there are challenges with moving styles up to the head which previously were in the footer results in a change to the CSS cascade. There are ongoing bugs yet unresolved related to this (e.g. #65272). This has necessitated some sites running the Load Combined Core Block Assets plugin.
@gaet What version of WordPress are you running?
#4
@
7 weeks ago
- Component General → Script Loader
- Focuses css added
- Severity normal → minor
Moving this over to the Script Loader component as it's really a Script & Style Loader. I've also lowered the severity as sites are rendering as intended.
Reviewing the HTML Spec for the body element, there is no mention of specific elements permitted or not permitted in the body element but there is a reference to including style tags:
Tag omission in text/html:
A body element's start tag can be omitted if the element is empty, or if the first thing inside the body element is not ASCII whitespace or a comment, except if the first thing inside the body element is a meta, noscript, link, script, style, or template element.
A body element's end tag can be omitted if the body element is not immediately followed by a comment.
(My emphasis)
Thanks for the report. This is coming from wp_enqueue_stored_styles(). For classic themes, block-support CSS (and duotone SVG filters) is intentionally printed at wp_footer (priority 1) rather than wp_head, to avoid adding render-blocking inline CSS for styles that may only be known once the page content has rendered. The same applies to duotone's SVG filter output, also hooked to wp_footer. The
-inline-csssuffix on the tag's id (<style id="core-block-supports-inline-css">) is just how WP_Styles prints any inline style - it's{handle}-inline-cssfor every inline style tag, nothing specific to this handle.Per the WHATWG HTML spec,
<style>is only permitted where metadata content is expected (i.e.<head>, or inside a<noscript>that's a child of<head>) - it's no longer valid as flow content in<body>(the oldscopedattribute exemption was removed years ago). Browsers are lenient and render it fine, but W3C's validator correctly flags it per spec.This looks like an intentional tradeoff rather than an oversight. Moving this back into
<head>for classic themes would reintroduce the render-blocking/ordering issue it was moved away from.