Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the CSS calc() function is not working?

Tags:

css

css-calc

Why this one calc(100vw/4) works but neither of the following do?

  1. calc(100vw/4-(10-4))
  2. calc(100vw/4-(10px-4))
  3. calc(100vw/4-(10px-4px))
  4. calc(100vw/4-calc(10px-4px))

How do I manage the expression calc(100vw/x-(10px-x)) where x gets replaced in a SASS loop to work?

like image 806
super.t Avatar asked Dec 19 '22 03:12

super.t


1 Answers

You need to add spaces between operators, it's a common mistake to forget them. We can also nest operation using calc as many as we want but they are equivalent to simple parentheses.

From the documentation:

Note: The + and - operators must be surrounded by whitespace. For instance, calc(50% -8px) will be parsed as a percentage followed by a negative length—an invalid expression—while calc(50% - 8px) is a percentage followed by a subtraction operator and a length. Likewise, calc(8px + -50%) is treated as a length followed by an addition operator and a negative percentage.

The * and / operators do not require whitespace, but adding it for consistency is both allowed and recommended.

Note: It is permitted to nest calc() functions, in which case the inner ones are treated as simple parentheses.

.one {
  background: red;
  width: calc(100% - 150px);
  margin-top: calc(20px + calc(40px * 2)); /*Same as calc(20px + (40px * 2))*/
  height: calc(100px - 10px);
  padding: calc(5% + 10px) calc(5% - 5px);
}
<div class="one">

</div>

enter image description here


Same logic when dealing with max(),min() and clamp() since they accept the same argument as calc()

Related: Why doesn't min() (or max()) work with unitless 0?

like image 148
Temani Afif Avatar answered Jan 08 '23 16:01

Temani Afif