Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: not2easy

<!-- Do not use comments -->

...in your style sheets

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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>

<!--
<style type="text/css">

...

</style>
//-->

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)

WebmasterWorld Senior Member 10+ Year Member



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)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

10+ Year Member



`-->` 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)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



`-->` 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 :)

gsx

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

10+ Year Member



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 <!-- ... -->.

Comments:
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)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thanks for clarification.

However...

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.

gsx

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

10+ Year Member



[w3.org...]

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)

10+ Year Member



>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.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month