HTML · Comments, Whitespace & Special Characters · Lesson 5 of 32

Comments, Whitespace & Special Characters

Comments, Whitespace & Special Characters

HTML provides several features that help developers write readable, maintainable and correctly displayed web pages. Three important concepts are Comments, Whitespace and Special Characters.

Quick Overview
  • Comments are notes written inside HTML code that are not displayed on the webpage.
  • Whitespace includes spaces, tabs and line breaks used to improve code readability.
  • Special Characters are characters that may need HTML entities to be displayed correctly.

1. HTML Comments

An HTML comment is a section of code written for developers or programmers that is ignored by the browser when rendering the webpage.

Comments are useful for explaining code, documenting sections, temporarily disabling code and helping developers understand the structure of a webpage.

Syntax of an HTML Comment

<!-- This is an HTML comment -->

Everything between <!-- and --> is treated as a comment.

Example

<h1>Welcome to HTML</h1>

<!-- This paragraph introduces the website -->

<p>HTML is used to create webpages.</p>

The browser displays the heading and paragraph, but the comment itself is not displayed on the webpage.

Comments Can Span Multiple Lines

An HTML comment can contain more than one line.

<!--
    This section contains
    information about HTML
    elements and attributes.
-->

Using Comments to Temporarily Disable Code

Developers can temporarily hide an HTML element by placing it inside a comment.

<h1>Main Heading</h1>

<!--
<p>This paragraph is temporarily disabled.</p>
-->

<p>This paragraph is visible.</p>

The commented paragraph will not appear on the webpage.

Important:

HTML comments are not a security mechanism. Comments can still be viewed by users who inspect the page source. Therefore, never place passwords, API keys, confidential information or sensitive data inside HTML comments.

2. Uses of HTML Comments

Comments are mainly used to make HTML code easier to understand and maintain.

Use Purpose Example
Documentation Explain what a section of code does. <!-- Navigation Menu -->
Code Organisation Divide a large HTML document into logical sections. <!-- Header Section -->
Debugging Temporarily remove an element from the rendered page. Commenting out a paragraph.
Team Collaboration Help other developers understand the code. Explaining a complex section.

3. Whitespace in HTML

Whitespace refers to spaces, tabs and line breaks present in HTML source code.

Developers use whitespace mainly to improve the readability and organisation of HTML code.

Example

Consider the following HTML:

<p>HTML is easy to learn.</p>

It can also be written with extra spaces and line breaks:

<p>

    HTML

    is

    easy

    to

    learn.

</p>

In normal HTML rendering, browsers generally collapse consecutive whitespace characters into a single visible space.

Example of Collapsed Whitespace

<p>
    HTML       is
    easy       to
    learn.
</p>

The browser normally renders this approximately as:

HTML is easy to learn.

Therefore, adding multiple spaces in ordinary HTML does not normally create multiple visible spaces on the webpage.

4. Why Whitespace Matters

Although browsers usually ignore extra whitespace during normal HTML rendering, developers should use whitespace carefully because it makes source code easier to read.

Poorly Formatted Code

<div><h1>HTML Course</h1><p>Learn HTML from beginner to advanced.</p></div>

Well-Formatted Code

<div>

    <h1>HTML Course</h1>

    <p>
        Learn HTML from beginner to advanced.
    </p>

</div>

Both versions can produce the same visible content, but the second version is much easier for a developer to read and maintain.

Best Practice:

Use indentation, line breaks and consistent formatting to make HTML source code readable.

5. How to Preserve Whitespace

Sometimes the exact spaces and line breaks are important. HTML provides ways to preserve or represent whitespace explicitly.

The <pre> Element

The <pre> element displays preformatted text. Spaces and line breaks inside it are generally preserved.

<pre>
HTML
    CSS
        JavaScript
</pre>

The browser preserves the formatting inside the <pre> element.

Example: Displaying Code

<pre>
<h1>Hello World</h1>
<p>Welcome to HTML.</p>
</pre>

The <pre> element is commonly useful for displaying code, poems, ASCII diagrams and other preformatted text.

6. Special Characters in HTML

Some characters have a special meaning in HTML syntax. For example, the less-than symbol < is used to begin an HTML tag.

If we want to display such characters as ordinary text, we can use HTML character entities.

What is an HTML Entity?

An HTML entity is a special notation used to represent a character that may have a special meaning in HTML or may not be conveniently entered directly.

Entities generally begin with & and end with ;.

&entity-name;

Numeric character references can also be used.

&#number;

7. Common HTML Special Characters and Entities

Character Named Entity Numeric Reference Common Use
< &lt; &#60; Less-than symbol
> &gt; &#62; Greater-than symbol
& &amp; &#38; Ampersand
" &quot; &#34; Double quotation mark
' &apos; &#39; Apostrophe / single quotation mark
  &nbsp;   Non-breaking space
© &copy; &#169; Copyright symbol
® &reg; &#174; Registered trademark
&trade; &#8482; Trademark symbol

8. Displaying < and >

The symbols < and > have special meaning in HTML because they are used to define tags.

To display them as ordinary text, use:

&lt;
&gt;

Example

<p>
    Use &lt;h1&gt; to create a level-one heading.
</p>

The browser displays:

Use <h1> to create a level-one heading.

Comparison Example

<p>5 &lt; 10</p>
<p>20 &gt; 15</p>

Output:

5 < 10

20 > 15

9. Displaying the Ampersand (&)

The ampersand symbol & is used to begin an HTML character entity.

To safely represent the ampersand character in HTML, use:

&amp;

Example

<p>Research &amp; Development</p>

Output:

Research & Development

10. Non-Breaking Space

The entity &nbsp; represents a non-breaking space.

Unlike an ordinary space, a non-breaking space prevents the browser from breaking the line at that position.

<p>New&nbsp;York</p>

The browser displays:

New York

The two words are treated as a single unit for line wrapping.

Best Practice:

Do not use repeated &nbsp; entities simply to create visual spacing or to position elements. Use CSS for layout and spacing.

11. Quotation Marks and Entities

HTML provides entities for quotation marks.

&quot;  →  "
&apos;  →  '

Example

<p>
    She said, &quot;HTML is easy to learn.&quot;
</p>

Output:

She said, "HTML is easy to learn."

12. Copyright, Registered and Trademark Symbols

HTML entities can also be used to display commonly used typographical and legal symbols.

&copy;   → ©
&reg;    → ®
&trade;  → ™

Example

<p>CodeStep Academy&copy;</p>
<p>Example Brand&trade;</p>
<p>Example Product&reg;</p>

13. Unicode Characters

HTML pages can display a very large range of characters through Unicode, including characters from different writing systems, mathematical symbols and emoji.

A numeric character reference can be used when required.

&#128512;

This represents the Unicode character associated with the decimal code point.

Hexadecimal Character Reference

Unicode characters can also be represented using hexadecimal notation.

&#x1F600;

Both decimal and hexadecimal numeric character references can be useful when a character is difficult to type directly.

14. Displaying HTML Code on a Webpage

When teaching or documenting HTML, you often need to display HTML code as text instead of allowing the browser to interpret it as actual markup.

For example, suppose you want the browser to display:

<h1>Hello World</h1>

The angle brackets must be represented using entities.

<p>
    &lt;h1&gt;Hello World&lt;/h1&gt;
</p>

This technique is especially important when creating programming tutorials, documentation and educational websites.

15. Comments vs Visible Text

Feature HTML Comment Normal HTML Content
Displayed on webpage No Yes
Used by browser for rendering No Yes
Purpose Documentation or temporarily disabling code Presenting webpage content
Visible in page source Yes Yes

16. Complete Example

<!-- Main heading -->

<h1>HTML &amp; Web Development</h1>

<p>
    Learn HTML from &lt;beginner&gt; to &lt;advanced&gt; level.
</p>

<!--
    The following paragraph contains
    additional information.
-->

<p>
    Copyright &copy; 2026 Example Academy.
</p>

What Happens?

  • The first comment is ignored by the browser.
  • &amp; displays the ampersand character.
  • &lt; displays the less-than symbol.
  • &gt; displays the greater-than symbol.
  • The second comment is also ignored during rendering.
  • &copy; displays the copyright symbol.

Common Beginner Mistakes

  • Forgetting to close an HTML comment with -->.
  • Using comments to store passwords, API keys or confidential information.
  • Assuming that pressing multiple spaces in HTML creates multiple visible spaces.
  • Using repeated &nbsp; entities for page layout instead of using CSS.
  • Writing < directly when the intention is to display it as ordinary text.
  • Forgetting that & begins an entity reference.
  • Confusing HTML comments with CSS comments.
  • Using comments excessively and making the source code unnecessarily difficult to read.

HTML Comments vs CSS Comments

Different technologies use different comment syntax.

Technology Comment Syntax Example
HTML <!-- ... --> <!-- Header -->
CSS /* ... */ /* Header styles */
JavaScript // ... or /* ... */ // Calculate total

Best Practices

  • Write comments that explain why something is being done when the purpose is not obvious.
  • Keep comments concise and relevant.
  • Use comments to divide large HTML documents into logical sections.
  • Never store sensitive information in comments.
  • Use indentation and whitespace consistently.
  • Use HTML entities when a character has special meaning in HTML syntax.
  • Use CSS rather than repeated whitespace or &nbsp; entities for page layout.
  • Use <pre> when exact whitespace and line breaks need to be preserved.

Practical Activity

Create an HTML page that demonstrates comments, whitespace and special characters.

Requirements

  1. Add a comment before the main heading.
  2. Create a paragraph containing an ampersand.
  3. Display the symbols < and > as text.
  4. Display the copyright symbol.
  5. Use a non-breaking space between two words.
  6. Use a <pre> element to display formatted text.

Challenge

Display the following HTML code as visible text on the webpage:

<p>HTML &amp; CSS</p>

Exam Questions

Question 1: What is an HTML comment?

Click to View Answer

An HTML comment is text written inside <!-- and --> that is ignored by the browser and is not displayed as webpage content.

Question 2: Write the syntax of an HTML comment.

Click to View Answer
<!-- Comment text -->

Question 3: What is whitespace in HTML?

Click to View Answer

Whitespace refers to spaces, tabs and line breaks present in HTML source code. It is mainly used to improve code readability and formatting.

Question 4: Why does HTML use character entities?

Click to View Answer

Character entities are used to represent characters that have special meaning in HTML or characters that may need a special representation.

Question 5: Which entity is used to display the less-than symbol?

Click to View Answer

&lt;

Question 6: Which entity is used to display an ampersand?

Click to View Answer

&amp;

Question 7: What is the purpose of &nbsp;?

Click to View Answer

&nbsp; represents a non-breaking space. It prevents the browser from breaking a line at that space.

Question 8: Which HTML element is commonly used to preserve whitespace and line breaks?

Click to View Answer

The <pre> element.

Question 9: True or False: HTML comments are completely invisible to users and cannot be viewed.

Click to View Answer

False. Comments are not displayed as webpage content, but users can generally view them by inspecting the page source.

Question 10: What is the difference between &lt; and <?

Click to View Answer

&lt; is the HTML entity used to display the less-than character. The character < itself has a special role in HTML markup.

Interview Questions

1. Are HTML comments displayed on a webpage?

No. The browser does not render HTML comments as visible webpage content.

2. Can a user see HTML comments?

Yes. Although comments are not displayed on the page, they can generally be viewed through the page source or browser developer tools.

3. Why should sensitive information not be placed in comments?

Because comments are included in the HTML source sent to the browser and can therefore be accessed by users.

4. Does HTML preserve multiple spaces?

In normal HTML text, consecutive whitespace is generally collapsed into a single visible space. Special techniques such as <pre> can be used when exact whitespace must be preserved.

5. Why is &lt; used instead of < when displaying code?

Because < has special meaning in HTML. Using &lt; tells the browser to display the less-than character as text rather than interpreting it as the beginning of a tag.

6. What is the difference between &nbsp; and a normal space?

A normal space can be collapsed and can act as a line-breaking point. A non-breaking space keeps the connected words together when the browser wraps text.

7. Should &nbsp; be used for page layout?

Generally, no. CSS should be used for layout, alignment, margins and spacing. &nbsp; should be used when a non-breaking space is actually required.

Think Like a Web Developer

Problem

A developer wants to create a webpage that teaches students how to write HTML. The webpage must display the following code exactly as text:

<h1>Welcome to HTML</h1>

If the developer writes the HTML code directly, the browser interprets it as an actual heading rather than displaying the code.

Explain how the developer can display the HTML code as visible text.

Click to View Answer

The developer should replace the angle brackets with their corresponding HTML entities. The less-than symbol should be written as &lt; and the greater-than symbol as &gt;. The <pre> and <code> elements can also be used when presenting source code.

Quick Reference

Requirement HTML
HTML Comment <!-- Comment -->
Less-than &lt;
Greater-than &gt;
Ampersand &amp;
Double Quote &quot;
Single Quote &apos;
Non-breaking Space &nbsp;
Copyright &copy;
Registered Trademark &reg;
Trademark &trade;
Preserve Whitespace <pre>

Memory Trick

Comment → Explain

Whitespace → Readability

Entity → Display Special Character

&lt; → <

&gt; → >

&amp; → &

&nbsp; → Non-breaking Space

Exam Tips

  • Remember the exact syntax: <!-- ... -->.
  • Do not confuse HTML comments with CSS or JavaScript comments.
  • Remember that normal HTML whitespace is generally collapsed.
  • Remember &nbsp; as the non-breaking space entity.
  • Learn the four most important entities: &lt;, &gt;, &amp; and &quot;.
  • For code-display questions, remember that HTML entities prevent the browser from interpreting markup as actual tags.
  • For layout questions, mention CSS rather than using repeated spaces or &nbsp;.

Quick Revision

  • HTML Comments are written using <!-- ... -->.
  • Comments are not rendered as visible webpage content.
  • Comments can still be viewed in the HTML source.
  • Never store sensitive information inside HTML comments.
  • Whitespace includes spaces, tabs and line breaks.
  • Browsers generally collapse consecutive whitespace in ordinary HTML text.
  • <pre> can preserve whitespace and line breaks.
  • HTML entities represent special characters.
  • &lt; represents <.
  • &gt; represents >.
  • &amp; represents &.
  • &nbsp; represents a non-breaking space.
  • CSS should be used for webpage layout and spacing rather than excessive whitespace entities.

Final Checkpoint

Before moving to the next topic, make sure you can:

  • Write an HTML comment correctly.
  • Explain why comments are useful.
  • Explain how browsers handle normal whitespace.
  • Use <pre> to preserve formatting.
  • Explain what an HTML entity is.
  • Write the entities for <, >, & and a quotation mark.
  • Explain the purpose of &nbsp;.
  • Display HTML code as visible text on a webpage.
  • Distinguish between HTML, CSS and JavaScript comments.
  • Explain why sensitive information should never be stored in HTML comments.

Next Topic: Links & Navigation