CSS - 註釋



在 CSS 中,**註釋**用於在樣式表中新增解釋性說明或註釋,這些註釋不會被網頁瀏覽器解釋為樣式指令。

語法

/* 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>
廣告