Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 5
HTML Comments & Whitespace Handling
Comments and whitespace handling are fundamental concepts every HTML developer must understand. Comments let you leave notes in your code that browsers completely ignore - they are essential for documenting complex code, temporarily disabling features, and collaborating with other developers. Whitespace handling is equally important because HTML collapses multiple spaces and line breaks into a single space, which often surprises beginners. Understanding these concepts prevents frustrating layout bugs and makes your code professional and maintainable.
HTML Comments
HTML comments are written between <!-- and -->. Everything between these markers is completely invisible on the web page - the browser ignores it entirely.
Comments serve several important purposes:
- 1Documentation: Explain complex sections of code so you (or another developer) can understand it later. Professional codebases rely heavily on comments.
- 2Debugging: Temporarily 'comment out' a section of HTML to hide it from the page without deleting it. This lets you test what happens when certain elements are removed.
- 3Organization: Mark the beginning and end of major sections. Example: <!-- START: Navigation --> ... <!-- END: Navigation -->
- 4TODO notes: Leave reminders for future work. Example: <!-- TODO: Add mobile menu -->
Important warnings:
• Comments ARE visible in the page source code (View Source or DevTools). Never put passwords, API keys, or sensitive information in comments.
• Comments cannot be nested. <!-- outer <!-- inner --> --> will break.
• Very long comments slow down page load because the browser still downloads them.
Comments explain your code; whitespace is collapsed into single spaces by browsers
Whitespace Collapsing Rules
- HTML collapses ALL consecutive whitespace (spaces, tabs, newlines) into a SINGLE space. Writing 'Hello World' displays as 'Hello World'.
- Line breaks in your HTML code do NOT create line breaks on the page. The browser treats newlines as spaces.
- Use <br> to force a line break within text. Use <p> tags for separate paragraphs with spacing.
- Use <pre> (preformatted text) to preserve ALL whitespace and line breaks exactly as written in the code.
- Use (non-breaking space) to add extra spaces that won't collapse. Example: 'Hello World' shows 3 spaces.
- The CSS property white-space: pre preserves whitespace like <pre>. white-space: pre-wrap preserves whitespace but wraps at container edges.
- Indentation in your HTML source code is for DEVELOPER readability only - it does not affect the rendered page layout.
Pro Tip
Never put sensitive information in HTML comments. Comments are invisible on the rendered page but fully visible in View Source and DevTools. Passwords, API keys, admin URLs, and database credentials should NEVER appear in comments - anyone can read them.
Comments & Whitespace Example
<!DOCTYPE html>
<html>
<body>
<!-- This is a comment - invisible on the page -->
<h1>Comments & Whitespace Demo</h1>
<!-- Section: Whitespace collapsing -->
<p>Multiple spaces collapse into one.</p>
<p>Line breaks
in source code
are treated as
single spaces.</p>
<!-- Force line breaks with <br> -->
<p>Line 1<br>Line 2<br>Line 3</p>
<!-- Preserve whitespace with <pre> -->
<pre>
This preserves
all spaces
and line breaks!
</pre>
<!-- Non-breaking spaces -->
<p>Price: $29.99</p>
<!-- TODO: Add more examples later -->
<!-- Temporarily hidden content:
<p>This paragraph is commented out.</p>
-->
</body>
</html>Try It Yourself: Comments & Whitespace
Quick Quiz
Key Takeaways
- Comments and whitespace handling are fundamental concepts every HTML developer must understand.
- HTML collapses ALL consecutive whitespace (spaces, tabs, newlines) into a SINGLE space. Writing 'Hello World' displays as 'Hello World'.
- Line breaks in your HTML code do NOT create line breaks on the page. The browser treats newlines as spaces.
- Use <br> to force a line break within text. Use <p> tags for separate paragraphs with spacing.
Finished this lesson?