Is it possible to do mathematics inside CSS?

There’s a CSS function called calc that is starting to get pretty good support. The syntax works as followed:

width: calc(50% - 100px);

(Note that the whitespace around the operators is significant)

This allows true dynamic computational support in CSS. With a preprocessor, you can only combine static lengths with static lengths, and relative lengths with relative ones.

At this point in time calc has achieved widespread support and should be safe to use unless you have specific needs for supporting legacy browsers.

Leave a Comment