Is it bad practice to comment out single lines of CSS with //?

前端 未结 11 1722
梦如初夏
梦如初夏 2020-12-01 02:45

I have recently started using // to \"comment\" out single lines of CSS code. I understand that I am not actually commenting out the line; I am just breaking it

11条回答
  •  Happy的楠姐
    2020-12-01 03:24

    I recently read this article which sheds a lot of light on single line commenting practice in CSS.

    CSS allows you to use // after a fashion. It's not quite a line comment, but a next construct comment. That is, whenever you use //, the next CSS construct - either declaration or block - will be "commented out".

    So in your code snippet list-style-type:none is the next CSS construct and it gets commented out.

    li {
        float:left;
        //list-style-type:none;
        text-indent:0px;
    }
    

    Similarly, in the below code snippet

    //@keyframes foo {
      from, to { width: 500px; }
      50% { width: 400px; }
    }
    @keyframes bar {
      from, to { height: 500px; }
      50% { height: 400px; }
    }
    

    the // will comment out the first @keyframes declaration.

    If you try to use // just for writing comments into your stylesheet, you have to be careful - raw text isn't a CSS construct, so it'll look past that and comment out the next construct in your page. So in the below snippet

    // Do some stuff.
    .foo { animation: bar 1s infinite; }
    

    This will comment out the .foo block.

    You can get more information via the linked article mentioned at the start.

提交回复
热议问题