Annotating Your Code Lesson 3 of 7

HTML comments let you leave notes in your source code that browsers ignore.

HTML 101   DESN 368

Why Comments Matter

  • You’ll write a piece of HTML once — you’ll read it dozens of times
  • Comments are notes to your future self and anyone who inherits your code
  • They’re invisible to users but visible in the page source
  • When you return to a project weeks later, good comments save significant debugging time

The Comment Syntax

  • Comments start with <!-- and end with -->
  • Everything between those delimiters is ignored by the browser
<!-- This is a single-line comment -->

<!--
  This is a multi-line comment.
  It can span as many lines as needed.
  Browsers skip all of it.
-->
  • Comments can appear anywhere in your HTML — inside <head>, inside <body>, between elements

What Comments Are For

Section labels — in a long file, comments act as headers so you can scan quickly:

<!-- ============================
     NAVIGATION
     ============================ -->
<nav>
  <a href="/">Home</a>
  <a href="/work">Work</a>
</nav>

<!-- ============================
     HERO SECTION
     ============================ -->
<section id="hero">
  <h1>Portfolio</h1>
</section>

<!-- ============================
     PROJECT GRID
     ============================ -->
<section id="projects">
  ...
</section>

Explaining non-obvious decisions — when something needs context:

<!-- Empty alt because this image is decorative, not content -->
<img src="divider-wave.svg" alt="">

<!-- tabindex="-1" removes this from keyboard tab order intentionally -->
<div class="modal" tabindex="-1">
  ...
</div>

Temporarily disabling code — hide a block without deleting it:

<!-- 
  <section id="promo-banner">
    <p>Summer sale — 30% off!</p>
  </section>
-->

What Comments Are Not For

  • Not private — anyone can view your page source; never put passwords, API keys, or sensitive notes in comments
  • Not essays — keep them short and useful
    • <!-- Navigation section -->
    • <!-- This is the navigation section which contains the main nav links for the website -->
  • Not a substitute for clear code — if your HTML is so confusing it needs comments everywhere, simplify the structure first

Designer Note

  • In collaborative projects, commenting your HTML is a form of documentation
  • A developer who inherits your code shouldn’t have to reverse-engineer its structure
  • Well-placed section labels are considered professional practice — they’re a sign you wrote the code for the next person, not just for yourself