Comments & Whitespace

Two small topics that make an enormous difference to the readability and maintainability of your HTML — and how browsers handle invisible characters.

What Is an HTML Comment?

A comment is a note you write inside your HTML file that the browser completely ignores when rendering the page. The visitor never sees it. Only people reading the source code can see it.

Comments are written like this:

<!-- This is a comment. The browser ignores everything between these markers. -->
  • <!-- — opens the comment (note: two hyphens, no space after <!)
  • This is a comment. — the note itself, anything you like
  • --> — closes the comment (two hyphens then greater-than)

Why Use Comments?

Comments serve several important purposes:

1. Explain Your Code

HTML can become complex, especially with deep nesting. A comment explains why something is done, not just what:

<!-- Navigation menu - links must match the folder structure exactly -->
<nav>
  <a href="index.html">Home</a>
  <a href="about/index.html">About</a>
  <a href="contact.html">Contact</a>
</nav>

2. Mark the End of Long Sections

When a closing tag is far away from its opening tag, it's easy to lose track of which element you're closing. A comment at the closing tag helps:

<div class="main-wrapper">

  <!-- ... hundreds of lines of content ... -->

</div> <!-- end .main-wrapper -->

3. Temporarily Disable Code

Instead of deleting something you might want back later, you can "comment it out." It disappears from the page but stays in your file:

<!--
<p>This paragraph is temporarily hidden while I work on the design.</p>
-->

This is called commenting out code. It's a debugging technique you'll use constantly.

4. Leave Notes for Yourself and Others

<!-- TODO: Add the contact form once the backend is ready -->
<!-- FIXME: This section breaks on mobile screens wider than 320px -->
<!-- Last updated: March 2025 -->

Multi-Line Comments

Comments can span multiple lines:

<!--
  Author: Jane Smith
  Last updated: June 2025
  Description: Homepage layout for Acme Corp website
  Note: the sidebar is hidden on mobile via CSS only
-->
⚠ Comments are public

HTML comments are invisible on the rendered page but are fully visible in the source code. Anyone can see them by pressing Ctrl+U / Cmd+U in their browser. Never put passwords, private information, or sensitive notes in HTML comments. Developers have embarrassed themselves (and their companies) by leaving internal notes in comments that users discovered.

Keyboard Shortcut for Comments

In VS Code, you can toggle a comment on the current line (or selected lines) with:

  • Windows / Linux: Ctrl + /
  • Mac: Cmd + /

VS Code detects the file type and uses the correct comment syntax automatically. In an HTML file it wraps the selection in <!-- -->. In a CSS file it uses /* */. In JavaScript it uses //. This shortcut works in virtually every code editor.

What Is Whitespace in HTML?

Whitespace refers to any invisible characters in your source code: spaces, tabs, and line breaks (newlines). In normal text, several spaces in a row create — several spaces. In HTML, it works very differently.

The Whitespace Collapse Rule

HTML collapses multiple whitespace characters (spaces, tabs, new lines) into a single space. This is called whitespace collapsing.

<p>This    has     many      spaces      in    the    source.</p>
<!-- Renders as: "This has many spaces in the source." -->

<p>
  This paragraph
  is spread across
  multiple lines.
</p>
<!-- Renders as: "This paragraph is spread across multiple lines." -->

Notice the second example: all three lines of text render on one line with single spaces between the words. The line breaks in the source became spaces in the output.

🔵 Analogy: whitespace collapsing

Imagine your editor autocorrects "too many spaces" to "too many spaces." HTML's whitespace collapsing is exactly that — it doesn't matter how you space your source, the browser tidies it to single spaces. This is actually a feature: it means you can indent your code for readability without affecting how it looks on screen.

Why Is This Useful?

Because of whitespace collapsing, you can freely indent your HTML for readability:

<!-- Both render identically -->

<ul><li>Item one</li><li>Item two</li></ul>

<ul>
  <li>Item one</li>
  <li>Item two</li>
</ul>

The second version is far easier to read and debug. Use indentation freely — it costs nothing on screen.

When Whitespace Does Matter

There are situations where whitespace between inline elements can create a small but visible gap. This is a known quirk:

<!-- These images will have a tiny gap between them due to the whitespace -->
<img src="a.png" alt="A">
<img src="b.png" alt="B">

<!-- No gap: they're on the same line with no whitespace between them -->
<img src="a.png" alt="A"><img src="b.png" alt="B">

In practice, this is solved with CSS (using display: flex on the parent container) rather than squishing tags together, which is unreadable. But it's worth knowing the gap exists and why.

Forcing a Line Break: <br>

If you want to force text onto a new line without starting a new paragraph, use the <br> element (line break). It's a void element — no closing tag:

<p>
  221B Baker Street<br>
  Marylebone<br>
  London<br>
  NW1 6XE
</p>

This renders each line of the address on its own line. Use <br> sparingly — if you need many line breaks to create spacing, CSS margin and padding are almost always the right tool instead.

Non-Breaking Spaces: &nbsp;

Normally, a browser can break a line between any two words. The non-breaking space entity &nbsp; creates a space that the browser will never break a line across:

<p>The year was&nbsp;2024.</p>
<p>€&nbsp;49.99</p>
<p>Mr.&nbsp;Smith</p>

Use this when breaking between two words would look awkward — like a currency symbol and a number, or a title and a name.

📌 Don't use &nbsp; for spacing

A very common beginner mistake is using multiple &nbsp; entities to add visual spacing between elements: &nbsp;&nbsp;&nbsp;&nbsp;. This is considered bad practice. It's fragile, inaccessible to screen readers, and creates maintenance problems. Use CSS margin and padding to control spacing between elements.

The <pre> Element: Preserve Whitespace

The <pre> (preformatted text) element is the exception to the whitespace collapsing rule. Inside <pre>, spaces, tabs, and line breaks are preserved and displayed exactly as written:

<pre>
  Name:    Jane Smith
  Role:    Developer
  Team:    Frontend
</pre>

The browser renders this with the alignment exactly as typed. <pre> is typically used for code samples, ASCII art, and any content where exact spacing matters. It also switches to a monospace font by default.

Best Practices for Comments and Whitespace

  • Comment to explain why, not what. The HTML itself already shows what — your comment should add context.
  • Use consistent indentation throughout your file. Pick 2 or 4 spaces and stick to it.
  • Add a blank line between major sections of HTML for visual breathing room.
  • Comment the closing tags of complex wrapper elements (</div> <!-- end .sidebar -->).
  • Remove commented-out code before shipping to production, or at least review it — stale commented code becomes confusing.
  • Never put sensitive information in comments.

Interview Questions

What is an HTML comment and how is it written?

A note in the source code that the browser ignores when rendering. Written as <!-- comment text -->. Visible only in the source code, not on the page.

What is whitespace collapsing?

HTML's behaviour of treating any sequence of whitespace characters (spaces, tabs, newlines) as a single space in the rendered output. This allows developers to use indentation and line breaks in source code for readability without affecting the visual layout.

When should you use <br> vs a new paragraph?

Use <br> for a line break within a single continuous piece of content (like lines of an address or a poem). Use separate <p> elements for distinct paragraphs of text. Never use multiple <br> tags to create spacing — use CSS margin instead.

✏️ Exercise

Take your existing HTML file from earlier lessons. Add at least three HTML comments: one at the top explaining what the file is, one before each major section (heading + content), and one at the bottom marking the end of the body. Then deliberately add extra spaces and line breaks between some elements in your source code. Reload the page and notice that nothing changes visually — the whitespace collapsed.

🧠 Quiz

1. Can visitors see HTML comments on the page?

Not in the rendered page, but they can see them by viewing the page source (Ctrl+U / Cmd+U). Comments are hidden from display, not encrypted or private.

2. What does the <pre> element do differently from a normal <p>?

<pre> preserves all whitespace (spaces, tabs, and line breaks) exactly as written in the source code, and uses a monospace font. Normal elements collapse whitespace to a single space.

3. What keyboard shortcut in VS Code toggles a comment on the current line?

Ctrl + / on Windows/Linux, Cmd + / on Mac.

🚀 Mini Challenge

Write an HTML page that displays a poem. Use <h1> for the title, a <p> with <br> tags between each line of the poem, and a <pre> version of the same poem below it. Notice the difference in how they render. Add comments explaining what each version demonstrates. Which approach looks better on screen and why?

Summary

  • HTML comments are written as <!-- text --> and are ignored by the browser but visible in source code.
  • Never put sensitive information in comments — they are public.
  • HTML collapses multiple spaces, tabs, and line breaks into a single space — this is whitespace collapsing.
  • <br> forces a line break; &nbsp; creates a non-breaking space.
  • <pre> preserves whitespace exactly as written and uses a monospace font.
  • Use CSS, not &nbsp;, to create spacing between elements.

Related Topics