Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS 属性值计算 - calc【热度: 320】 #682

Open
yanlele opened this issue Apr 13, 2024 · 0 comments
Open

CSS 属性值计算 - calc【热度: 320】 #682

yanlele opened this issue Apr 13, 2024 · 0 comments
Labels
CSS CSS 相关问题 TOP100互联网 公司标签
Milestone

Comments

@yanlele
Copy link
Member

yanlele commented Apr 13, 2024

是的,CSS 支持计算值,这可以通过 calc() 函数实现。calc() 允许你进行数学运算,计算 CSS 属性值。这个功能非常有力,因为它可以混合使用不同的单位,并且可以用在几乎任何需要数值的地方。

以下是 calc() 函数的一些应用示例:

  1. 基本运算:可以执行加 (+)、减 (-)、乘 (*) 和除 (/) 四种基本运算。

    .element {
      width: calc(100% - 80px);
    }
  2. 混合单位calc() 函数可以混合使用像素、百分比、em、rem 等单位。

    .element {
      margin-top: calc(50vh - 50px);
    }
  3. 嵌套:你可以在 calc() 里面嵌套另一个 calc()

    .element {
      width: calc(100% - calc(50px + 2em));
    }
  4. 环境变量:结合 CSS 变量 (Custom Properties) 使用。

    :root {
      --main-padding: 10px;
    }
    
    .element {
      padding: calc(var(--main-padding) * 2);
    }
  5. 动态值调整:用于某些动态大小的调整。

    .element {
      position: absolute;
      bottom: calc(50% - 20px);
    }

当使用 calc() 时有一些规则需要注意,例如:

  • 运算符之间需要有空格。calc(50% -50px) 是无效的,而 calc(50% - 50px) 是有效的。
  • 不能进行 0 除运算,也就是说分母不能为 0。
  • 在进行乘法和除法时,至少有一个值必须是数值(即不带单元的数字)。

总的来说,calc() 是一个强大的 CSS 工具,可以在响应式设计和复杂布局管理中提供极大的帮助。

@yanlele yanlele added CSS CSS 相关问题 TOP100互联网 公司标签 labels Apr 13, 2024
@yanlele yanlele added this to the milestone Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS CSS 相关问题 TOP100互联网 公司标签
Projects
None yet
Development

No branches or pull requests

1 participant