在 CSS 中,注释可用于在样式表中添加解释性注释或注释,这些注释不会被 Web 浏览器解释为样式说明。
语法
/* This is a comment */
p {
color: red; /* Set text color to red */
}
CSS 注释旨在为开发人员带来好处,在呈现网页时会被浏览器忽略。它们在文档、调试等方面很有用。
CSS 注释的类型
在CSS中,有两种主要的方式可以创建注释:
- 单行注释: 使用 /* 开始注释,使用 */ 结束注释来创建单行注释。
- 多行注释: 多行注释允许您添加跨多行的注释。它们也包含在 /* 和 */ 中。
/* Single line Comment */
/*
Comment
which stretches
over multiple
lines
*/
HTML 和 CSS 注释
在 HTML 教程中,我们了解到,HTML 中的命令定义在 <!-- 和 --> 个符号之间。
语法
<html>
<head>
<style>
/* This is a CSS Comment */
</style>
</head>
<body>
<!-- This is an html comment format -->
</body>
</html>
下面是一个显示 html 注释和 CSS 注释格式的示例:
<html>
<head>
<style>
/* Target all div elements */
div {
background-color: red; /* Set background color */
height: 50px; /* Set the height */
width: 200px; /* Set the width */
padding: 5px; /* Set the padding */
border: 5px solid black; /* Set the border */
}
</style>
</head>
<body>
<!-- This is an html comment format -->
<div>
Styles Applied
</div>
</body>
</html>