css calc - round down with two decimal cases

前端 未结 3 2045
温柔的废话
温柔的废话 2021-01-17 09:09

I have the following situation:

div {
    width: calc((100% / 11) - 9.09px);
}

In the context, 100% = 1440px, and <

3条回答
  •  一整个雨季
    2021-01-17 09:50

    Unfortunately, there is not a native way in CSS to round (or ceil/floor) numbers.

    However — you mentioned you are using Sass. I found a small Sass library that can round, floor, and ceil numbers to a specified precision.

    For example, if you had a had 94.546 you could use decimal-floor(94.546, 2) which would return 94.54.

    Unfortunately, this might not help if you have to use calc() to calculate on the fly with CSS. However, if you can pre-calculate the width and floor it with Sass it would fit your needs. A possible solution could be using @media queries as a way to set breakpoints and use those breakpoints in your Sass preprocessing.

提交回复
热议问题