LESS CSS - 无法划分两个像素单位并返回无单位值

发布于 2025-01-07 17:59:03 字数 422 浏览 2 评论 0原文

我想(暂时)删除变量 @baseLineHeight@baseFontSize 的单位,以便我可以将它们除以得到相对行高。这是我尝试过的:

@baseFontSize: 12px;
@baseLineHeight: 18px;
font: @baseFontSize~"/"@baseLineHeight/@baseFontSize sans-serif;

生成以下错误:

Object #<Object> has no method 'toCSS' (Less::ParseError)

首选输出:

font: 12px/1.5 sans-serif;

I'd like to (temporarily) remove the units of my variables @baseLineHeight and @baseFontSize, so that I can divide them to get a relative line-height. This is what I've tried:

@baseFontSize: 12px;
@baseLineHeight: 18px;
font: @baseFontSize~"/"@baseLineHeight/@baseFontSize sans-serif;

Generates the following error:

Object #<Object> has no method 'toCSS' (Less::ParseError)

Preferred output:

font: 12px/1.5 sans-serif;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2025-01-14 17:59:03

同时似乎有一个函数可以做到这一点:
http://lesscss.org/functions/#misc-functions-unit

这里评论中的代码,只是为了完整性。 (感谢 cfx)。

font: @baseFontSize~"/"unit(@baseLineHeight/@baseFontSize) sans-serif;

Meanwhile there seems to be a function for that:
http://lesscss.org/functions/#misc-functions-unit

Here the code from the comment, just for completeness. (thanks to cfx).

font: @baseFontSize~"/"unit(@baseLineHeight/@baseFontSize) sans-serif;
坠似风落 2025-01-14 17:59:03

我错过了文档中有关 JavaScript 评估的部分。这似乎解决了我的问题:

font: @baseFontSize~"/"`parseInt("@{baseLineHeight}") / parseInt("@{baseFontSize}")` sans-serif;

I missed the part of the documentation regarding JavaScript evaluation. This seems to solve my problem:

font: @baseFontSize~"/"`parseInt("@{baseLineHeight}") / parseInt("@{baseFontSize}")` sans-serif;
我也只是我 2025-01-14 17:59:03

其他答案似乎并没有实际作用。

根据 LESS 文档unit() 函数 将删除或更改维度的单位。由于该函数仅接受单个维度作为参数(以及可选参数),因此您将使用以下内容:

unit((@baseLineHeight/@baseFontSize))

因为 严格数学,您会注意到上面的行可能需要用括号括起来,以便实际计算数学。

@baseFontSize: 12px;
@baseLineHeight: 18px;

p {
  font: @baseFontSize ~"/" unit((@baseLineHeight/@baseFontSize)) sans-serif;
}

上面的 LESS 将输出以下所需的结果:

p {
  font: 12px / 1.5 sans-serif;
}

The other answers don't seem to actually work.

According to the LESS documentation, the unit() function will remove or change the unit of a dimension. Since the function only accepts a single dimension as a parameter (and an optional parameter), you would use the following:

unit((@baseLineHeight/@baseFontSize))

Because of strict math, you will notice that the line above may need to be wrapped in parenthesis so that the math is actually evaluated.

@baseFontSize: 12px;
@baseLineHeight: 18px;

p {
  font: @baseFontSize ~"/" unit((@baseLineHeight/@baseFontSize)) sans-serif;
}

The LESS above will output the following, desired results:

p {
  font: 12px / 1.5 sans-serif;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文