CSS Comments

CSS Comments: Explaining Your Code

CSS comments are used to add notes or explanations to your code, helping you and others understand it better.

Comments can be added anywhere in the CSS code, and they can span across single or multiple lines. It’s a good practice to add comments to clarify complex parts of your code for future reference or collaboration.

Note: Comments are simply ignored by the browsers, so they won't affect how your webpage looks or works in any way. Older methods like <!-- --> for hiding CSS in older browsers are outdated and not recommended.


Syntax

In CSS, a comment starts with / and ends with /.

/* Code comments */

1. Single-line Comment

You can place a comment on a single line to explain the code immediately following or preceding it.

Single-line Comment Example:

<!DOCTYPE html>
<html>
<head>
    <title>Single line comment</title>
    <style>
        h1 {
            color: navy;
        }
        /* This is a single line comment */
    </style>
</head>
<body>
    <h1>IntricateDevo</h1>
    <p>Study portal for CS students</p>
</body>
</html>

2. Multi-line Comment

When you need to write longer explanations, your comment can span across multiple lines. The browser will ignore everything between the opening / and the closing /.

Multi-line Comment Example:

<!DOCTYPE html>
<html>
<head>
    <title>Multi-line Comment</title>
    <style>
        h1 {
            color: navy;
        }
        /* This is a multi-line
           comment that spans across
           several lines */
    </style>
</head>
<body>
    <h1>IntricateDevo</h1>
    <p>A Computer Science portal</p>
</body>
</html>

Browser Support

CSS comments are universally supported across all modern browsers and platforms. Since they are ignored by the browser, they do not affect the visual layout or functionality of the web page. No special handling or cross-browser testing is required for comments.


Exercise

?

Which of the following is the correct syntax for a CSS comment?