Single-Line Comment in HTML
This article will explore different ways of adding comments to an HTML file.
Comments in HTML
There are different languages used in programming, including C, C++, C#, Python, HTML, Java, etc. Every programming language has a different syntax for writing comments.
Comments are statements that are always ignored by the compiler. These statements don’t get executed.
Comments are used for a better understanding of the source code. There are two benefits of writing comments which are as follows:
- Comments help us to understand our code even after years of writing it.
- Comments help us to understand the code written by other programmers.
Any text inserted inside the comments cannot be displayed on the browser.
Using comments, we can explain the working of every program section. Comments are also used to hide any part of the source code.
Steps to Write a Program in HTML
By following these steps, we can write a program in HTML.
-
First, you must have Notepad or Notepad++ to write the HTML source code.
-
Start writing the code in HTML.
-
Save the file by clicking on
Save As
. -
Save the file with the extension of
.html
. -
After saving the file, the default browser icon will appear on the file.
-
After clicking on the file, the browser will render the HTML.
Write Single-Line Comments in HTML
This code snippet shows the use of comments in HTML language.
In HTML, there are two types of comments: single-line and multi-line comments. Here, we will see how to write a single-line comment.
A single-line comment begins with <!--
and ends with -->
.
<html>
<body>
<h1> SINGLE LINE COMMENT </h1>
<!-- Single line comment -->
<!--
Hello -->
<!--
hey -- -- OK-->
<!---->
<!------ Welcome
-->
<!------>
name-->
<!>
</body>
</html>
In the above code, the first tag is of HTML, a paired tag. Then the second tag is of a body
of HTML.
In the body
tag, there is another paired tag of heading
. Then under the heading
tag, there is a single-line comment starting with <!--
and ending with -->
.
Another comment of Hello
is also valid because it has an accurate starting and ending. Then the comment hey
and OK
is also valid because whatever is written inside the comments tag will be considered part of the comments.
The comment <!---->
is also valid because it is an empty comment in HTML. If this occurs in a multiple of four, the comment is valid in HTML.
The last welcome
comment is also valid because it has an accurate starting and ending. Then the comment <!------> name-->
shows name-->
on the browser, and the first half is considered a valid comment.
The last one, <!>
, is invalid because it doesn’t have the correct starting and ending of the comment in HTML.