less莫名的语法报错?

less莫名的语法报错,不知是何原因?
image.png

// 居中
.center(@width:null,@height:null) {
  position: absolute;
  top: 50%;
  left: 50%;
  & when (@width = null) and (@height= null){
    transform: translate(-50%, -50%);
  }
  & when not (@width = null) and (@height = null){
    width: @width;
    margin-left: -(@width / 2);
    transform: translateY(-50%);
  }
  & when (@width = null) and not(@height=null){
    height: @height;
    margin-top: -(@height / 2);
    transform: translateX(-50%);
  }
  & when not(@width = null) and not(@height = null){
    width: @width;
    height: @height;
    margin: -(@height / 2) 0 0 -(@width / 2);
  }
}
阅读 2.1k
2 个回答

用括号把 when 的条件包起来

// 居中
.center(@width: null, @height: null) {
    position: absolute;
    top: 50%;
    left: 50%;

    & when ((@width =null) and (@height=null)) {
        transform: translate(-50%, -50%);
    }

    & when (not (@width =null) and (@height =null)) {
        width: @width;
        margin-left: -(@width / 2);
        transform: translateY(-50%);
    }

    & when ((@width =null) and not(@height =null)) {
        height: @height;
        margin-top: -(@height / 2);
        transform: translateX(-50%);
    }

    & when (not(@width =null) and not(@height =null)) {
        width: @width;
        height: @height;
        margin: -(@height / 2) 0 0 -(@width / 2);
    }
}

先试一试编译能通过吗,或者把less文件关了再开,vscode有时确实会出现这样的问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进