Commenting out CSS

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Quote: http://www.w3.org/TR/xhtml1/#C_4
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.

I read this a while ago. I was wondering if anyone else read this, and what their thoughts are.

And which browsers choke on CSS?

Mark Hensler
If there is no answer on Google, then there is no question.

Busy's picture

He has: 6,151 posts

Joined: May 2001

As a rule I try not to use comments in .css files, if I do it's /* comment */

as to which browsers choke on CSS, they all can if done the right way Laughing out loud

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

If you use in your documents, just don't use the comments.

Browsers lower than N4.x and IE4 (or is it 3) will not recognize the tag and will ignore it.

The only issue with "choking" is when it knows that it is CSS, but has a hissy fit on the declarations themselves. In general, font-color, font-family, et cetera, background-color (and -image, et cetera) are fine in any browser that understands CSS. You'll want to keep padding/borders/margins from N4.x in some combinations.

hth,

Smiling S

Busy's picture

He has: 6,151 posts

Joined: May 2001

found this you may find interesting, have never used it

(XHTML and Javascript)

If you are using internal scripts, enclose it within the starting tag . This will mark it as unparsed character data. Otherwise characters like & and < will be treated as start of character entities (like &nbsp;) and tags (like ) respectively.
Example for XHTML Javascript:
<script type="text/javascript" language="Javascript">

//-->
</script>

(XHTML and CSS)

Enclose internal style definitions within the starting tag to mark it as unparsed character data.
Example:

Otherwise the & and < characters will be treated as start of character entities (like &nbsp;) and tags (like ) respectively.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I don't understant the quote in my first post. Specifically this part:
"Note that XML parsers are permitted to silently remove the contents of comments."

Does that mean this shouldn't be done:

Busy, why would you use tag? When would there be any < or & to parse?

Mark Hensler
If there is no answer on Google, then there is no question.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Mark, if you are able, it's highly recommended to use external or global stylesheets. When you need to use a local stylesheet, this is the way to do it:

body {background-color: #fff; }

Comments really aren't needed at all.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Why do I see them all over the place? Was this at one time necessary, or ... ?

Busy's picture

He has: 6,151 posts

Joined: May 2001

I think it's to do with the <?xml tag

the HTML comments "" were/are needed for version 3 browsers and below I believe, I guess they'd ignore the tag and try read the contents of it causing a big mess

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Also, convention because of the <script> convention. Sort of a "better safe than sorry" paranoia. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.