Comments is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand. comments are not displayed in the browser.
A CSS comment is placed inside the <style>
element, and starts with /*
and ends with */
:
Example:
Single-line Comments
<!DOCTYPE html>
<html>
<head>
<style>
/* This is a single-line comment */
h1 {
color: green;
}
</style>
</head>
<body>
<h1>Welcome to Fullstackadda.com</h1>
<p>Comments are not displayed in the browser</p>
</body>
</html>
Output:
Example 2:
Multi-line Comments
<!DOCTYPE html>
<html>
<head>
<style>
/* This is a
multi-line
comment */
h1 {
color: green;
}
</style>
</head>
<body>
<h1>Welcome to Fullstackadda.com</h1>
<p>Comments are not displayed in the browser</p>
</body>
</html>
Example 3:
Combintion of HTML and CSS comments.
<!DOCTYPE html>
<html>
<head>
<style>
/* This is css comment */
h1 {
color: green;
}
</style>
</head>
<body>
<!-- this is HTML Comment -->
<h1>Welcome to Fullstackadda.com</h1>
<p>Comments are not displayed in the browser</p>
</body>
</html>
Advantages of Comments:
Comments in programming languages are an essential tool for developers that provide a number of advantages:
- Code readability: Comments help make your code more readable and easier to understand, by providing additional context and explanations for your code. This can help other developers who may need to work with your code, or even help you understand your own code if you come back to it after some time has passed.
- Code maintenance: Comments can help make your code more maintainable, by providing a roadmap for future changes and improvements. By explaining the reasoning behind certain design decisions or pointing out areas that may need improvement, comments can help future developers avoid making costly mistakes or duplicating code.
- Collaboration: Comments can facilitate collaboration between developers, by enabling communication and collaboration within the code itself. By providing explanations and context for your code, comments can help team members understand each other’s contributions and work more effectively together.
- Debugging: Comments can also be helpful during debugging, by providing a way to temporarily disable certain parts of the code or explain how certain parts of the code are meant to function. This can help isolate bugs and make it easier to fix them.