:root {
--offset: 30px;
}
.grid-cols-custom {
/* Calculate the column widths using custom properties and calc() */
grid-template-columns: calc((424px / (100% - var(--offset))) * 100%) calc((608px / (100% - var(--offset))) * 100%);
here in a grid I have 2 items one is 424px and other is 608px . I want them to be responsive. But padding and gap creates an visual error. so i want a percentage with a calculation :
logic : ((424px/ (full width of the div – 30px )) * 100%) so our divs will always adjust their width
please help me . Here is a problem told by css validator ‘one operand must be a number’
i have been at this for 4 hours :{
2
Answers
I believe there is a much simpler option for you. It’s responsive and basically the same sizes with a 30px gap. I think your solution is too complicated and brittle, especially to be responsive.
Your numbers:
Based on the values you’re using, this is roughly a 40% to 60% ratio. Using
4fr 6fr
for the columns or even better2fr 3fr
should be close enough.We could get it even closer if you wanted by doing
424fr 608fr
. It’s odd numbers, but that would be the exact of what you’re looking for. Of the total width, use 424 fractions for the first column and 608 fractions for the second. Then add the gap.According to the calc docs on MDN,
The error says ‘one operand must be a number.’ That makes me think using incorrect units could be the problem (or missing whitespace around operands), even though MDN says you can use different units in the calc method.
But in this specific scenario, a responsive grid is better off using the
fr
unit. MDN fr unit These can define the exact sizes you want and provide responsive re-sizing. The calc method is probably introducing an operand error, and without seeing your entire code it is difficult to pinpoint the exact line it is happening on. You can use a purely grid-approach without calc.Change the values of each fr unit to the exact ones you need