Search code examples
csscomments

Why is the comment character sequence (/* */) in CSS important?


Why are comments in CSS even considered? I was testing a website out earlier and just put some plain text after a semicolon:

#myId {
   property: value; this is my comment
}

And no errors were raised, the value still applied to the selector, and everything worked out just fine. Why use /* */? I tested it out in Chrome and Firefox. Are there other browsers that might not parse the CSS if I do this that I am not aware of?


Solution

  • Your browser probably just dropped the "this is my comment". After the ";", it thought that "this is my comment" was a property, but didn't know what it was. But it will drop everything after it until it reaches the "}".

    Try something like:

    #myId {
      border: 1px solid red; this is my comment
      border: 2px solid blue;
    }
    

    The blue border won't be displayed, but it should.

    Then try:

    #myId {
      border: 1px solid red;
      border: 2px solid blue;
    }
    

    The blue border appears!

    So I would use the /* */ for comments ;P