2

Situation

I recently decided to put comments in my CSS files. And once I did so, one of them stopped working.

Both ways of making comments make my entire CSS file not to work. I know this is lacking informations, I just don't know where it could even possibly come from.

In case it does matter, this is how I write my CSS:

// Background
body            { background-color: #666666; }
#content            { background-color: #cccccc; }
#menu           { background-color: #cccccc; }
#menu-footer        { background-color: #33cccc; }
#menu-items a.active    { background-color: #33cccc; }
#menu-items a:hover     { background-color: #99cccc; }

// The white spaces are actually tabs (Alt+i on Emacs)

Update 1

I am looking for ways to debug this situation. I see my CSS files in the developer tool from Google Chrome, but properties are not applied.

foobar {
    // color: cyan;
}

Does this simply make the CSS wrong but only on the one line ? So the rest of the file keep getting parsed ?


Update 2

I always used // to comment my CSS but with the later notation I used in this post. Now that I changed my mind and am using inline CSS, // being an invalid token make the whole file not readable.

1
  • Same story. Formally learning js html and css and starting out with // in css with some interesting results that could not be explained :} Commented Jan 11, 2024 at 21:34

4 Answers 4

4

css does not recognize the double slash as a comment. You have to use the /* */ one.

I might be wrong, but since the double slash is not a valid css token the behaviour might be browser dependent. I would expect browsers to simply ignore the property or the statement that follows the //, but I never checked/tested.

There are rules on what browsers should do in various situations, however I did not see any for unknown token (maybe a I didn't look well enough).

Sign up to request clarification or add additional context in comments.

Comments

2

Use */ Text */ , instead of // (// is comment in javascript)

/* Comment */

For Example

/**** Background ****/
    body            { background-color: #666666; }
    #content            { background-color: #cccccc; }
    #menu           { background-color: #cccccc; }
    #menu-footer        { background-color: #33cccc; }
    #menu-items a.active    { background-color: #33cccc; }
    #menu-items a:hover     { background-color: #99cccc; }

/* The white spaces are actually tabs (Alt+i on Emacs) */

Comments

1

Try the comments this way

/* Background */
body            { background-color: #666666; }
#content            { background-color: #cccccc; }
#menu           { background-color: #cccccc; }
#menu-footer        { background-color: #33cccc; }
#menu-items a.active    { background-color: #33cccc; }
#menu-items a:hover     { background-color: #99cccc; }

/* The white spaces are actually tabs (Alt+i on Emacs) */

Comments

1

The only way to comment in CSS is by /* this is a comnent */

/*This is a comment*/
p
{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.