homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / CSS
Forum Library, Charter, Moderators: not2easy

CSS Forum

<!-- Do not use comments -->
...in your style sheets

 10:53 pm on Mar 20, 2003 (gmt 0)

Historically we were recommended to use HTML comments to hide style sheets from "older" browsers. It could look something like this:
<style type="text/css">



<style type="text/css">



However, XML compliant browsers (version 6/7 or later) are allowed to silently remove any content within these comments. This might cause unexpected results!

Note that it doesn't matter whether your page follows HTML, XHTML, or XML - the problem is the same. Therefore, do not use comments in your style sheets!

The same goes for JavaScripts...

Reference: [w3.org]
C.4. Embedded Style Sheets and Scripts
Use external style sheets if your style sheet uses < or & or ]]> or --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.



 1:47 am on Mar 22, 2003 (gmt 0)

I prefer to always use external stylesheets.
Internal or inline style information has to be downloaded with each and every page. But using an external stylesheet instead gives the browser the opportunity to cache it.

Thus reducing the time my users wait for subsequent pages and (slightly) reducing the load on my server.


 12:30 pm on Mar 22, 2003 (gmt 0)

I agree with you. I prefer external style sheets (and scripts) too. However, there are times when an internal style sheet is needed... and there are still a lot of people using comments in their internal style sheets. Almost every day someone posts a question on here with a problem related to this.


 1:30 pm on Mar 22, 2003 (gmt 0)

`-->` is used with a STYLE element.
`//-->` is the technique in the inside of a SCRIPT element.

#If strict with specification, both will be truly treated as a comment in XML.
#We should use an external CSS.


 1:37 pm on Mar 22, 2003 (gmt 0)

`-->` is used with a STYLE element.
`//-->` is the technique in the inside of a SCRIPT element.

Both are frequently used by people in the <style> element. However, both are equally incorrect CSS syntax (which is why they are ignored by browsers).

External CSS is wonderful! I wish more people would listen to your advice :)


 3:05 pm on Mar 22, 2003 (gmt 0)

The second format is wrong:

You are hiding the <style> tag from some browsers by having it inside a comment.

The correct format is always: <tag><!-- details --></tag>

Any browser that does not uderstand tag, ignore it. It will ignore the comment too. but <!--<tag> details <tag>--> means that all browsers should ignor the whole statement. You are also nesting the < and > symbols which may confuse some browsers, it shouldn't but it might.

You also state not to use comments within stylesheets. This needs clarifiying. Styles in the HTML require the comment tags as in your first example but without the '//' (as stated before this is needed for JavaScript only). Comments in an exernal stylesheet should be of the format /*....*/ and NOT <!-- ... -->.

1: HTML: <!-- ... -->
2: Style: /* ... */
3: JavaScript: // ...
4: Perl: # ...

It's no wonder it's can become confusing.


 3:17 pm on Mar 22, 2003 (gmt 0)

Thanks for clarification.

Styles in the HTML require the comment tags

HTML comments (<!-- -->) should not be used within the <style> tags whatsoever. It may hide your styles from XML compliant browsers.


 6:59 pm on Mar 22, 2003 (gmt 0)


So are you saying this affects .xml files only, or .html as well? Because I have never seen the problem.


 8:01 pm on Mar 22, 2003 (gmt 0)

>So are you saying this affects .xml files only, or .html as well?

There is even no necessity of commenting out, in HTML.
This technique is for the browser which does not understand a STYLE element.

In XML(includes XHTML), the contents of a STYLE element are #PCDATA.
Therefore, `<!-- ... -->` is truly treated as a comment, the contents from `<!--` to `-->` is not parsed.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / CSS
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved