如何计算 LESS CSS 中的百分比?

发布于 2024-12-25 11:12:33 字数 482 浏览 4 评论 0原文

我想根据 LESS CSS 的父容器计算子容器(div 等)的宽度(以百分比表示)。

我正在使用 Ethan Marcotte 的论坛:目标/上下文=结果

父容器:620px
子容器:140px

我正在使用这个计算:

div.child-container {
    width: (140/620)*100%;
}

但是输出是:

div.child-container {
    width: 0.2258064516129;
}

我想将小数点移动两位数并添加%,如下所示:

div.child-container {
    width: 22.58064516129%;
}

非常感谢任何提示。

I would like to calculate the width of child-container (div etc) in percentages depending on the parent container with LESS CSS.

I am using the forumula by Ethan Marcotte: target / context = result.

Parent container: 620px

Child container: 140px

I am using this calculation:

div.child-container {
    width: (140/620)*100%;
}

However the output is:

div.child-container {
    width: 0.2258064516129;
}

I would like to move the decimal point two digits and add the %, like this:

div.child-container {
    width: 22.58064516129%;
}

Any hints greatly appreciated.

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

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

发布评论

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

评论(3

一萌ing 2025-01-01 11:12:33

根据 LESS CSS 网站,您需要更改方程的顺序

输出几乎与您所期望的一样 - LESS 理解颜色和单位之间的差异。如果在操作中使用单位,例如:

@var: 1px + 5;

LESS 将使用该单位作为最终输出 - 在本例中为 6px

应该是:

width: 100%*(140/620);

According to the LESS CSS website, you need to change the order of your equation

The output is pretty much what you expect—LESS understands the difference between colors and units. If a unit is used in an operation, like in:

@var: 1px + 5;

LESS will use that unit for the final output—6px in this case.

It should be:

width: 100%*(140/620);
萌辣 2025-01-01 11:12:33

也许当OP询问时,percentage函数不存在,但供将来参考我添加这个答案。

div.child-container {
    width: percentage(140/620);
}

Maybe the percentage function didn't exist when OP was asking but for future reference I add this answer.

div.child-container {
    width: percentage(140/620);
}
凑诗 2025-01-01 11:12:33

对于2022年,LESS 4.0和除法运算关注的是:

从 4.0 开始,不会使用 / 运算符在括号之外执行除法。

<前><代码>@color: #222 / 2; // 结果是 `#222 / 2`,而不是 #111 (!)
背景颜色:(#FFFFFF / 16); //结果是#101010

请参阅文档

As for 2022, LESS 4.0 and division operator concerned:

From 4.0, No division is performed outside of parens using / operator.

@color: #222 / 2; // results in `#222 / 2`, not #111 (!)
background-color: (#FFFFFF / 16); //results is #101010

See the docs.

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