所以我在最新版本的 chrome 上试验内部样式表,似乎有一个错误破坏了代码。
出于某种原因,我无法在不导致代码失败的情况下在 background-color:rgb(51,51,51) 之前添加任何注释。
这是我的代码(背景颜色不变):
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
body {
width:100%; <!--browser screen must be fixed width-->
height:100%; <!--and height-->
margin:0px; <!--removes uneven margin added to row's margins-->
background-color:rgb(51,51,51); <!--note that height and width must be specified to work-->
}
</style>
</head>
<body>
<h1>
Headline
</h1>
</body>
</html>
这是我的其他代码(这次后台有效):
<html lang="en">
<head>
<meta charset="utf-8">
<style>
body {
background-color:rgb(51,51,51); <!--note that height and width must be specified to work-->
width:100%; <!--browser screen must be fixed width-->
height:100%; <!--and height-->
margin:0px; <!--removes uneven margin added to row's margins-->
}
</style>
</head>
<body>
<h1>
Headline
</h1>
</body>
</html>
请注意,注释可能没有意义(我删除了额外的代码但保留了注释)。那么到底发生了什么?我做错了什么?
最佳答案
定义见CSS2 specification :
Comments begin with the characters "/*" and end with the characters "*/"
所以你应该使用/* */
CSS 中的注释。
然而HTML风格的注释<!-- -->
也是可能的,但是唯一有效的位置是包装整个 CSS 规则 block :
<style>
<!--
body {
background-color:rgb(51, 51, 51);
width:100%;
height:100%;
margin:0px;
color: red;
}
-->
</style>
<!-- -->
分隔符用于防止不支持 HTML 3.2 的浏览器显示 CSS block 。 (这与用 <!-- -->
包装 Javascript 代码相同)。不过,这些都是非常古老的用户代理。
关于html - 为什么背景颜色 :attribute break when previous line contains comments?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26571050/