samin
samin

Reputation: 45

Why do HTML comment delimiters not work within HTML style tags?

I am using CSS and HTML. I am using an external stylesheet and a "style" element within the HTML to override the external stylesheet.

My question is: when I put an HTML comment inside the "style" element, then the style does not override the external CSS. However, when I use CSS comments inside the "style" element then it does work. HTML comments belong inside HTML and the "style" element is still HTML, so why do the HTML comments inside the "style" element cause the overrides to fail?

For example:

<html>
    <head>
        <title>test</title>
        <style>
            <!-- body{background:#eee;} -->
        </style>
    </head>
    <body></body>
</html>

The above example demonstrates the odd behavior of applying the style in spite of the comment tags.

jsFiddle Demo (tested in Chrome 16.0.912.75 Windows 7)

Upvotes: 0

Views: 585

Answers (2)

James Cazzetta
James Cazzetta

Reputation: 3220

Use /* Style */ for Style Commenting and the above for HTML.

Upvotes: 5

srijan
srijan

Reputation: 1512

CSS is not a markup language. The style block inside your HTML page is an autonomous piece of code. Similar to javascript inside script block. Its not HTML. So HTML comments are not technically comments inside style block.

Upvotes: 4

Related Questions