Comments in HTML
In HTML, comments
are used to write notes or explanations that are not displayed on the web page.
They are mainly used to document explanations about the code, modification details, author information, etc.
The CodeFriends web coding practice environment utilizes comments in HTML code to help learners better understand the code.
How to Use Comments
HTML comments begin with <!--
and end with -->
.
Anything wrapped within these comment tags will not be visible on the web page.
<!-- This line is a comment. It will not appear on the webpage --> <p>This sentence will appear on the web page.</p>
When to Use Comments
Comments are commonly used in the following situations:
Code Explanation
You can add explanations for complex code or portions that may need modifications later.
Disabling Code
You can temporarily disable a section of code by wrapping it in comments. The commented code will not appear on the webpage.
<!-- The image below is temporarily disabled. <img src="example.jpg" alt="Example Image"> -->
Section Separation
Comments can also be used to separate different sections of a web page.
For example, you can use comments to delineate sections of a website like the following:
-
Header
: The top of the web page, often containing the logo, main menu (navigation), and search bar -
Main
: The main content of the website -
Footer
: The bottom of the web page, including site information (social media, copyright, etc.)
<!-- Header Start --> <header>...</header> <!-- Header End --> <!-- Main Start --> <main>...</main> <!-- Main End --> <!-- Footer Start --> <footer>...</footer> <!-- Footer End -->
In this way, HTML comments improve code readability and facilitate collaboration with other developers.
Practice
Follow the highlighted sections in the code to input your text.
Creating HTML Comments
Write the HTML code to convert the content below into a comment.
Hint: HTML comments start with <!--
and end with -->
.
Body Content
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help