I've read the documentation for calc()
and understand the syntax, but being in CSS, where no code is executed, I can't figure out when it should be used.
The paradigmatic example sounds quite simple to me and I don't know if it is very useful:
/* property: calc(expression) */
width: calc(100% - 80px);
Are there canonical cases for the use of calc()
? Could you name an example in which its use is practical for a solution of a real case?
I see that its use is quite widespread across browsers (93% according to Can I use... ). How recommended is it to use it, and what would be its equivalent for browsers that don't support it (IE 8 and earlier, with some issues with IE 9)?
I know several cases where its use is not canonical, but it is much easier to do
calc
it than in any other way.Positioning of background images
It is quite easy to position a background image
But what happens when we want to position the background image in relation to the right and bottom sides and we don't know in advance the size of the containing element? With
calc
is the simplest.Much easier that way.
Creation of a GRID system
Imagine that you want to design your own GRID system, let's say 7 columns.
This would cause that in CSS code we would have decimal figures that are difficult to read and understand:
We can make the code more readable and even almost "self-explanatory" by replacing the ugly decimals with the use of
calc
.Again, it seems easier to do it this way.
Position and dimension floating elements.
Imagine that you have to position 2 floating elements in a variable (and unknown) width container maintaining the aspect ratio between them, and with a fixed separation (margin) between them.
Considerations to take into account
Syntax
It is important to respect the syntax of
calc
:calc(100%[espacio]-[espacio]50px)
Things likecalc(100%[espacio]-50px)
or willcalc(100%-50px)
not work in all browsers.backup rules
Although most browsers support
calc
, it's good practice to use a fallback rule for browsers that don't:You have these and other use cases at https://css-tricks.com/a-couple-of-use-cases-for-calc/
Man, put like this, I can think of uses for when you want to divide a container into equal parts, without having to do the calculation of how many px it would be.
Really anything you can think of. But in my opinion, perhaps the best option would be in combination with some Less or Sass type language where you can store values in variables. And as you progress through the code you may not really care what value it has. For example:
This example is very small and perhaps the potential is not really appreciated, but imagine a file with 500-700 lines of Less where you know that a container is 500px in size and you want two inside that are half each, but you don't know how much is the first container really worth. By combining Less and the function
calc()
you can get half the value of a less variable. (half, a third, whatever you want,...)But this, like almost everything, is as far as your imagination goes.
Minimum control is another direct application of
calc()
, a practical example can be seen with fonts or typography.Suppose I want to adapt my font size according to the width of the viewport, for which I would use units
vw
:So far so good, 3% of the width works for my web app, but I want to ensure that the font size is a minimum of 0.7rem regardless of resolution and that the font size still "adapts". Although I can do it with a media query and a
(max-width: ...)
, with calc I can maintain a font that adapts to all resolutions:In summary, some media query could be eliminated if it is used
calc()
and keep the sizes adaptable without losing the aesthetics.As you have already been given several interesting examples of use and I don't think I can contribute anything more in this regard, I will try to answer the other questions.
Although the MDN documentation is very good, it is always advisable to also see what the specification says, in this case https://www.w3.org/TR/css3-values/#calc-notation where the syntax and peculiarities are explained in detail of this function.
The example you give to illustrate the question:
width: calc(100% - 80px);
it may seem simple and useless as it is an apparently simple mathematical calculation, but if we look more closely we see that there are two different units of measurement: percentages and pixels; therein lies the magic ofcalc()
, it does the calculations in real time by subtracting the 80 pixels from the current width of the element, if this size is modified it will recalculate the new width.The possibility of doing calculations with different units on the fly is one of the
calc()
most useful and interesting qualities.The support of
calc()
is quite broad but as you ask for an alternative several options occur to me: