问题
Possible Duplicate:
Commenting out stylesheets in HTML documents to support older browsers
I am reading Professional ASP.NET 2.0 published by Wrox. see below code.
<head>
<style type="text/css">
<!--
body {
font-family:Verdana;
}
-->
</style>
</head>
following lines are from book:
HTML comment tags are included because not all browsers support internal stylesheets.It is generally the older browsers that do not accept them. putting HTML comments around the style definitions hides these definitions from very old browsers.
now my question is that how it is possible that contents between comments hides to only old browsers not to new ones? is new browsers not get affected by comments?
回答1:
Old (or rather, ancient) browsers do not recognise <style> so it gets treated like any other unknown element and its content is treated as HTML. The comment is treated as a comment, so nothing is rendered.
Browsers which support <style> know to treat the content as CSS and ignore the HTML comment (as per the specification).
回答2:
The idea is that this is a technique is built into the way new browsers support inline style sheets. It's just a rule that in an internal stylesheet, the html comment delimiters (<!-- and -->) are ignored, so that this method will work. (If I'm not mistaken they are ignored only at the beginning and end of the stylesheet, but I'm not completely sure about that.)
In older browsers, it will not know what to do with the script tag, so it will put everything in it straight on the page. However, there is nothing in it but a comment, so the user won't get to see anything. If you do want to use comments in your CSS, use the CSS comment style (/* and */).
Do also note that this is method exists for very old (or ancient) browsers. This technique isn't very common anymore these days and I dare say it's not necessary anymore either (browsers that old probably won't display your page anywhere near correctly anyway).
来源:https://stackoverflow.com/questions/13219650/html-comments-tags-in-internal-stylesheet