Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Nested Comments

Tags:

comments

css

Is there any way to nest comments in CSS?

For example, when I try to comment out the following two statements, the outer comment ends when it encounters the */ in the nested comment, leaving the rest of the first statement and second statement uncommented.

/*     #container {         width: 90%;     /* nested comment here */         margin: 0 auto;     }      #container .class {         width: 25%;     } */ 

I run into this problem often when I want to try out a different styling technique that involves multiple statements.

I'm familiar with the CSS specification for comments and its rationale, but also know there's a workaround for nesting HTML comments and am hoping there's a similar hack for CSS.

Has anyone found a way to nest comments in CSS?

like image 405
cantera Avatar asked Dec 10 '11 21:12

cantera


People also ask

Can you nest comments in CSS?

As with most programming languages that use the /* */ comment syntax, comments cannot be nested.

How do you comment multiple lines in CSS?

To add both inline and multiline comments in CSS, you start with a forward slash and asterisk ( /* ), and you end the comment it with an asterisk and forward slash ( */ ).

Can you nest comments in HTML?

Look for shortcuts in your editor to do nested comments in html. This can with one keypress (un)comment a block of code and do a substitution of <! -- inner-comment --> to <!~~ inner-comment ~~> (just like guido's answer suggests), making commenting and uncommenting blocks just as easy as it is in other languages.


1 Answers

CSS does not have a nestable comment syntax.

You could instead wrap the rules in something which does nest, but will not match anything, such as a non-existent media type:

@media DISABLED {     #container {         width: 90%;     /* nested comment here */         margin: 0 auto;     }      #container .class {         width: 25%;     } } 

This does have the caveat that it will not be obviously a comment (so it will not be removed by tools that remove comments), and will cause the browser to spend time on parsing it and finding that it doesn't match. However, neither of these should be a problem for temporary development use, which is the usual reason one would want to comment out a large chunk simply.

like image 127
Kevin Reid Avatar answered Oct 07 '22 05:10

Kevin Reid