I have a layout that has a lot of images and in order to make my layout responsive, I am using percentage values for the width and height. Each image is nested into a div tag and the images are given a width and height of 100% of their parent's width and height.
Using media queries, depending on the screen size, I am changing the width of the divs that contain each images as follows:
When only 2 images can fit on the screen: 50% width each div that contains an image
When only 3 images can fit on the screen: 33.33% width each div that contains an image
When only 4 images can fit on the screen: 25% width each div that contains an image
When only 5 images can fit on the screen: 20% width each div that contains an image
etc..
But those images look pixelated when they get bigger, they loose quality... How do I make them not loose quality and not look pixelated when they go from having a 20% width to having a 50% width?
Do I use the srcset technique? What responsive image technique do I use to allow my images to scale to any size without getting pixelated?
If you want to retain the original sizes of the images and make it responsive, there is a cool way to do this!
-> You can use a flexbox. (That is the secret)
Run the following code below and open the code in full screen. Then change your screen width by minimizing the screen and watch how responsive the images are using flexbox.
The magic down below is caused by a CSS property known as flex-wrap: wrap; Which will wrap all the images in a responsive container!
You can make something like this:-
If you resize the image they will align and be responsive!