Comment trong CSS giúp ghi chú, tạm vô hiệu hóa code, tổ chức file. Cú pháp: /* comment */.
Cú pháp
- Single-line:
/* comment */ - Multi-line:
/* This is a multi-line comment */
Ví dụ trong stylesheet
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is a multi-line comment
explaining the following rule */
h1 { color: blue; }
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Best practice
- Comment **section**:
/* === HEADER === */,/* === FOOTER === */. - Comment **hack/fix**:
/* Fix IE11 flex bug */. - Comment **TODO**:
/* TODO: Refactor this */. - KHÔNG lồng comment:
/* outer /* inner */ outer */→ lỗi (inner đóng outer). - Minifier (production) sẽ xóa comment. Giữ comment trong source dev.
So sánh với HTML/JS comment
| Language | Single-line | Multi-line |
|---|---|---|
| CSS | /* comment */ | /* comment */ |
| HTML | <!-- comment --> | <!-- comment --> |
| JavaScript | // comment | /* comment */ |