I have a 3×3 css grid for which I want to have the respective two outer column and row sizes be calculated. The middle ones are 1fr
.
So far I have:
(see also this codepen)
html {
--zoom-scale: 1;
}
body {
--weight: calc(((var(--zoom-scale) - 1) / 2 * 1fr));
}
.grid {
grid-template-columns: var(--weight) 1fr var(--weight);
grid-template-rows: var(--weight) 1fr var(--weight);
/* Expecting this for --weight = 1 */
/* grid-template-columns: 0 1fr 0;
grid-template-rows: 0 1fr 0; */
/* And this for --weight = 3 */
/* grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr; */
}
My Chrome never accepts any calculated value for the template properties though. What’s the correct syntax here?
As you may have guessed, the overall problem I’m trying to solve here is to divide a given rectangular space into a grid in such a fashion that the middle cell can be scaled up by --zoom-scale
and still be entirely contained in the grid container.
2
Answers
You cannot perform calculation with
fr
you can verify this by usinggrid-template-columns: calc(1 * 1fr);
and you will get an invalid value.You can switch to percentage where you go from
0
to50%
You can also use margin to perform the same task with less of code
The
fr
unit isn’t a mathematical operator (+, -, ×, ÷), so it can’t be used in thecalc()
function.The
fr
unit is simply a representation of free space. It’s not an actual unit of length.The
calc()
function is a calculation using mathematical operators.A solution to the problem in the question has been provided in another answer.