2 corrected wrong formula in point 2
source | link

I agree with Marjan about leaving empty spaces as they are instead of adding irrelevant images. Unless ofcourse, they add to the aesthetics and aren't a burden on the eyes (depends on your design).

If you'd like to avoid the empty spaces on the right of the images, you could modify your code to match the total width of the images in a row with the width of your gallery container, preferably in the following manner.
1. In the case of three images in a row, calculate the width of the empty space on the right ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 3) and add this value to the width of each image, scaling the height proportionately. 
2. In the case of two images in a row, do the same as above but calculate the gap using ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 2) and add this value to the width of each image, scaling the height proportionately.

You can also set a certain tolerance limit for the width of the empty gap on the right, and if the width meets your tolerance limit, you could show a third image in the same row by reducing the width of all three images so that they are equal to the width of your gallery container.

I'd suggest not playing around more with the empty spaces below each image (if any), as you'd need more code and javascript processing, effectively increasing your page load time even more. Either adjust the empty spaces to the right of your container, OR use a similar approach to adjust the spaces below each image, NOT both.

I agree with Marjan about leaving empty spaces as they are instead of adding irrelevant images. Unless ofcourse, they add to the aesthetics and aren't a burden on the eyes (depends on your design).

If you'd like to avoid the empty spaces on the right of the images, you could modify your code to match the total width of the images in a row with the width of your gallery container, preferably in the following manner.
1. In the case of three images in a row, calculate the width of the empty space on the right ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 3) and add this value to the width of each image, scaling the height proportionately. 2. In the case of two images in a row, do the same as above but calculate the gap using ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 2) and add this value to the width of each image, scaling the height proportionately.

You can also set a certain tolerance limit for the width of the empty gap on the right, and if the width meets your tolerance limit, you could show a third image in the same row by reducing the width of all three images so that they are equal to the width of your gallery container.

I'd suggest not playing around more with the empty spaces below each image (if any), as you'd need more code and javascript processing, effectively increasing your page load time even more. Either adjust the empty spaces to the right of your container, OR use a similar approach to adjust the spaces below each image, NOT both.

I agree with Marjan about leaving empty spaces as they are instead of adding irrelevant images. Unless ofcourse, they add to the aesthetics and aren't a burden on the eyes (depends on your design).

If you'd like to avoid the empty spaces on the right of the images, you could modify your code to match the total width of the images in a row with the width of your gallery container, preferably in the following manner.
1. In the case of three images in a row, calculate the width of the empty space on the right ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 3) and add this value to the width of each image, scaling the height proportionately. 
2. In the case of two images in a row, do the same as above but calculate the gap using ((TotalWidth - imageWidth1 - imageWidth2) / 2) and add this value to the width of each image, scaling the height proportionately.

You can also set a certain tolerance limit for the width of the empty gap on the right, and if the width meets your tolerance limit, you could show a third image in the same row by reducing the width of all three images so that they are equal to the width of your gallery container.

I'd suggest not playing around more with the empty spaces below each image (if any), as you'd need more code and javascript processing, effectively increasing your page load time even more. Either adjust the empty spaces to the right of your container, OR use a similar approach to adjust the spaces below each image, NOT both.

1
source | link

I agree with Marjan about leaving empty spaces as they are instead of adding irrelevant images. Unless ofcourse, they add to the aesthetics and aren't a burden on the eyes (depends on your design).

If you'd like to avoid the empty spaces on the right of the images, you could modify your code to match the total width of the images in a row with the width of your gallery container, preferably in the following manner.
1. In the case of three images in a row, calculate the width of the empty space on the right ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 3) and add this value to the width of each image, scaling the height proportionately. 2. In the case of two images in a row, do the same as above but calculate the gap using ((TotalWidth - imageWidth1 - imageWidth2 - imageWidth3) / 2) and add this value to the width of each image, scaling the height proportionately.

You can also set a certain tolerance limit for the width of the empty gap on the right, and if the width meets your tolerance limit, you could show a third image in the same row by reducing the width of all three images so that they are equal to the width of your gallery container.

I'd suggest not playing around more with the empty spaces below each image (if any), as you'd need more code and javascript processing, effectively increasing your page load time even more. Either adjust the empty spaces to the right of your container, OR use a similar approach to adjust the spaces below each image, NOT both.