Use HTML comments (<!-- -->) to improve code organization, documentation, and team collaboration. Learn how to add notes and explanations effectively.
HTML comments are lines of text within your HTML code that are ignored by the browser. They are not displayed on the web page itself but serve as notes for developers reading the code. Comments are essential for explaining complex code, documenting your work, leaving reminders, or temporarily disabling sections of HTML.
How to Write an HTML Comment
HTML comments start with an opening angle bracket, an exclamation mark, and two hyphens (<!--), and
end with two hyphens and a closing angle bracket (-->).
<!-- This is a single-line HTML comment -->
<!--
This is a
multi-line
HTML comment.
-->
Key Characteristics:
-
Ignored by Browser: The browser parses the HTML and skips over anything enclosed in
<!-- -->, meaning it has no effect on the rendered page. -
Visible in Source Code: Anyone viewing the page source (e.g., by right-clicking and selecting "View Page Source") can see the comments.
-
Plain Text: Comments can contain any text, including code snippets or explanations.
Common Uses for HTML Comments
-
Explaining Complex Code: If you have a section of HTML that is intricate or uses advanced techniques, a comment can clarify its purpose or logic.
<!-- This complex layout uses CSS Grid. The grid-template-areas define the main regions: header, content, sidebar, footer. See styles.css for detailed grid properties. --> <div class="container"> <!-- ... grid items ... --> </div> -
Temporary Disabling Code (Commenting Out): You can temporarily remove a piece of HTML from rendering without deleting it by commenting it out. This is useful for debugging or testing changes.
<!-- <div class="old-feature"> <p>This section is temporarily disabled.</p> </div> --> <div class="new-feature"> <p>This is the new, active section.</p> </div> -
Adding Notes and Reminders: Leave notes for yourself or other developers about future updates, TODO items, or important considerations.
<!-- TODO: Update this section with new product details by end of week. --> <p>Current product description...</p> <!-- Important: Ensure this form is validated by JavaScript before submission --> <form action="/submit" method="post"> <!-- ... form fields ... --> </form> -
Documenting Sections: Break down large HTML files into logical sections with comments, making the overall structure easier to navigate.
<!-- ===================== --> <!-- HEADER SECTION --> <!-- ===================== --> <header>...</header> <!-- ===================== --> <!-- MAIN CONTENT --> <!-- ===================== --> <main>...</main> -
Including Author Information or Copyright: While often placed in
<footer>, comments can also be used for such metadata.<!-- Page created by: John Doe Date: 2023-10-27 Version: 1.0 -->
Comments in Other Languages (Context)
While this section focuses on HTML comments, it's worth noting that other programming languages have their own comment syntax:
-
CSS:
/* This is a CSS comment */ -
JavaScript:
// This is a single-line JS commentor/* This is a multi-line JS comment */ -
Python:
# This is a Python commentor"""This is a multi-line Python docstring"""
Understanding these different syntaxes is important when working across HTML, CSS, and JavaScript.
Best Practices for HTML Comments
-
Be Concise and Clear: Write comments that are easy to understand quickly. Avoid overly long or ambiguous explanations.
-
Explain the "Why," Not Just the "What": Good comments explain why something is done a certain way, especially if it's non-obvious, rather than just restating what the code does.
-
Good:
<!-- Using flexbox here to center the absolutely positioned element --> -
Less Good:
<!-- This is a div with a button -->(The HTML itself shows this).
-
-
Keep Comments Up-to-Date: If you modify the code, remember to update any related comments. Outdated comments can be more confusing than no comments at all.
-
Don't Over-Comment: Avoid commenting every single line. Well-structured, semantic HTML often requires fewer comments. Use them for logic, intent, or reminders.
-
Avoid Sensitive Information: Never put passwords, API keys, or other sensitive data in HTML comments, as they are easily viewable in the page source.
-
Consider Build Processes: Some web development workflows involve build tools that can strip comments from production code to reduce file size. Be aware of this if comments contain crucial information for deployment.
HTML comments are a simple yet powerful tool for developers to make their code more understandable and maintainable. They are an integral part of writing professional and collaborative web code.
By implementing these form controls and best practices, you can create effective, user-friendly, and accessible web forms that enhance user engagement and data collection.
Related Articles
Our most attended masterclasses
Comments
Our most attended masterclasses