DEV Community

Marvellous
Marvellous

Posted on

Comments in CSS HTML & CSS Comments

Writing lots of lines of code and you're worried you might not understand your code next week?

Want to make your project understandable to other programmers?

Comments are the way to go about it. A comment is a programmer-readable explanation or annotation in the source code of a computer program.

Comments are added to make the source code easier for humans to understand and are generally ignored by compilers and interpreters.

Comments are started with a language-specific symbol

HTML5

<!--This is a comment -->
Enter fullscreen mode Exit fullscreen mode

CSS

/* This is a comment */
Enter fullscreen mode Exit fullscreen mode

JavaScript

//This is a comment
Enter fullscreen mode Exit fullscreen mode

When to use comments

"A handy rule of thumb is: a comment should only be added to answer a question that the code can't. If you aren't confused by what a piece of code is doing, but rather why it's doing it at that moment, then a comment should be added." - Mark Cronin

Comments can be placed wherever white space is allowed within a style sheet. They can be used on a single line, or traverse multiple lines. This allows commenting inside a block of code and between a group of codes. What matters is making your code glanceable.

/* A one-line comment */

/*
A comment
which stretches
over several
lines
*/
Enter fullscreen mode Exit fullscreen mode

Note

The /* */ comment syntax is used for both single and multiline comments. There is no other way to specify comments in external style sheets.

Top comments (2)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Javascript also supports multiline comments with same syntax:


/*
javascript
multiline
comment
*/

and it can be used to comment a part of a line like this:


doSomething(  foo,  /* foo 2 */ );

Collapse
 
mariasavag90503 profile image
MariaSavage

All the aspects of the installation of the new turf have been authenticated for thk citizens. The surface of the floor installation for all offers. Path is met for the chance. Gale is formed for the mid of the targeted element for the things in this ambit for the objective in this field for flooring.