Why can I close the style tag from inside CSS comments?

css, html

Solution

HTML is parsed first. This is why you might see `<\/b>` in JavaScript that works with HTML: older browsers used `</` as the "end of script block" marker (newer ones specifically look for `</script>`).

So the HTML is parsed, and the result is two `<style>` elements, one with a starting comment and the other with an ending comment.

If you need comments like that, consider using HTML comments:

<style><!--
    /* Whatever you want here! */
--></style>

All being well, this should allow the stylesheet to be parsed correctly.

Problem

Why <\style> in comments is breaking embedded styles? I thought I could write there whatever I want! ``` <!doctype html> <meta charset="utf-8"> <title>test</title> <style> /* </style> <h1>HTML!</h1> <style> */ </style> ``` You can see it in action here.

Original source