style in body is now valid HTML


The HTML validator was recently updated. It is now valid to include a <style> element inside the <body> rather than the <head> - albeit only when meeting certain conditions. While this has always worked in browsers, until recently it was not technically valid.

Here are the rules for using <style> inside of <body>:

  • The style element must be the first child of its parent.
  • All rules inside the style element must be inside a @scope rule.

The following is valid:

<body>
<div>
    <style>
        @scope {
            h1 {
            color: yellowgreen;
        }
        }
    </style>
    <h1>Testing</h1>
</div>
</body>

The following is invalid, for several reasons:

<body>
<div>
    <h1>Testing</h1>
<style>
    h1 {
        color: yellowgreen;
    }
</style>
</div>
</body>