Comments
Exercise
- In the "index.html" file, write the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My First Web Page</title> </head> <body> </body> </html>
- Save.
- If "Live Server" is not already running, right-click anywhere in the editor and select "Open with Live Server" from the context menu.
- Add the following code between the opening and closing body tags:
<!-- This is a comment and will not be displayed in the browser --> <h1>Lots of Comments!</h1>
- Save.
- In the browser, you should see the following:
- Update the code and comment out the heading like this:
<!-- <h1>Lots of Comments!</h1> -->
- Save.
- The heading will stop showing on the browser.
Code Walkthrough and Explanation
<!-- This is a comment and will not be displayed in the browser -->
This is the way to add comments in HTML by putting text in between <!--
and
-->
.
Comments in HTML are used to add annotations to the code that are not displayed in the final web pages. Comments
are
ignored by the web browser and are only intended to provide information or explanations to the person reading
the
code.
<!-- <h1>Lots of Comments!</h1> -->
Sometimes when you want to temporarily stop a piece of code from rendering, you can also comment it out. This is helpful for troubleshooting your code to find problems.
There are several reasons to use comments in HTML:
- Documentation: Comments can be used to add notes and explanations to the code, making it easier for others to understand what the code is doing.
- Debugging: Comments can be used to temporarily remove sections of code to help troubleshoot issues.
- Collaboration: Comments can be used to communicate with other team members about specific sections of code or to provide feedback on changes made to the code.
- Organization: Comments can be used to structure the code and to separate sections of code into logical sections, making it easier to navigate and understand the code.
In summary, comments in HTML are a useful tool for making code more readable and understandable, improving collaboration, and simplifying the development process.
Experiment with the Code
- Try adding your own comments.
Video and Code References
Questions? Subscribe and ask in the video comments: