calculating margin offset in order to stack playing cards to fit inside box

34 Views Asked by At

So, I have x amount of cards and a container width that is variable. I am trying to calculate how much minus margin i have to apply to each card in order to make them all fit inside the container.

I started to tackle the problem by multiplying the witdh of a single card to the number of cards total to get how much space is needed to show all the cards fully.

x = 21
total-width = 143 * x = 3003

Then i thought to divide the total width with the width of the container.

total-width / 765 = 3.925

This number is obviously wrong. Adding -3px to each card would not give the desired result.

What am I missing here? My math knowledge is REALLY bad, so sorry if the question is low-grade.

Visualization of what I am trying to achieve: enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

One card is displayed fully and consumes 143 units of the available width, and the other $x-1$ cards consume $m$ units of width each, so you want to solve $143+(x-1)m=765$ for $m$, yielding $m=(765-143)/(x-1)$. For $x=21$, this is $$m=\frac{765-143}{x-1}=\frac{622}{20}=31.1.$$