HTML Lesson 8 – Comments and White-space | Dataplexa
LESSON 8

Comments and Whitespace

Master hidden notes in your HTML and control spacing for cleaner, more readable code.

Comments are like sticky notes for your HTML. They're completely invisible to visitors but help you remember why you wrote something. Think of them as secret messages that only you and other developers can see when looking at the code. Whitespace means all the empty space in your code — the spaces between words, blank lines, and tabs. HTML treats whitespace in a special way that might surprise you.

HTML Comments

Comments start with ``. Everything between these markers becomes invisible to the browser. You can write anything inside a comment — notes about what the code does, reminders for later, or temporarily hide code without deleting it.
<!--
Your comment text here
-->
Opening tag (orange) + content (gray) + closing tag (orange)
Alex wants to add some helpful notes to their portfolio code so they remember what each section does:
<!-- Navigation section for Alex's portfolio -->
<nav>
  <a href="index.html">Home</a>
  <a href="projects.html">Projects</a>
  <a href="contact.html">Contact</a>
</nav>

<!-- Main content area -->
<main>
  <h1>Welcome to My Portfolio</h1>
  <p>I'm a web developer passionate about creating amazing websites.</p>
  
  <!-- TODO: Add more about me content later -->
</main>
localhost/index.html
What just happened?
All the comments disappeared completely. The navigation and heading rendered normally, but visitors never see the notes about "Navigation section" or "TODO" reminders. Comments are your invisible helpers.

Common Comment Uses

Professional developers use comments for several important reasons. They mark different sections of a page, explain complex code, leave reminders for future updates, and temporarily disable code without deleting it.
Section Markers
Mark the start and end of major page sections like header, main content, and footer.
Code Explanations
Explain why you chose certain HTML structures or what they accomplish.
TODO Reminders
Note features to add later, content to update, or improvements to make.
Temporary Hiding
Hide code temporarily while testing instead of deleting it completely.
Here's how Alex might comment different parts of their portfolio:
<!-- ========== HEADER SECTION ========== -->
<header>
  <h1>Alex Thompson</h1>
  <p>Web Developer & Designer</p>
</header>

<!-- ========== MAIN CONTENT ========== -->
<main>
  <!-- About me section -->
  <section>
    <h2>About Me</h2>
    <p>I love creating websites that make people smile.</p>
    <!-- TODO: Add photo of me here -->
  </section>
  
  <!-- Temporarily hiding skills section until content is ready
  <section>
    <h2>My Skills</h2>
    <p>HTML, CSS, JavaScript</p>
  </section>
  -->
</main>
localhost/index.html
What just happened?
Only the header and "About Me" section appeared. The skills section got completely hidden because it was wrapped in comment tags. Notice how the `` reminder also disappeared.

Understanding Whitespace

Whitespace includes spaces, tabs, and line breaks in your HTML code. Browsers handle whitespace in a way that often surprises beginners — they collapse multiple spaces into just one space, and they ignore most line breaks completely. This means you can format your code however makes it easiest to read. The browser will display the content the same way regardless of your spacing choices.
<!-- All of these create the EXACT same result -->

<!-- Version 1: Everything on one line -->
<p>Hello     world    this    has    lots    of    spaces</p>

<!-- Version 2: Split across multiple lines -->
<p>Hello     world    
   this    has    
   lots    of    spaces</p>

<!-- Version 3: Nicely formatted -->
<p>
  Hello     world    
  this    has    
  lots    of    spaces
</p>
localhost/index.html
What just happened?
All three paragraphs look identical! The browser collapsed all those extra spaces into single spaces and ignored the line breaks. Each paragraph shows "Hello world this has lots of spaces" with normal spacing.

Whitespace Collapse Rules

The browser follows specific rules when handling whitespace. Multiple consecutive spaces become one space. Line breaks become spaces (or disappear entirely). Leading and trailing spaces get removed. Tabs get treated like regular spaces. Here's exactly how Alex's portfolio content would render with different whitespace:
<!-- Testing whitespace in Alex's portfolio -->
<h1>Alex        Thompson</h1>

<p>I'm    a    web    developer    who    loves
   creating    beautiful    websites    that    
   make    people    happy.</p>

<p>My favorite technologies:
   HTML
   CSS  
   JavaScript</p>
localhost/index.html
What just happened?
The heading shows "Alex Thompson" with normal spacing. The paragraph text flowed together into regular sentences. The technology list became one line: "My favorite technologies: HTML CSS JavaScript" because line breaks turned into spaces.

Clean Code Formatting

Good formatting makes your HTML easier to read and maintain. You should use consistent indentation (usually 2 spaces per level), add blank lines between major sections, align related elements, and keep lines reasonably short. Professional developers format their code like this because it makes mistakes easier to spot and helps team members understand the structure quickly.
Messy Formatting
<header><h1>Alex</h1><p>Developer</p></header><main><section><h2>About</h2><p>I make websites</p></section></main>
Clean Formatting
<header>
  <h1>Alex</h1>
  <p>Developer</p>
</header>

<main>
  <section>
    <h2>About</h2>
    <p>I make websites</p>
  </section>
</main>
Here's how Alex should format their complete portfolio page:
<!-- Alex's Portfolio - Clean Formatting Example -->
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Page information -->
  <meta charset="UTF-8">
  <title>Alex Thompson - Web Developer</title>
</head>

<body>
  <!-- ========== HEADER SECTION ========== -->
  <header>
    <h1>Alex Thompson</h1>
    <p>Creative Web Developer</p>
  </header>
  
  <!-- ========== MAIN CONTENT ========== -->
  <main>
    <section>
      <h2>Welcome</h2>
      <p>Thanks for visiting my portfolio. I create websites that users love.</p>
    </section>
  </main>
  
  <!-- ========== FOOTER SECTION ========== -->
  <footer>
    <p>© 2024 Alex Thompson</p>
  </footer>
</body>
</html>
localhost/index.html
What just happened?
The page rendered perfectly with clear header, main content, and footer sections. All the clean formatting and helpful comments disappeared in the browser but made the code much easier to understand for developers.

Common Mistakes

New developers often make specific mistakes with comments and whitespace. Here are the most common ones and how to fix them.
Forgetting Comment Closing
Writing `` breaks your entire page. Always close comments properly.
<!-- BROKEN: Missing closing -->
<!-- This comment never ends
<h1>This heading won't appear</h1>
<p>Neither will this paragraph</p>

<!-- FIXED: Properly closed -->
<!-- This comment ends correctly -->
<h1>This heading appears perfectly</h1>
<p>And so does this paragraph</p>
localhost/index.html
Most code editors highlight unclosed comments in different colors, making this mistake easy to spot. VS Code shows unclosed comments in gray that extends beyond where it should end.

Pro Developer Tips

Professional developers follow specific patterns that make their HTML bulletproof. Always use consistent indentation (2 spaces is standard). Add comments before major sections, not after. Write TODO comments in ALL CAPS so they're easy to find. Remove commented-out code before going live. Most importantly, format your code like someone else will need to read it — because they will! Future you will thank present you for clear, well-commented HTML. Companies like Apple and GitHub have strict formatting standards that all their developers follow. Clean, consistent code prevents bugs and makes collaboration smooth.

Quiz

What appears between the opening `<!--` and closing `-->` of an HTML comment when viewed in the browser?

How would this HTML display in the browser: `<p>Hello world</p>`?

Alex wants to remember to add a contact form to their portfolio later. What's the best way to use a comment for this?

Up Next: Links and Navigation
Connect your pages together and build navigation menus that help users explore your website.