Convert px to em in Less

后端 未结 7 1598
独厮守ぢ
独厮守ぢ 2021-01-01 07:34

What is the equivalent of Scss\' emCalc() in less?

padding: emCalc(24px);

in Scss will calculate em based on the viewpoint and

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 07:50

    LESS doesn't have such a feature, according to documentation.

    Built-in unit or convert functions do not provide such a convertion.

    Note that Scss' implementation of this function assumes convertion using one global font-size value. You can easily achieve same thing in LESS with the use of variables:

    @em: 16px;    // LESS variable - default value for 1em
    

    And then use it like this:

    div {
      height: @em;
      width: 6 * @em;
    }
    

    Code above compiles to:

    div {
      height: 16px;
      width: 96px;
    }
    

    Note that this (and Scss' version, too) is not the way that real em work, because in CSS dimensions specified in em are computed based on font size of element on which they are used.

提交回复
热议问题