html - 为什么背景颜色 :attribute break when previous line contains comments?

标签 html css styles comments

所以我在最新版本的 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/

相关文章:

c# - 有人可以给我一些关于 WPF 的建议吗?

javascript - 如何使用 Canvas 创建如下链接的动画?

jquery - 浏览到目标常见问题解答主题

javascript - 左右定位的奇怪结果不同

html - Bootstrap 输入组字段大小不正确

html - 使每个 div 成为视口(viewport)的大小

html - 使用 css 如何创建一个药丸形状,一侧是圆形,另一侧是倾斜的?

html - 表格单元格未垂直对齐

css - Div 透明度在 IE8 中不起作用

javascript - 力向图 - 定义节点的颜色,如果该节点同时是源和目标