In VS Code, when you use Ctrl + / to comment out code in certain languages like Python or JavaScript, it adds a line comment to each line by default instead of creating a block comment.
For example, in JavaScript:
// let i = 1
// function abc() {
// let i = 0;
// console.log(i);
// }
VS Code will create a block comment only when you use Shift + Alt + A,
/* function abc() {
let i = 0;
console.log(i);
}
*/
However, for HTML or CSS, things are different--no matter whether you use Ctrl + / or Shift + Alt + A, VS Code always adds a block comment, like this:
.banner {
/* display: block;
margin: 0 auto;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5); */
}
This isn't very convenient, especially in CSS, because we often need to comment and uncomment properties to quickly test different styles in the browser.
For example, if I want to see the effect of adding the background-color property, I have to first remove the block comment and then re-comment out the display, margin and border-radius properties:
If VS Code used line comments instead, I could simply uncomment the line with background-color:
.banner {
/* display: block; */
/* margin: 0 auto; */
/* border-radius: 50%; */
background-color: rgba(255, 255, 255, 0.5);
}
I don't know why VS Code changes its commenting behavior for HTML and CSS, but it's really inconvenient.
So, is there a way to change this?
I found this solution online: Use single-line comments for HTML & CSS.
However, it's not perfect.
For example, let's take JavaScript again:
let i = 1;
function abc() {
// let i = 0;
console.log(i);
}
Here, one line is already commented.
If I now comment out the entire block using Ctrl + /, I'll get:
// let i = 1
// function abc() {
// // let i = 0;
// console.log(i);
// }
As you can see, the line let i = 0; gets double-commented, which is just I want.
But with the method provided in the link above, if you already have commented code in CSS like this:
.banner {
display: block;
/* margin: 0 auto;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5); */
}
and then press Ctrl + /, it actually uncomments the already-commented lines instead:
.banner {
/* display: block; */
margin: 0 auto;
/* border-radius: 50%; */
/* background-color: rgba(255, 255, 255, 0.5); */
}