I did something similar for the Instagram thumbnail image gallery, where the image can be smaller than the size of the thumbnail, but not larger, and the thumbnails are assembled at 100% of the width of the container. This is so that the images occupy the entire width without stretching them.
You can get this effect quite easily if you use a CSS preprocessor. If you are not using a CSS preprocessor, then there will be more effort.
Here is a CodePen example that contains a link to another demo.
In essence, the magic part is Stylus CSS:
floaterSize = 300px .floater max-width floaterSize for num in (1..10) @media screen and (min-width: floaterSize*num) .floater width unit(100/(num+1), '%')
What he does is that he calculates the ideal width for the element with a maximum width limit and then adds another to the line after reaching this limit, which creates a liquid arrangement of elements, each of which does not cover more than the specified maximum width (in in this case, floaterSize ).
The first few generated ones are as follows:
@media screen and (min-width: 300px) { .floater { width: 50%; } } @media screen and (min-width: 600px) { .floater { width: 33.333333333333336%; } } @media screen and (min-width: 900px) { .floater { width: 25%; } }
This is a mobile approach. Essentially, if your window is 299 pixels or less, you should see 1 block, but if it has 599 pixels or less, it will hold 2 blocks, and 899 pixels will hold 3 blocks, etc., until you want go higher in screen width. The provided Stylus will write 10 media queries, up to @media screen and (min-width: 3000px) .
All the generated CSS that this Stylus code provides can be seen below.
@media screen and (min-width: 300px) { .floater { width: 50%; } } @media screen and (min-width: 600px) { .floater { width: 33.333333333333336%; } } @media screen and (min-width: 900px) { .floater { width: 25%; } } @media screen and (min-width: 1200px) { .floater { width: 20%; } } @media screen and (min-width: 1500px) { .floater { width: 16.666666666666668%; } } @media screen and (min-width: 1800px) { .floater { width: 14.285714285714286%; } } @media screen and (min-width: 2100px) { .floater { width: 12.5%; } } @media screen and (min-width: 2400px) { .floater { width: 11.11111111111111%; } } @media screen and (min-width: 2700px) { .floater { width: 10%; } } @media screen and (min-width: 3000px) { .floater { width: 9.090909090909092%; } }