【问题标题】:Comment causing problems in HTML code注释导致 HTML 代码出现问题
【发布时间】:2014-04-12 17:01:35
【问题描述】:

我开始学习 HTML 并开始接触 CSS。

我正在尝试将 a 从黑色变为绿色(非常简单)。 现在,由于这是为了我自己的学习,我正在添加我自己的 cmets 以供参考。

我注意到如果我保留评论,CSS 将不起作用,文本会保持黑色,但是当我删除评论时,CSS 工作?

谁能解释一下为什么会这样?

这是我的代码

<html xmlns="http://w3.org/1999/xhtml"> 

<head> 
<title>Homepage</title> 
<meta http-equiv="Content-type" content="text.html; charset=UTF-8"/>
<meta name="keywords" content="CCS, in-line, internal, external, style sheets"/>
<meta name="description" content="CSS internal, external and in-line"/>
<meta name="author" name = "Jordan Atkinson" />

<!-- internal CSS, this is to be put after the meta tags and will be inside a tag called <style type="text/css>
This is the tag that is used so that CSS can recognise its start point, imagine it as the public static void main(String[] args) in Java-->
<style type="text/css">

<!-- Typing h1 (known as a 'selector' selects the element in the body you want to apply the CSS, the braces is where you put the attributes and values to
style an element. H1 can be any tag within the body, so you could type table if you wish to apply styles to tables. Color (american spelling):green 
changes the color to that specified after the colon. after the green, you MUST always end with a semi colon.-->
h1 {color:green;}

</style>

</head>

<body>

<h1>Homepage</h1>


</body> 

</html> 

导致问题的评论是这样的:

<!-- Typing h1 (known as a 'selector' selects the element in the body you want to apply the CSS, the braces is where you put the attributes and values to
style an element. H1 can be any tag within the body, so you could type table if you wish to apply styles to tables. Color (american spelling):green 
changes the color to that specified after the colon. after the green, you MUST always end with a semi colon.-->

任何帮助将不胜感激。

问候

约旦。

【问题讨论】:

    标签: html css tags comments stylesheet


    【解决方案1】:

    样式元素被定义为(在 HTML 4 术语中)包含 CDATA。

    它们包含样式表。它们不能包含标记。

    HTML cmets 是标记。

    在这种情况下,您的评论将被视为选择器的一部分。

    如果您想在样式元素中添加注释,请使用 CSS 注释。

    /* CSS comment syntax */
    

    您有一个xmlns 属性,因此您可能正在使用XHTML(这可能不是一个好主意,它会导致很多复杂性并且很少有开发人员可以从中受益)。您可以在 XHTML 中的样式元素中使用标记(因为 XML 没有内在 CDATA 的概念),但这与 HTML 不兼容,因此如果使用 text/html,您的文档将会中断。

    【讨论】:

    • 我关注的教程视频似乎出于某种原因在 HTML 和 XHTML 之间切换,我只有 6% 的视频教程,其中大约有 200 个,所以我希望这能解决问题很长的路:)但是谢谢你,我记住了这一点:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2017-08-29
    • 2013-01-03
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多