By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Comments in HTML

Reference on HTML Comments. Covers their use in Web-design, and some background information.

Edited: 2014-01-04 19:31

Most languages include the ability to write comments in the code, this is also the case with HTML. Comments are intended for small reminders, or explanations of the code – especially useful in large HTML source files.

In HTML you write comments using <!-- as opening tag, and --> as closing tag where CSS is using /* and */ to create comments in the code.

Usually only the developer or designer needs to edit the HTML of a template, and once a template is finished, it will rarely be edited – that's where comments are useful. Few will be remember everything in their code, so it can be a good idea to leave small comments, explaining complex sections in the HTML.

Comments in HTML, XHTML and XML

The following is an example of how to write comments in HTML source code.

<!DOCTYPE html>
<html lang="en">
  <head>
   <!--This is a HTML comment-->
   <title>My first Website</title>
  </head>
  <body>
   <h1>My first Website.</h1>
   <p>This example shows how to write comments in HTML</p>
   <!--This is a HTML comment-->
  </body>
</html>

Comments in CSS

Since the HTML and CSS web standards are so thoroughly connected with each other, it is relevant to mention comments in CSS. In CSS, you begin writing a comment with the /* opening tag, and end the comment with the */ closing tag – this is also known as a block comment in CSS. Comments can also be used in embedded CSS.

<!DOCTYPE html>
<html lang="en">
  <head>
   <style type="text/css">
    /* A css commen in embedded CSS */
   </style>
  </head>
  <body>
  </body>
</html>